2
#include <string>

template<typename>
void g()
{}

template<typename T>
void f()
{
    g<typename T::value_type>(); // ok
    g<T::value_type>(); // ok
    typename T::value_type n1; // ok
    T::value_type n2; // failure        
}

int main()
{
    f<std::string>();
}

Compiled with vc++.

In the code above, whether T::value_type is a type or a variable can be determined at compile-time. There is no ambiguity here, or g<T::value_type>() will also be required written as g<typename T::value_type>().

Why is typename required in such a case?

What's the rationale behind?

xmllmx
  • 39,765
  • 26
  • 162
  • 323

0 Answers0