例如,这是我的成员函数 ( 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
?