我在重用名为evaluate
的函数时遇到麻烦
template<typename T>
template<typename T2>
T2 Polynomial<T>::evaluate(T2 val) const {
int degree = 0;
return accumulate(coefs.begin(), coefs.end(), T2{0},
[&](T2 res, T coef){ return res+coef*pow(val,degree++);} );
}
我正试图在名为product
的函数中调用它
template<typename T>
template<typename T2>
T2 Polynomial<T>::product(T2 val, vector<Polynomial<T>>& polys) {
vector<future<T2>> futures;
T2 product = this->evaluate(val);
// evaluate asynchronously each polynomial with val
for (Polynomial<T>& p : polys) {
auto evaluateFunction = [&]() {
int degree = 0;
return accumulate(p.coefs.begin(), p.coefs.end(), T2{0},
//change [&] to [&val, °ree]
[&](T2 res, T coef) { return res + coef * pow(val, degree++); });
};
futures.push_back(async(launch::async, this->evaluate(val))); //normally there was a evaluateFunction there
}
// compute the final product
for (auto& fut: futures) {
product *= fut.get();
}
return product;
}
重用“求值”功能会给出错误,提示“无匹配功能” 在异步中(模板中未解析的类型),所以我不得不重新制作它。
如何解决此错误?
我不了解futures.push_back(async(launch::async, this->evaluate(val)));
答案 0 :(得分:3)
this->evaluate(val)
将被评估为this->evaluate(val)
的结果并重新放置
并且应在val
而不是p
上调用评估
你应该做
std::bind(&Polynomial<T>::evaluate, p, val)
或带有lambda
[&]{ return p.evaluate(val); }
这将从Polynomial<T>::evaluate
创建一个带有p
和val
作为参数的可调用对象