【发布时间】:2016-03-02 16:26:54
【问题描述】:
对不起,如果标题听起来令人困惑,但问题是。我正在研究一个非常简约的Expression Templates 案例(请参阅下面的完整 C++98 代码),除了一个细节外,我什么都理解:为什么 Vector 类型的赋值运算符需要有一个外国或第三方模板 A去工作?请参阅下面的注释行(抱歉没有找到行号的方法)
#include <iostream>
using namespace std;
template <class A>
struct Expr {
inline operator const A&() const {
return *static_cast<const A*>(this);
}
};
// ----------------------------------------------
// Addition Expression
// ----------------------------------------------
template<class A, class B>
class DExprSum : public Expr< DExprSum< A,B > > {
const A& a_;
const B& b_;
public:
DExprSum(const A& a, const B& b) : a_(a), b_(b) { }
double operator()(int i) const {
return (double)(a_[i] + b_[i]);
}
double operator[](int i) const {
return this->operator()(i);
}
};
template <class A, class B>
inline DExprSum<A, B> operator+(const Expr<A>& a, const Expr<B>& b) {
return DExprSum<A, B>(a, b);
};
// ----------------------------------------------
// A simple vector class
// ----------------------------------------------
template<class T>
class Vector : public Expr<Vector<T> > {
private:
T *p;
int len;
public:
Vector(int length) {
len = length;
p = new T[length];
};
T operator()(int i) const {
return p[i];
}
T& operator[](int i) const {
return p[i];
}
// <<<<<<------------ HERE why do I need a new template<class A>
// rather than simply using Expr<Vector<T> >
template<class A>
void operator=(const Expr<A>& expr) {
const A& a(expr);
// parallelize using OpenMP
#pragma omp parallel for schedule(runtime) // OMP_SCHEDULE=static,50 OMP_NUM_THREADS=10
for (int i=0; i < len; ++i) {
p[i] = a(i);
}
}
~Vector() {
delete[] p;
};
};
int main() {
Vector<double> a(3);
Vector<double> b(3);
Vector<double> c(3);
a[0] = 1;
a[1] = 2;
a[2] = 3;
b[0] = 2;
b[1] = 3;
b[2] = 4;
c = a + a + b + b;
for (int i = 0; i < 3; ++i) {
cout << c[i] << endl;
}
return 0;
}
如果我将赋值运算符定义更改为(这是它的实际意图):
void operator=(const Expr<Vector<T> >& expr) {
const Vector<T>& a(expr);
// parallelize using OpenMP
#pragma omp parallel for schedule(runtime) // OMP_SCHEDULE=static,50 OMP_NUM_THREADS=10
for (int i=0; i < len; ++i) {
p[i] = a(i);
}
}
我收到编译器错误error: no viable overloaded '='
【问题讨论】: