initializer_list with auto contains multiple expressions

≯℡__Kan透↙ 提交于 2019-12-03 23:44:30

The rule of auto type deduction changed since C++17.

(since C++17)
In direct-list-initialization (but not in copy-list-initalization), when deducing the meaning of the auto from a braced-init-list, the braced-init-list must contain only one element, and the type of auto will be the type of that element:

auto x1 = {3}; // x1 is std::initializer_list<int>
auto x2{1, 2}; // error: not a single element
auto x3{3};    // x3 is int
               // (before C++17 x2 and x3 were both std::initializer_list<int>)

So before C++17, all the variables in your sample work fine and have type std::initializer_list<int>. But since C++17, for direct initialization (i.e. for x11 and x22) the braced-initializer must contain only one element (and their type would be the type of the element) then become ill-formed code.

See N3922 and N3681 for more.

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