5

I wanted to explicit instantiate a template, when I ran into the fact that I need to use a elaborated type specifier.

At cppreference it is state that:

If the (class-)name refers to a typedef name, a type alias, a template type parameter, or an alias template specialization, the program is ill-formed,...

I would like to have a bit of an understanding why it is this way. (with special interest in the typedef part)

edit:

using MyType = MyTemplate<Many,Parameters>;
template class MyType; // invalid code

From here I learned that this restriction only holds for MyType and that it is okay for Many and Parameters to be typedefs.

  • 1
    "when I ran into the fact that I need to use a elaborated type specifier." can you provide a [mre]? – L. F. Nov 07 '19 at 10:43
  • Are you trying to forward declare `MyType`? You are out of luck. The best you can do is forward declare the components, and put the alias where you want the forward declaration. – Caleth Nov 07 '19 at 11:39
  • Note that `MyType` *is not* a template (it is probably a class) – Caleth Nov 07 '19 at 11:40
  • 3
    C++ name lookup is very painful to implement correctly, this rule gives compiler writers a break. – Hans Passant Nov 07 '19 at 11:56

1 Answers1

0

Standard says:

7.1.6.3 Elaborated type specifiers [dcl.type.elab]: 2) ... 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.

So the usage of typedefs in elaborated type specifiers is explicitly prohibited by the standard.

At least typedef is not required to be an alias to a class, union or enum type, but it can also be an alias for a primitive type, and there are no elaborated type specifiers for such case, and it would not have much sense.

Yuri Kovalenko
  • 1,325
  • 9
  • 19