Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the rationale for requiring inclusion of <initializer_list>?

From [dcl.init.list]

The template std​::​initializer_­list is not predefined; if the header <initializer_­list> is not included prior to a use of std​::​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?

like image 225
Passer By Avatar asked Mar 19 '18 17:03

Passer By


1 Answers

It seems that there is no rational behind it. Maybe at the time there was an hope that further language resolutions and modifications allow an implementation, but this doesn't seems real now.

In any case further reading about the argument can be found here.

like image 102
Zig Razor Avatar answered Oct 18 '22 13:10

Zig Razor