Search code examples
c++language-lawyerinitializer-list

Can std::initializer_list be specialized?


While going through the various rules on list-initialization, I found this in dcl.init.list#3.6:

Otherwise, if T is a specialization of std​::​initializer_­list<E>, the object is constructed as described below.

On the other hand, in the synopsis of std::initializer_list, in support.initlist, I found the following statement:

If an explicit specialization or partial specialization of initializer_­list is declared, the program is ill-formed.

These appear to be contradictory statements, so what am I misunderstanding?


Solution

  • "A template specialization" has two distinct meanings:

    1. "Explicit (full) specialization" or "partial specialization" - a language construct that changes the meaning of a template for some combination of template parameters.

    2. Something that was generated from a template by substituting template arguments into it.
      In other words, if you specify template arguments for a template, the resulting type/function/variable/... is a specialization of that template. E.g. std::vector<int> is a specialization of std::vector.

    Looks like the first passage you quote uses (2).

    So "if T is a specialization of std​::​initializer_­list<E>" roughly means "if there exists such E that std::is_same_v<T, std::initializer_list<E>>", or "if T is a std::initializer_list<E>".