I have a following header:
#include <string_view>
#include <tuple>
std::tuple<int, int, int> foo(std::string_view sv);
class s {
public:
s(std::string_view sv);
private:
int x, y, z;
};
Is there any way to implement s::s so that it assigns x, y, z using foo in initializer list? Assume that foo is expensive so should not be called multiple times, and due to interdependencies between the returned values, it cannot be broken down either.
Without using initializer list, this task is trivial:
s::s(std::string_view sv) {
std::tie(x, y, z) = foo(sv);
}
But it would not work if x, y, z were types without default constructor. One solution I did find is delegating to a private constructor:
s::s(std::string_view sv) : s{ foo(sv) } {}
s::s(const std::tuple<int, int, int>& t) :
x{ std::get<0>(t) },
y{ std::get<1>(t) },
z{ std::get<2>(t) }
{}
However I find it a bit unelegant as it requires modifying the header with an implementation detail. Are there any other solutions to this problem?