我在重用名为 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;
}
重用'evaluate'函数给出错误'no matching function' 在异步中(模板中未解析的类型)所以我不得不重新制作它。
如何解决这个错误?
我不明白 futures.push_back(async(launch::async, this->evaluate(val)));
最佳答案
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
作为参数
关于c++ - 异步中没有匹配函数(模板中未解析的类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51654744/