Reputation: 1063
I have a simple template like
template <typename a, typename b, typename c>
class myclass
{
};
And I realize that I can specialize the template in both following ways:
template <typename a, typename b>
class myclass<a, b, int>
{
};
template <typename a, typename b>
class myclass<a, b, typename int>
{
};
Why there is no difference between "typename int" and "int" ?
Upvotes: 0
Views: 101
Reputation: 41770
There is, it's must be a visual studio bug. Because visual studio don't implement two phase name lookup and has no means of what is a dependent type or not, it allows you to compile it without errors. However, when compiled with gcc, you'll get the following error:
main.cpp:11:33: error: template argument 3 is invalid
class myclass<a, b, typename int>
^
But when using it with dependent names, typename
keyword must appear to tell the compiler it's not a value or other thing than a type. In fact, the typename
keyword must only appear when it's really needed.
However, there are context where the compiler already know that the expression must yield to a type, like inheritance. If you try to put typename
in front of a dependent name there, you'll get an error. Take a look a this code:
template <typename A>
struct MyType : typename std::decay<A>::type {};
This result in that error:
main.cpp:6:17: error: keyword 'typename' not allowed in this context (the base class is implicitly a type)
struct MyType : typename std::decay<A>::type {};
^
The compiler only need those keywords when there's is an ambiguous statement in a template. Otherwise, you can't put these keywords.
Upvotes: 1