Explicitly instantiate class through template alias
Asked Answered
B

1

20

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

Byssinosis answered 4/8, 2014 at 11:59 Comment(5)
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<double> is also grammatically a simple-template-id.Madeline
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.Saliva
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.Brioche
@Brioche if I can't use an alias as the original template I'll need to fall back to a macro here.Byssinosis
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.Byssinosis
M
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.

Madeline answered 4/8, 2014 at 12:36 Comment(3)
This makes it very difficult when you have more than one level of partial template specializations. Is anyone aware of this being in works to be allowed for in future releases?Acicular
@Acicular The current draft standard still has essentially the same rule. I'm not aware of any proposal to change it, though I don't keep a very close eye on the proposal list. It's not likely anyone else will see your question above soon.Madeline
thanks for your response. I'll explore avenues to get it considered. As you know, its very difficult with more than one level of partial template specializations. It needs to repeat all the args, and changing any requires change at all levels below.Acicular

© 2022 - 2024 — McMap. All rights reserved.