2

Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?

I'm reading this so I'm now looking at the implementation of std::forward and std::move:

    // TEMPLATE FUNCTION forward
template<class _Ty> inline
    _Ty&& forward(typename identity<_Ty>::type& _Arg)
    {   // forward _Arg, given explicitly specified type parameter
    return ((_Ty&&)_Arg);
    }

    // TEMPLATE FUNCTION move
template<class _Ty> inline
    typename tr1::_Remove_reference<_Ty>::_Type&&
        move(_Ty&& _Arg)
    {   // forward _Arg as movable
    return ((typename tr1::_Remove_reference<_Ty>::_Type&&)_Arg);
    }

I think I get the whole story but I don't understand the use of the typename outside of the template definition (template<...>). What does it mean in this case?

Community
  • 1
  • 1
demorge
  • 1,097
  • 1
  • 7
  • 17

1 Answers1

7

It indicates that identity<_Ty>::type& is a type, not a static variable from the class.

Here's a simpler example:

template<class T> void f() { T::x * p; ... } 

You can think that p is a pointer to something of a class T::x. Or you can think that T::x is a static variable that gets multiplied by p. So, in this case the compiler can't really know what did you mean by this code.

To avoid such ambiguousness, you should specify that T::x is a type by adding typename:

template<class T> void f() { typename T::x * p; ... } 

It's kind of the same thing in your case.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105