模板 类型区分的小疑问
template<typename T>
class CompoundT { // primary template
public:
enum { IsPtrMemT = 0 };
typedef T BaseT;
typedef T BottomT;
typedef CompoundT<void> ClassT;
};
template<typename T, typename C>
class CompoundT <T C::*> { // partial specialization for pointer-to-members
public:
enum { IsPtrMemT = 1 };
typedef T BaseT;
typedef typename CompoundT<T>::BottomT BottomT;
typedef C ClassT;
};
template<typename T>
void HelperFunc(const T&)
{
cout << CompoundT<T>::IsPtrMemT << endl;
cout << typeid(CompoundT<T>::BaseT).name() << endl;
}
class TestClass
{
public:
int _value;
void Func()
{
}
};
int _tmain(int argc, _TCHAR* argv[])
{
HelperFunc(&TestClass::_value);
//HelperFunc(&TestClass::Func);
return 0;
}
在vs2008下,HelperFunc(&TestClass::Func);这句会挂掉,请问为什么?
去掉:cout << typeid(CompoundT<T>::BaseT).name() << endl; 就正常了
[解决办法]
CompoundT<T>::BaseT
改为
typename CompoundT<T>::BaseT
[解决办法]
VS2010编译通过,输出
1
int
[解决办法]
template<typename T>
void HelperFunc(const T&)
{
cout << CompoundT<T>::IsPtrMemT << endl;
cout << typeid(typename CompoundT<T>::BaseT).name()
<< endl;
}
这样是可以的,用 gcc 4.7.2 可以编译过。