我們想專用基類的成員函數。但是,它不會編譯。有沒有人知道任何可以編譯的選擇?繼承的成員函數的模板特化
下面是一個例子
struct Base
{
template<typename T>
void Foo()
{
throw "Foo() is not defined for this type";
}
};
struct Derived : public Base
{
template<>
void Foo<int>() { cout << "Foo<int>()" << endl; } // compile error (cannot specialize members from a base class)
template<>
void Foo<double>() { cout << "Foo<double>()" << endl; } // compile error (cannot specialize members from a base class)
};
不會有多態行爲到基地,你不能使模板功能虛擬。 不知道你想達到什麼。 – CashCow
我們試圖創建具有特定成員的工廠類,並且當我們不知道如何處理該方法未專用的類型時,我們想拋出一個異常。 我們希望在我們的例子中創建許多像Derived一樣的類。否則,我們不會有基類。 – Alex
在編譯時失敗比在運行時拋出異常更好。 – CashCow