相关疑难解决方法(0)

为什么= {}初始化对元组不起作用?

对我而言,a pair只是a的特例tuple,但令我惊讶的是:

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
Run Code Online (Sandbox Code Playgroud)

我们{}用来初始化时为什么会有区别tuple

我试过甚至g++ -std=c++1y但仍然有错误:

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, …
Run Code Online (Sandbox Code Playgroud)

c++ tuples initializer-list c++11 list-initialization

25
推荐指数
2
解决办法
5478
查看次数