C++11 调用此方法的正确方法是什么?
我试图理解占位符,但是我不能调用这个方法。我有一个隐式参数C++11 调用此方法的正确方法是什么?,c++11,bind,placeholder,C++11,Bind,Placeholder,我试图理解占位符,但是我不能调用这个方法。我有一个隐式参数Dummy{},float和LastDummy{}。当我调用函数时,我跳过这些参数。我的电话也打不通 struct AnotherDummy {}; struct YetAnotherDummy {}; struct LastDummy {}; class X { public: void foo(std::string one, Dummy two, int three, AnotherDummy four,
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
应该是函数的第一个参数,即占位符\u 1
。其余参数从占位符\u 2
开始
所以你需要做些什么
auto functor = std::bind(&X::foo, _1, _7, _4, Dummy{}, _6, _2, 5.0f, _5, _3, _8, LastDummy{});
请注意,
\u 1
不是函数的第一个参数,剩余的占位符已经增加了1
好了,但是现在谁是2?@IonutAlexandru\u 2
是第一个正式参数(std::string one
),但是关于_4呢?我有一个隐式参数float和_4+1=_5,它也是float。@IonutAlexandrufloat five
变成\u 6
等等。在functor
参数列表中,它是第四个参数(因为它是第四个位置保持器)。