From [dcl.init.list]
The template
std::initializer_list
is not predefined; if the header <initializer_list> is not included prior to a use ofstd::initializer_list
— even an implicit use in which the type is not named — the program is ill-formed.
Since std::initializer_list
is special-cased by the compiler anyway, why is it not treated as a first-class syntax of the language?
My thoughts on some possibilities and its counter-arguments:
Namespace pollution
Could be solved just like std::size_t
, the inclusion of the header only introduces the identifier.
Note how auto sz = sizeof(0);
is well-formed even without the inclusion of any headers, as opposed to auto il = {1, 2, 3};
being ill-formed.
Compilation overhead
libstdc++ implementation of std::initializer_list
is literally less than 50 lines with no dependency on other headers. How big of overhead can that be?