[英]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.我正在用向量的向量构建一个 c++11 矩阵。 I already implemented the vectors (can't use std::vector
) and I want the matrix to inherit from myVector<myVector<T>>
.我已经实现了向量(不能使用std::vector
)并且我希望矩阵从myVector<myVector<T>>
继承。 myVector<T>
works fine, but myVector<myVector<T>>
has some functions I want to override (to delete them). myVector<T>
工作正常,但myVector<myVector<T>>
有一些我想覆盖的功能(删除它们)。 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).我只想从myVector<myVector<T>>
专业化中删除operator*
函数(这样myVector<int>
实例将能够使用函数,但myVector<myVector<int>>
实例将不能)。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.