我想专门设计一个模板类,使其对 Base 类型的指针和所有其他指针类型的行为有所不同。我尝试使用 enable if。但它没有按照我想要的方式工作。谁能告诉我该怎么做。我试过的代码:
class Base
{
};
class Derived:public Base
{
};
class Non_base
{
};
template<class T,class Enable=void> class Vector
{
public:
Vector()
{
cout<<"Constructor of Vector "<<endl;
}
};
template<class T> class Vector<T*>
{
public:
Vector()
{
cout<<"Constructor of Vector<T *> "<<endl;
}
};
template<> class Vector<Base*>
{
public:
Vector()
{
cout<<"Constructor of Vector<Base*> fully specialized"<<endl;
}
};
//template<class T> class Vector<T *>:public Vector<Base *>
//{
//public:
// Vector()
// {
// cout<<"Constructor of Vector<Base*> partially specilized"<<endl;
// }
//};
template<class T> class Vector<T*,typename enable_if<is_base_of<Base,T>::value>::type>
{
Vector()
{
cout<<"Constructor of Vector<Base*> partially specilized"<<endl;
}
};