dengzikun
dengzikun

Reputation: 33

c++ typename in vc++ and clang

The following code:

template<typename T>
struct A{
    using TT = typename T;

};

vc++ is ok. clang outputs: error: expected a qualified name after 'typename' using TT = typename T; .

If you change

using TT = typename T; 

to

using TT = T; 

,vc++ and clang are ok.

why?

Upvotes: 3

Views: 839

Answers (2)

aschepler
aschepler

Reputation: 72271

Your code is incorrect C++. The C++ grammar specifies that the keyword typename (besides its other use to declare a template parameter) may only be used at the beginning of a qualified-id, which is a name that includes at least one :: token.

Apparently MSVC allows a more generous syntax here.

(typename must be used in most contexts within a template whenever the qualified-id is a dependent name that is supposed to be a type, not a variable or a template. Here "dependent name" roughly means that the compiler can't be sure of finding a declaration for it, due to depending on template parameters. But it's also valid to use the keyword on a qualified-id that is not dependent, or not in a template at all.)

Upvotes: 1

user9511748
user9511748

Reputation:

The compiler already knows that T is a type name because you declared it in class template, so you should using TT = T instead of using TT = typename T. VC++ does not follow all ISO standard, so some code that failed to compile in gcc or clang may work in VC++

Upvotes: 1

Related Questions