Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ={} initialization doesn't work for tuple?

To me a pair is just special case of a tuple, but following surprises me:

pair<int, int> p1(1, 2);   // ok tuple<int, int> t1(1, 2);  // ok  pair<int, int> p2={1, 2};  // ok tuple<int, int> t2={1, 2}; // compile error 

Why there is difference when we use {} to initialize tuple?

I tried even g++ -std=c++1y but still has error:

a.cc: In function 'int main()': a.cc:9:29: error: converting to 'std::tuple<int, int>' from initializer list would use explicit constructor 'constexpr std::tuple<_T1, _T2>::tuple(_U1&&, _U2&&) [with _U1 = int; _U2 = int; <template-parameter-2-3> = void; _T1 = int; _T2 = int]'      tuple<int, int> t2={1, 2};                              ^ 
like image 645
Deqing Avatar asked Aug 19 '15 00:08

Deqing


People also ask

How do you initialize a tuple?

Initialize a Tuple A tuple with values can be initialized by making a sequence of values separated by commas. It is important to keep in mind that if you want to create a tuple containing only one value, you need a trailing comma after your item. tup2 = 'Michael', # This is a string, NOT a tuple.

How do you initialize a tuple in CPP?

There are two ways in which we can initialise a Tuple. Specify the elements of Tuple in the declaration of Tuple. Use make_tuple() function to initialise a tuple variable.

What is std :: Initializer_list?

An object of type std::initializer_list<T> is a lightweight proxy object that provides access to an array of objects of type const T .


2 Answers

In addition to Praetorian's correct answer (which I've upvoted), I wanted to add a little more information...

Post-C++14, the standard has been changed to allow:

tuple<int, int> t2={1, 2};  

to compile and have the expected semantics. The proposal that does this is N4387. This will also allow constructs such as:

tuple<int, int> foo() {     return {1, 2}; } 

It only allows it if all T in the tuple are implicitly contructible from all arguments.

As a non-conforming extension, libc++ already implements this behavior.

like image 130
Howard Hinnant Avatar answered Sep 18 '22 07:09

Howard Hinnant


The tuple constructor you're trying to call is explicit, so copy-list-initialization will fail. The corresponding pair constructor is not explicit.

Change your code to

tuple<int, int> t2{1, 2}; 

and it'll compile.

like image 37
Praetorian Avatar answered Sep 20 '22 07:09

Praetorian