如果想将类的非静态成员函数用std::function进行指示,需要将隐藏的类进行标识,
否则,会出现类似如下错误:
error: conversion from ‘void (CA::*)()’ to non-scalar type ‘std::function<void()>’ requested
#include <stdio.h>
#include <stdlib.h>
#include <functional>
class CA{
public:
CA() = default;
void func()
{
printf("CA::func\n");
}
};
int main()
{
// std::function<void ()> function_ = &CA::func; // error
std::function<void (CA*)> function_ = &CA::func;
CA a;
// function_(); // error
function_(&a);
return 0;
}
对于std::function和std::bind的区别,可参考: