There has already been a similar question on SO, but I want to stress another aspect of braced-init-lists. Consider the following:
auto x = {1}; //(1)
This is ill-formed (8.5.4/2) unless the header <initializer_list>
is included. But why? The standard says, that the template std::initializer_list
is not predefined. Does this mean, that declaration (1) introduces a new type? In all other situations, where auto
may be used such as
auto y = expr;
where expr
is an expression, the type auto deduces already exists. On the other hand, from a logical point of view, the compiler must assign an implicite type to the construct {1}
, for which std::initializer_list
is then another name. But in declaration (1) we do not want to name this type. So why must this header be included. There is a similar situation with nullptr
. Its type implicitely exists, but to name it explicitely you have to include <cstddef>
.