简体   繁体   中英

How to specialize functions of template of template?

How to override functions of specialization of template of template?

I'm building a c++11 matrix out of vectors of vectors. I already implemented the vectors (can't use std::vector ) and I want the matrix to inherit from myVector<myVector<T>> . myVector<T> works fine, but myVector<myVector<T>> has some functions I want to override (to delete them). How can I do that?

code:

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

I want to delete operator* function only from myVector<myVector<T>> specialization (so that myVector<int> instances will be able to use function, but myVector<myVector<int>> instances will not).

Maybe using delete?

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

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

This solution isn't completely generic but works for a specific data type:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM