Officially, what is typename for?

后端 未结 8 1650
野的像风
野的像风 2020-11-22 15:07

On occasion I\'ve seen some really indecipherable error messages spit out by gcc when using templates... Specifically, I\'ve had problems where seemingly correc

8条回答
  •  天涯浪人
    2020-11-22 15:25

    Consider the code

    template somefunction( T * arg )
    {
        T::sometype x; // broken
        .
        .
    

    Unfortunately, the compiler is not required to be psychic, and doesn't know whether T::sometype will end up referring to a type name or a static member of T. So, one uses typename to tell it:

    template somefunction( T * arg )
    {
        typename T::sometype x; // works!
        .
        .
    

提交回复
热议问题