#include #include using namespace std; using namespace std::placeholders; // there are a few way of declaring "callable objects" // as a non-static method class A { public: int f0(int i) { return v+i; } int v; }; // as a function: int f1(A *a, int i) { return a->v+i; } // as a functor class F2 { public: int operator()(A *a, int i) const { return a->v + i; } }; F2 f2; // as a lambda expression auto f3 = [](A *a, int i) { return a->v + i; }; int main(int arg, char **argv) { // std::function (from #include ) can hold all of them... std::function f = nullptr; if (!f) cout << "not yet initialized (1)" << endl; f = f1; if (!f) cout << "not yet initialized (2)" << endl; f = f2; f = f3; f = std::bind(&A::f0,_1,_2); // have to use bind to get a non-static // member method // cannot compare f to anything except to check if it is nullptr // std::bind can be used on all these things }