Why do you sometimes need to write `typename T` instead of just `T`?

后端 未结 3 834
Happy的楠姐
Happy的楠姐 2021-02-14 15:20

I was reading the Wikipedia article on SFINAE and encountered following code sample:

struct Test 
{
    typedef int Type;
};

template < typename T > 
void         


        
3条回答
  •  醉酒成梦
    2021-02-14 15:38

    Basically, you need the typename keyword when you are writing template code (i.e. you are in a function template or class template) and you are referring to an indentifier that depends on a template parameter that might not be known to be a type, but must be interpreted as a type in your template code.

    In your example, you use typename T::Type at definition #1 because T::Type depends on the template parameter T and might otherwise be a data member.

    You don't need typename T at definition #2 as T is declared to be a type as part of the template definition.

提交回复
热议问题