Search code examples
c++templatesc++11template-specializationpartial-specialization

Partial Specialization of Alias Templates


Partial specializations of alias templates are not permitted:

For example, trying to be creative, yields this error in clang:

template <typename T>
using unwrapped_future_t = T;

template <typename T>
using unwrapped_future_t<future<T>> = typename future<T>::value_type;
                        ^~~~~~~~~~~
> error: partial specialization of alias templates is not permitted

Why is this not permitted?


Solution

  • You can find the answer in the original proposal of alias templates:

    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)