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

后端 未结 3 822
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:32

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

提交回复
热议问题