When should I use the keyword “typename” when using templates

后端 未结 4 1948
無奈伤痛
無奈伤痛 2020-12-11 23:10

I\'ve been working lately on a small project, and I couldn\'t figure out something..

I\'ve been given a .h file that was containing a class, using a typename templat

4条回答
  •  眼角桃花
    2020-12-11 23:37

    For Function1, you need to tell the compiler what Node is -- in this case, it's a nested type inside Something. Because it's dependent on T (it's a dependent name), you need to tell the compiler it's a type, so you must write it as typename Something::Node. The issue is that there might be some T for which Something::Node isn't actually a type (i.e. if you partially specialize Something).

    For Index, what you have is fine -- const T& is just a reference to a const T, and the compiler knows what T is.

提交回复
热议问题