1
我想專門研究一個模板類,使它對Base類型的指針和所有其他指針類型的行爲有所不同。我嘗試使用啓用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;
}
};
當我試圖爲模板函數做同樣的事情時,我得到一個錯誤:重新聲明模板類,這似乎是正確的。我宣佈了一個函數來接受指針,並且在一個函數中我檢查了它是否是Base類型,而在另一箇中我檢查了它是否是基類型。但是這兩個函數都有相同的論點。如何避免它? – sajas 2013-02-11 09:45:27
嗯,不知道你在描述什麼。也許再開一個問題。 – Potatoswatter 2013-02-12 03:05:24