cigien
cigien

Reputation: 60268

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?

Upvotes: 0

Views: 261

Answers (2)

HolyBlackCat
HolyBlackCat

Reputation: 96719

"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>".

Upvotes: 4

NathanOliver
NathanOliver

Reputation: 180955

There is no contradiction.

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

Means you cannot declare a specialization. The compiler itself is allowed to stamp out specializations of std​::​initializer_­list


The thing that might be causing you issue is that the concrete type you get out of a template is called a specialization. This is what the first paragraph is talking about. The second paragraph is talking about actually defining/declaring a specialization for std::initializer_list

Upvotes: 2

Related Questions