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

后端 未结 3 713
心在旅途
心在旅途 2021-02-14 14:48

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:29

    In general, C++'s syntax (inherited from C) has a technical defect: the parser MUST know whether something names a type, or not, otherwise it just can't solve certain ambiguities (e.g., is X * Y a multiplication, or the declaration of a pointer Y to objects of type X? it all depends on whether X names a type...!-). The typename "adjective" lets you make that perfectly clear and explicit when needed (which, as another answer mentions, is typical when template parameters are involved;-).

提交回复
热议问题