Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are all the std::tuple constructors necessary?

std::tuple contains, amongst others, the following constructors:

explicit tuple( const Types&... args );

template< class... UTypes >
explicit tuple( UTypes&&... args );

Both have equivalent descriptions in that they initialise each of the elements with the corresponding value in args. The only difference is that in the second the parameters are forwarded.

From what I have understood about rvalue references, I don't see why the first version is required since the same parameters could be passed into the second version. The references would be forwarded and no-one would any the wiser especially as there is no mention of move semantics.

Can anyone explain what it is that makes both constructors necessary?

like image 308
DrYap Avatar asked Aug 18 '13 15:08

DrYap


1 Answers

Here is a simplified example:

template <typename T>
struct foo
{
    foo(const T&);
    template <typename U>
    foo(U&&);
};

The second constructor requires some kind of template type deduction. This doesn't work in all cases, e.g. with initializer lists. The following initialization only works, if the first constructor is available:

auto f = foo<std::vector<int>>{ { 1, 2, 3 } };
like image 64
nosid Avatar answered Nov 13 '22 15:11

nosid