异步中没有匹配函数(模板中未解析的类型)

时间:2018-08-02 13:20:40

标签: c++ asynchronous future

我在重用名为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, &degree]
                              [&](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)));

有什么问题

1 个答案:

答案 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创建一个带有pval作为参数的可调用对象