Alias template specialisation

What the Standard means by “specialization” is the transformation of a generic template to a more specialized entity. For example, instantiating a non-member class template yields a class that’s not a template anymore. The term “specialization” is two fold, and can refer to a generated specialization (which is a specialization that was instantiated, possibly from a partial specialization) and to an explicit specialization (which is what you referred to).

Alias templates aren’t instantiated and there aren’t specializations of them. There is nothing they could instantiate to. Instead, whenever their name is followed by a template argument list, the type denoted is the type you get by replacing the name and argument list by the alias’ed type, replacing all template parameter references with the arguments given in the argument list. That is, rather than the specialization of it being an alias, the alias template itself serves as an alias, without the need to instantiate anything. This replacement is done very early. Consider:

template<typename T> using ref = T&;
template<typename T> void f(ref<T> x) { x = 10; }
int main() { int a; f(a); return a; /* 10 */ }

The replacement is done at the time ref<T> is named (such a names are used to refer to class or function template specializations; hence the spec describes such names to “refer to the specialization of an alias template”). That is, the parameter of f has type T&, and thus, T can be deduced. This property is preventing explicit or partial specializations of alias templates. Because in order to pick the correct specialization of ref, it needs to know T. But to know it, it needs to compare ref<T> against the argument type to deduce T. It’s summarized in the paper N1406, “Proposed addition to C++: Typedef Templates”, section 2.2

2.2 The Main Choice: Specialization vs. Everything Else

After discussion on the reflectors and in the Evolution WG, it turns out that we have to choose between two mutually exclusive models:

  1. A typedef template is not itself an alias; only the (possibly-specialized) instantiations of the typedef template are aliases. This choice allows us to have specialization of typedef templates.

  2. A typedef template is itself an alias; it cannot be specialized. This choice would allow:

    • deduction on typedef template function parameters (see 2.4)
    • a declaration expressed using typedef templates be the same as the declaration without
      typedef templates (see 2.5)
    • typedef templates to match template template parameters (see 2.6)

It should be noted that the quoted paper, which favors option 1, did not make it into C++0x.


EDIT: Because you desperately want to have a spec quote saying so explicitly. 14.5p3 is one that does

Because an alias-declaration cannot declare a template-id, it is not possible to partially or explicitly specialize an alias template.

Leave a Comment