#include <functional>
void hello(int a)
{
std::cout<<a<<std::endl;
}
void call_when(int x,const std::function<void(int)> &f)
{
if(x == 0)
f(x);
}
call_when(0,hello);
需要头文件functional,在std::function作为参数使用时,必须加const,否则会报错:
error C2664: “call_when”: 不能将参数 2 从“void (__cdecl *)(int)”转换为“std::function<_Fty> &”
1> with
1> [
1> _Fty=void (int)
1> ]
加上const后,错误即消除,可顺利运行。