Is it possible to initialize all elements of std::tuple
by the same argument, using the non-default constructors of the underlying types?
template <typename... TElements>
struct Container {
// I'd wish to be able to do something like this:
Container(Foo foo, Bar bar)
: tuple(foo, bar)
{}
std::tuple<TElements...> tuple;
};
The point is that I don't know the tuple size (it's templated by a variadic parameter), so I can't duplicate the arguments as many times as I need. The only thing I know is that all types in TElements
have a constructor taking Foo
and Bar
as arguments and don't have a default constructor.
We want to do variadic expansion (to get just the right amount of parameters), but we have to put a ‘hint’ to tie the expansion to whichever pack it is we want to match:
with double parameter pack expansion you can (try to) construct each element of a given tuple class with all given parameters to a function:
And then somewhere in the code:
full working example: Link
Edit:
Since @Rakvan deleted his answer, I'll preserve the second (correct) part of it:
here is a working exaple
The clearest way is just to construct each element in the
tuple
constructor argument list:This will result in move (or copy) constructing each element of the tuple from its corresponding constructor parameter; if this is unacceptable you could use piecewise construction:
Unfortunately in this case we have to do some kind of gymnastics (here
sizeof
and a comma operator) to get the variadic listTElements
mentioned and ignored.