【发布时间】:2017-11-01 03:12:00
【问题描述】:
我有一个稍微复杂的用例,将成员函数指针传递给外部函数,然后由成员函数再次调用(不要问!)。我正在学习 std::function 和 std::mem_fn 但我似乎无法转换我的老派函数指针
void (T::*func)(int) 到 std::function<void (T::*)(int) func>
在下面的代码中,我希望能够在来自 anotherMember 的调用中将 std::function 传递给 memFuncTaker
#include "class2.hpp"
#include <iostream>
class outer{
public:
void aMember(int a){
std::cout << a <<std::endl;
}
void anotherMember(double){
memFuncTaker(this, &outer::aMember);
}
};
template<class T>
void memFuncTaker(T* obj , void (T::*func)(int) ){
(obj->*func)(7);
}
【问题讨论】:
-
我没有看到任何尝试在您的代码中使用
std::function。 -
你的问题标题是“Converting pointer to member function to std::function”,但问题听起来像“Converting std::function to pointer to member function”。
标签: c++ c++11 function-pointers member-function-pointers std-function