我可以使用lambda绑定(bind)私有(private)成员函数。我正在努力使用std::bind编写等效内容。这是我的尝试,但是无法编译。

#include <functional>

class A {
    private:
        double foo(double x, double y);
    public:
        A();
        std::function<double(double,double)> std_function;
 };

A::A() {
    // This works:
    //std_function = [this](double x, double y){return foo(x,y);};

    std_function = std::bind(&A::foo,this,std::placeholders::_1));
}

最佳答案

std_function应该带有2个参数,但是您只指定了一个。请注意,placeholders用于以后调用std_function时要绑定(bind)的参数。

更改为

std_function = std::bind(&A::foo, this, std::placeholders::_1, std::placeholders::_2);

关于c++ - 标准绑定(bind)等同于lambda,用于将成员函数绑定(bind)到标准函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48945958/

10-11 19:19
查看更多