我试图理解placeholders
,但是我不能调用此方法。我有一个隐式参数Dummy{}
,float
和LastDummy{}
。当我调用函数时,我会跳过这些参数。我的电话也无法使用。
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;
}
对于非静态成员函数,将在其上调用函数的对象(在成员函数内成为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
。