#pragma once #include // Type-erased reference to a function object. template class FunctionRef; template class FunctionRef { public: template ::type, FunctionRef>::value, int>::type = 0 > FunctionRef(FunctionObject && a_FunctionObject) { data_ = &a_FunctionObject; call_function_ = &ObjectFunctionCaller; } Ret operator () (Args... args) { return call_function_(data_, std::forward(args)...); } private: template static Ret ObjectFunctionCaller(void * callable, Args... args) { // Convert opaque reference to the concrete type. using ObjectPtr = typename std::add_pointer::type; auto & Object = *static_cast(callable); // Forward the call down to the object. return Object(std::forward(args)...); } using CallFunction = Ret(*)(void *, Args...); void* data_; CallFunction call_function_; };