Search code examples
templatesc++11template-aliasesexplicit-instantiation

Explicitly instantiate class through template alias


Is it possible to explicitly instantiate a template class through a template alias?

If so, how? Otherwise, can someone point to the ISO paper in which this was discussed and decided against?

template<class T>
struct A { };

/// Explicit instantiate A for int:
template struct A<int>;

/// Alias
template<class T>
using B = A<T>;

/// Explicitly instantiate A for double via alias B:
template struct B<double>;
/// error: elaborated type refers to a non-tag type

Shouldn't this instantiate A<double> since B<T> is just a different name for A<T> ?


Solution

  • This is indirectly forbidden, because:

    7/3 forbids writing the explicit specialization without a class-key (class, struct, or union):

    In a simple-declaration, the optional init-declarator-list can be omitted only when declaring a class (Clause 9) or enumeration (7.2), that is, when the decl-specifier-seq contains either a class-specifier, an elaborated-type-specifier with a class-key (9.1), or an enum-specifier.

    7.1.6.3/2 forbids combining a class-key with an alias template specialization:

    3.4.4 describes how name lookup proceeds for the identifier in an elaborated-type-specifier. ... If the identifier resolves to a typedef-name or the simple-template-id resolves to an alias template specialization, the elaborated-type-specifier is ill-formed.