std::initializer_list, braced initialization and header

  • A+
Category:Languages

While reading about a different topic I came across a weird behaviour, at least to me. This whole thought originated from the special interactions between auto and braces. If you write something like:

auto A = { 1, 2, 3 } 

the compiler will deduce A to be a std::initializer_list. The weird thing is that a similar rule applies not only to auto, where there can be special reasons for it, but also to other things. If you write the following:

template<typename T> void f(std::vector<T> Vector) {     // do something } 

you can't of course call it in this way:

f({ 1, 2, 3}); 

even though a std::vector can be braced initialized. However, if you substitute the std::vector with std::initializer_list, the call works and the compiler will properly deduce int as the type T. The more interesting thing is, however, that in the former case you need to #include <vector>, in the latter you don't need to #include <initializer_list>. This made me think and after a test I realized somehow std::initializer_list don't need its own header, so it is in some way part of the "base" features.

Moreover, for everything to make sense, std::initializer_list should be to standard objects in more or less the same way lambdas are to callable objects (in the strictest meaning, that is an object with a operator()). In other words, unnamed braced definitions should default to std::initializer_list just like lambdas are (mostly) unnamed callable objects.

Is this reasoning correct? Moreover, can this behaviour be changed and, if so, how?

UPDATE: the header for initializer_list was found to be included transitively from iostream (really weird). However, the question remains: why the call works for std::initializer_list and not for std::vector?

 


This is not correct, you must include the <initializer_list> header from [dcl.init.list]p2:

... 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 (9.1.7.4) — the program is ill-formed.

Mostly likely you are including the header transitively, perhaps from <vector>

We can see from a live godbolt examples that having no includes we obtain a diagnostic as required from gcc/clang/MSVC e.g.:

error: use of undeclared identifier 'std'     void foo( std::initializer_list<int>) {           ^ 

and including either <vector> or <iostream> we no longer obtain a diagnostic.

Why it does not deduce as you expect is covered by [temp.deduct.type]p5 which tells us this is a non-deduced context:

The non-deduced contexts are:
...
- A function parameter for which the associated argument is an initializer list ([dcl.init.list]) but the parameter does not have a type for which deduction from an initializer list is specified ([temp.deduct.call]).> [ Example:

template<class T> void g(T); g({1,2,3});                 // error: no argument deduced for T 

— end example  ]
...

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: