20

例如,这是我的成员函数 ( do_it):

class oops
{
public:
    void do_it(GtkWidget *widget, GdkEvent *event, gpointer data)
    {
        g_print ("Hi there :)\n");
    }
};

...我用std::bind它使它看起来像一个非成员函数:

oops o;
std::function<void(GtkWidget*, GdkEvent*, gpointer)> f = std::bind(&oops::do_it, o);

但它不起作用,以下是编译器错误消息:

program.cc: In function ‘int main(int, char**)’:
program.cc:69:85: error: conversion from ‘std::_Bind_helper<false, void (oops::*)(_GtkWidget*, _GdkEvent*, void*), oops&>::type {aka std::_Bind<std::_Mem_fn<void (oops::*)(_GtkWidget*, _GdkEvent*, void*)>(oops)>}’ to non-scalar type ‘std::function<void(_GtkWidget*, _GdkEvent*, void*)>’ requested
   std::function<void(GtkWidget*, GdkEvent*, gpointer)> f = std::bind(&oops::do_it, o);
                                                                                     ^

我必须使用以下方法修复它std::placeholders

oops o;
std::function<void(GtkWidget*, GdkEvent*, gpointer)> f = std::bind(&oops::do_it, o, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);

为什么不指定就不起作用std::placeholders

4

2 回答 2

54

std::bind()旨在创建一个可调用实体,该实体表示对函数的(部分)调用。它将调用的一些参数绑定到生成的调用对象,并让其余参数在调用时指定:

void f(int,int,int);

int main()
{
    std::function<void()> f_call = std::bind( f , 1 , 2 , 3);

    f_call(); //Equivalent to f(1,2,3)
}

的第一个参数std::bind()是要调用的函数,其余的是调用的参数。

在此示例中,调用对象是在指定所有三个参数的情况下生成的,因此调用点没有参数。现在考虑一个部分定义的调用:

std::function<void(int,int,int)> f_call = std::bind( f );

这不会编译,因为该函数有三个参数,而您没有指定任何一个!这没有意义,对吧?如果你有一个带有三个参数的函数,你应该将三个参数传递给调用对象。

如果您需要指定必须在调用点指定某些参数,则必须使用占位符来表示该参数。例如:

using namespace std::placeholders;

std::function<void(int,int,int)> f_call = std::bind( f , _1 , _2 , _3 );

f_call( 1 , 2 , 3 ); //Same as f(1,2,3)

如您所见,我们使用占位符为函数调用指定了三个“空格”,即在调用点指定的三个参数。
请注意,占位符的编号指定调用点的参数编号。调用点的第一个参数由 标识_1,第二个由标识_2,依此类推。这可用于以不同方式指定参数,重新排序函数调用的参数等。例如:

std::function<void(int,int)> f_call = std::bind( f , _1 , 2 , _2 );

f_call( 1 , 3 ); //Equivalent to f( 1 , 2 , 3 );

std::function<void(int,int,int)> reordered_call = std::bind( f , _3 , _2 , _1 );

reordered_call( 3 , 2 , 1 ); //Same as f( 1 , 2 , 3 );

最后,std::bind()可用于将成员函数绑定到用于调用它的对象:

struct foo
{
    void f() const;
};

int main()
{
    foo myfoo;
    std::function<void()> f = std::bind( &foo::f , std::cref( myfoo ) );

    f(); //Tah dah!
}

成员函数可以被视为具有一个隐藏参数的函数,该参数是完成调用的对象。这就是对象被绑定为第一个参数的原因。

但是,正如上面的示例一样,如果您只知道绑定点的某些参数,并且需要稍后在调用点指定其他参数,则应该使用占位符

using namespace std::placeholders;

oops o;

std::function<GtkWidget*,GtkEvent*,gpointer> do_it = std::bind( &oops::do_it , std::ref( o ) , _1 , _2 , _3 );

do_it( /* first param */ , /*second param */ , /* third param */ ); //Call

一些细节

调用对象的签名

注意我们使用std::function来存储调用对象。该函数的签名取决于生成的调用对象的类型,即取决于您在绑定点指定参数的方式

调用对象只是另一个可调用实体,它充当对原始函数的调用。下面是我们的f()函数示例:

std::function<void()> f_call = std:bind( f , 1 , 2 , 3 );

这里调用对象的签名是void(),因为我们已经在绑定点指定了空洞的参数集,调用点就没有人要指定了(所以调用对象是没有参数的)。

在部分调用的情况下:

std::function<void(int,int,int)> f_call = std::bind( f, _1 , _2 , _3 );

f_call( 1 , 2 , 3 );

调用对象的签名是void(int,int,int),因为我们在调用点留下了三个要指定的参数(注意占位符)。通常,调用对象具有与您在绑定点指定的占位符相同数量的参数。.

于 2014-03-15T10:29:31.533 回答
0

请注意,占位符的编号指定调用点的参数编号。调用点的第一个参数由_1 标识,第二个由_2 标识,依此类推。

我想为 @ Manu343726 添加一些示例。如果您尝试将具有 3 个参数的函数转换为具有 2 个参数的函数。您可以在绑定时指定 1 个参数,并在调用时提供其余 2 个参数。

typedef std::function<void(int, int)> add2;
int add3(int x1, int x2, int x3) { return x1 + x2 + x3; }
auto fn = std::bind(add3, 11, std::placeholders::_1, std::placeholders::_2);
fn(22, 33)

现在 11 是 add3 的 x1,_1 是 add3 的 x2,_2 是 add3 的 x3。_2 表示fn在调用时有 2 个参数要指定。而且这种格式是错误的:

auto fn = std::bind(add3, 11, std::placeholders::_2, std::placeholders::_3);

于 2017-11-20T15:51:23.017 回答