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.
CodePudding user response:
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 typeT
replacing T is determined using the rules for template argument deduction. If the initialization is copy-list-initialization, a declaration ofstd::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