Are all std :: tuple constructors needed?

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

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

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

From what I understood about rvalue links, I don’t understand why the first version is required, since the same parameters can be passed to the second version. Links will be redirected and no one will be wiser, especially if the semantics of movement are not mentioned.

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

+6
source share
2 answers

Here is a simplified example:

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

The second constructor requires a certain type of template type inference. This does not work in all cases, for example. with lists of initializers. The following initialization only works if the first constructor is available:

 auto f = foo<std::vector<int>>{ { 1, 2, 3 } }; 
+10
source

This is for redirecting RValue links and optimized for building relocation. The first version is used for lvalues. See the following link for a better explanation.

http://thbecker.net/articles/rvalue_references/section_07.html

0
source

Source: https://habr.com/ru/post/951969/


All Articles