You can use std::function , but if that is not efficient enough, you can write a functor object similar to what lambdas do behind the scenes:
auto compare = [] (int i1, int i2) { return i1*2 > i2; }
almost coincides with
struct Functor { bool operator()(int i1, int i2) const { return i1*2 > i2; } }; Functor compare;
If a functor needs to grab some variable in a context (for example, the "this" pointer), you need to add members inside the functor and initialize them in the constructor:
auto foo = [this] (int i) { return this->bar(i); }
almost coincides with
struct Functor { Object *that; Functor(Object *that) : that(that) {} void operator()(int i) const { return that->bar(i); } }; Functor foo(this);
source share