如果我有一個像這樣的類:(Vector<T>一個模板類),現在我想專門化它:Vector<int>. 我如何繼承Vector<T>?
我的代碼是:
template<typename T> class Vector&<int>:public Vector <T>
但它給出了錯誤:
模板引數在偏特化中不可推匯出。
我應該如何處理?
我實際上并不是說我想在 Vector 中使用。但我想了解語言方面有什么問題?
這是否意味著規范類不能從其他模板類繼承?
uj5u.com熱心網友回復:
看起來您希望Vector<int>special 從 general 繼承Vector<int>。這是不可能的,因為只能有一個Vector<int>,但有一個小技巧你可以做到。只需添加另一個模板引數,一個具有默認值的布爾非型別。現在對于每一種T,都有兩種不同的Vector<T, b>,一種可能的b。
template <class T, bool = true>
class Vector
{ void whatever(); };
普通用戶只需使用Vector<float>或Vector<int>不顯式傳遞第二個引數。false但是如果需要,專業可以通過。
template<>
class Vector<int, true> : public Vector<int, false>
{ int additional_member; };
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/518060.html
標籤:C 班级模板遗产派生类
