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

问题描述 投票:0回答:1

我试图理解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;
}
c++11 bind placeholder
1个回答
0
投票

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

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

所以您需要做例如

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

请注意,_1不是该函数的第一个参数,并且其余占位符已增加1

© www.soinside.com 2019 - 2024. All rights reserved.