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

孤街浪徒 提交于 2019-11-28 22:51:22
Praetorian

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.

Howard Hinnant

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!