Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.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++ 将类成员函数转换为std::function的模板化函数_C++_C++11_Lambda_Function Pointers - Fatal编程技术网

C++ 将类成员函数转换为std::function的模板化函数

C++ 将类成员函数转换为std::function的模板化函数,c++,c++11,lambda,function-pointers,C++,C++11,Lambda,Function Pointers,我有一个方法,在我的代码中使用此签名多次调用: void foo (std::function<void(int)> func, int a) { func(a); } 此外,如果我有一个类实例,我可以将函数指针传递给我的实例,大致如下所示: class Baz { bar(int a) {...} callFoo() { foo(std::bind(&Baz::bar, this, _1); } } 然而,在我的应用程序中,我经常需要在我持有

我有一个方法,在我的代码中使用此签名多次调用:

void foo (std::function<void(int)> func, int a) {
  func(a);
}
此外,如果我有一个类实例,我可以将函数指针传递给我的实例,大致如下所示:

class Baz { 
  bar(int a) {...}
  callFoo() {
     foo(std::bind(&Baz::bar, this, _1);
  }
}
然而,在我的应用程序中,我经常需要在我持有指向对象的指针的位置调用foo,该对象有一个有效的成员函数,可以作为foo的参数。我真的不想给每个碰巧持有有效指针的类一个调用“foo”的方法。事实上,我更希望这些类甚至不知道foo

我想要一个如下所示的方法:

template<class T>
static void callFooSugar(T* ClassPointer, [What goes here?] ClassMethod);

有没有一种不用宏来编写callFooSugar的方法?我是用C++11编译的。

比如?foo[&MyBaz]inti{MyBaz.bari;},…;有什么问题@太啰嗦了,你想要点什么吗simpler@PiotrS.:这实际上是一个很好的解决方案。我想知道是否有办法去掉第二个参数中的&Baz?@ThomasKejser:不,您需要&Baz::来形成指向成员函数的指针
template<class T>
static void callFooSugar(T* ClassPointer, [What goes here?] ClassMethod);
Baz MyBaz;
callFooSugar(&MyBaz, bar);