简体   繁体   中英

Is initializer_list considered part of the C++ core language?

I ask because auto deduces {} to be initializer_list . I don't know of any other class in the standard library that the core language depends on like this. You could take out vector or array and C++ would still function, but take out initializer_list and it would break.

What you call {} (specifically = {...} ) the standard calls copy-list-initialization .

And yes, std::initializer_list is given special consideration in the wording of the standard .

If the placeholder-type-specifier is of the form type-constraint auto , the deduced type T replacing T is determined using the rules for template argument deduction. If the initialization is copy-list-initialization, a declaration of std::initializer_list shall precede ([basic.lookup.general]) the placeholder-type-specifier .

[Example 1:
auto x1 = { 1, 2 };             // decltype(x1) is std​::​initializer_­list<int>
auto x2 = { 1, 2.0 };           // error: cannot deduce element type
auto x3{ 1, 2 };                // error: not a single element
auto x4 = { 3 };                // decltype(x4) is std​::​initializer_­list<int>
auto x5{ 3 };                   // decltype(x5) is int

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM