【问题标题】:std::bind on a multi parameter function fails at compile time resolution in function template多参数函数上的 std::bind 在函数模板中的编译时解析失败
【发布时间】:2020-04-29 19:33:40
【问题描述】:

我有一个模板函数:

template <class F>
        Real solveImpl(const F& f,
                       Real xAccuracy)

我需要传递一个函子 f,它接受一个参数 x 并返回一个双精度值,基本上是一个 std::function&lt;Real(Real)&gt;。我有以下成员函数:

Real UnderlyingDefaultTimeSimulation::cumulativeHazardFunction(int
            scenarioIdx, Real t1, Real t2, Real target)

除了t2之外的所有参数都将被压制,所以我继续使用经典

auto f = std::bind(&UnderlyingDefaultTimeSimulation::cumulativeHazardFunction, j, t1, std::placeholders::_1, exponential[i]);

认为新创建的函数 f 将有效地成为一个只接受单个参数 t2 的可调用函数。当我写 solve(f, accuracy) 时,编译器在尝试所有可能的匹配项并显示错误消息后在编译时解析失败:

没有匹配函数调用对象 std::_1::bind(............)

关于如何打开我的成员函数的任何其他方式:

Real UnderlyingDefaultTimeSimulation::cumulativeHazardFunction(int
                scenarioIdx, Real t1, Real t2, Real target)

在钳制除t2 之外的所有参数后转换为std::function&lt;Real(Real)&gt;

谢谢, 一个

【问题讨论】:

    标签: c++


    【解决方案1】:

    您还需要为*this 绑定一些东西,否则,没有什么可以调用成员函数。如果您已经在成员函数中,您可能想要当前对象:

    auto f = std::bind(&UnderlyingDefaultTimeSimulation::cumulativeHazardFunction, *this, j, t1, std::placeholders::_1, exponential[i]);
    //                                                                             ^~~~~
    

    你也可以使用 lambda:

    auto f = [=](const Real& t2) {
        return /* this-> */ cumulativeHazardFunction(j, t1, t2, exponential[i]);
    };
    

    【讨论】:

    • 谢谢!会试试看。
    猜你喜欢
    • 1970-01-01
    • 2019-01-18
    • 2019-04-30
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 2013-03-10
    • 2017-10-01
    相关资源
    最近更新 更多