Violet Giraffe
Violet Giraffe

Reputation: 33579

Is it allowed to typedef a class template type argument into the same name?

This seems to compile and even work as expected in MSVC. But is it legal C++ code and is it guaranteed to do what is expected here (that is, export the template type to the struct's users under the same name)?

template <typename EnumType>
struct Enum
{
   // There are two hard problems in CS: cache invalidation and naming things.
   typedef EnumType EnumType;
};

Upvotes: 4

Views: 448

Answers (1)

user2249683
user2249683

Reputation:

I think the type definition is not allowed.

14.6.1 Locally declared names (N4296)

6 A template-parameter shall not be redeclared within its scope (including nested scopes).A template-parameter shall not have the same name as the template name. [ Example:

 
template<class T, int i> class Y { 
   int T;  // error: template-parameter redeclared 
   void f() { 
       char T; // error: template-parameter redeclared 
   }
}; 

template<class X> class X;  // error: template-parameter redeclared

— end example ]

The typedef EnumType EnumType is a redefinition of the template-parameter as typedef-name.

Upvotes: 2

Related Questions