Search code examples
c++usingtypename

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?


Solution

  • 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.)