16

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> ?

gnzlbg
  • 7,135
  • 5
  • 53
  • 106
  • Interesting. You can't use a `typedef` because 14.7.2/3 requires an explicit instantiation of a class to use a _simple-template-id_. But `B` is also grammatically a _simple-template-id_. – aschepler Aug 04 '14 at 12:08
  • 3
    A similar example with out-of-class member definitions (rather than explicit instantiations) was discussed last week on the standards reflector. There may be an issue to be resolved in the standard. – Jonathan Wakely Aug 04 '14 at 12:21
  • I find it counter-intuitive that you would be allowed to explicitly instantiate through template aliases. After all, you also cannot explicitly specialize a template alias. Just do all the work through the underlying template, and use the alias as, well, an alias. – TemplateRex Aug 04 '14 at 12:33
  • @TemplateRex if I can't use an alias as the original template I'll need to fall back to a macro here. – gnzlbg Aug 04 '14 at 12:35
  • Alias are convenient syntax sugar but never necessary. If I need syntax sugar for the explicit instantiation then aliases are not enough. Defining an alias _and_ a macro adds unnecessary boilerplate in situations where the macro is enough. – gnzlbg Aug 04 '14 at 12:37

1 Answers1

13

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.

aschepler
  • 70,891
  • 9
  • 107
  • 161