Function Template: typename declaration
On GCC, The following gives me an error: no type named 'x' in 'struct Type'
On VC++, It complains about p
being undeclared
struct Type
{
static int const x = 0;
};
template <class T> void Func()
{
typename T::x * p; // p to be pointer
}
int main()
{
Func<Type>();
}
T::x
becomes Type::x
, which is an int
, not a type.
You've told the compiler that T::x
names a type by using typename
. When Func<Type>
is instantiated, T::x
is not a type, so the compiler reports an error.
Since Type::x
is not a type, rather a value, so when you write typename
, you're telling the compiler to find a nested type with name x
inside Type
, but it couldn't. Hence the GCC says no type named 'x' in 'struct Type'
which is more helpful than the message generated by VC++.
In C++11, the using keyword can be used for type alias
struct Type
{
using x = static int const;
};
精彩评论