Elbert Mai's generic callback/delegate, C++11 with no other dependencies except <utility>
. No exceptions/rtti/heap usage, and just about 100 lines of single header template code.
This is effectively a raw function pointer wrapped in a typesafe template and has no runtime code size or processing overhead with any of the compilers.
Implementation patterned after version from this smart function pointer article and https://github.com/reiver-dev/cppcallback
Use primarily through CREATE_DELEGATE macro:
struct foo {
bool bar(int param) { }
};
bool baz(int param) { }
foo foo_instance;
auto delegate = CREATE_DELEGATE(&foo::bar, &foo_instance);
delegate(42);
delegate = CREATE_DELEGATE(&baz);
delegate(90210);
Delegate<bool(int)> call_me_maybe = delegate;
call_me_maybe(31337);
Built with maximum warnings on gcc, clang, msvc, full test coverage