簡體   English   中英

如何專業化模板的模板功能?

[英]How to specialize functions of template of template?

如何覆蓋模板的模板專業化功能?

我正在用向量的向量構建一個 c++11 矩陣。 我已經實現了向量(不能使用std::vector )並且我希望矩陣從myVector<myVector<T>>繼承。 myVector<T>工作正常,但myVector<myVector<T>>有一些我想覆蓋的功能(刪除它們)。 我怎樣才能做到這一點?

代碼:

template <typename T> class myMatrix : public myVector<myVector<T>>{
 ...
 //scalar multiplication
 myVector<T> MyVector<T>::operator*(const T& scalar) const{...}
}

我只想從myVector<myVector<T>>專業化中刪除operator*函數(這樣myVector<int>實例將能夠使用函數,但myVector<myVector<int>>實例將不能)。

也許使用刪除?

class A
{
    public:
    A(){;};
    int some_function();
};

class B: public A
{
    public:
    B():A(){;};
    int some_function() = delete;
};

此解決方案並非完全通用,但適用於特定數據類型:

template <class T>
class Vector {

public:

    virtual void DoSomething () {
        printf("general Vector template");
    }

};

template<>
class Vector <Vector<int>> {
public:

 void DoSomething () = delete;


};

int main(int argc, const char * argv[]) {

    Vector<int> iv;
    iv.DoSomething();  // OK;

    Vector<Vector<int>> viv;
    viv.DoSomething(); // ERROR: attempt to use a deleted function;

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM