这是我精简的程序,我试图使用函数变量在运行时修改类功能。所以 - 我声明使用std::function
模板和与兼容签名函数myFunc
成员变量m_func
:如何将成员函数赋值给使用std :: function定义的成员变量?
#include <functional>
#include <iostream>
struct T
{
T():m_func([](int){return true;}) {}
void assign() {m_func = &T::myFunc;} // <======== Problem is here
void call(int M) const {std::cout << m_func(M) << std::endl;}
private:
bool myFunc(int N) {return (N >= 4);}
using func = std::function<bool(int)>;
func m_func;
};
int main()
{
T t;
t.assign();
t.call(6);
}
但是,编译器(克++ 4.8.4与-std = C++ 11选项)给我一个错误长输出,说template argument deduction/substitution failed
和更多...
为什么我不能将myFunc
函数分配给m_func
变量?
@nwp - 我不会介意你是否关闭它...谢谢 – HEKTO