调用此方法的正确方法是什么?

时间:2019-10-30 15:08:56

标签: c++11 bind placeholder

我正试图了解placeholders,但无法调用此方法。我有一个隐式参数Dummy{}floatLastDummy{}。当我调用函数时,我会跳过这些参数。另外我的电话打不通。

struct AnotherDummy {};
struct YetAnotherDummy {};
struct LastDummy {};

class X { public:
    void foo(std::string one, Dummy two, int three, AnotherDummy four, 
             float five, YetAnotherDummy six, double seven, int eight, LastDummy nine)
    {

    }
};

int main()
{
    using namespace std::placeholders;

    auto functor = std::bind(&X::foo, _6, _3, Dummy{}, _5, _1, 5.0f, _4, _2, _7, LastDummy{});

    X obj;
    functor(&obj, YetAnotherDummy{}, 1, 2.3f, 'x', AnotherDummy{}, Dummy{}, 2.3);

    return 0;
}

1 个答案:

答案 0 :(得分:0)

对于非静态成员函数,将在其上调用该函数的对象(成员函数内部成为this指针的对象)作为隐藏的第一个参数传递。

所以使用functor的主要问题是X对象obj应该是函数的 first 参数,即占位符{{ 1}}。其余参数从占位符_1开始。

所以您需要做例如

_2

请注意,auto functor = std::bind(&X::foo, _1, _7, _4, Dummy{}, _6, _2, 5.0f, _5, _3, _8, LastDummy{}); 不是该函数的第一个参数,并且其余占位符已增加_1

相关问题