Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++11 调用此方法的正确方法是什么?_C++11_Bind_Placeholder - Fatal编程技术网

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。@IonutAlexandru
float five
变成
\u 6
等等。在
functor
参数列表中,它是第四个参数(因为它是第四个位置保持器)。