3 ms·
The clean approach is to wrap functions into classes: class Wrap { void call() {...} }; template <typename F> { ... F::call(); } And with some imagination on
by ambrop7 12y ago
The clean approach is to wrap functions into classes:
class Wrap { void call() {...} };
template <typename F> { ... F::call(); }
And with some imagination one can write a macro to do this wrapping given an existing function, used like this:
void my_func();
using MyFuncWrapped = WRAP_FUNC(my_func);
// Now MyFuncWrapped::call() calls my_func.
My implementation of the wrapper: https://github.com/ambrop72/aprinter/blob/master/aprinter/meta/WrapFunction.h https://github.com/ambrop72/aprinter/blob/master/aprinter/me... . But note it doesn't use std::forward since this code assumes no references being used.
- cbsmith 12y agoBut you don't get much by wrapping it in that class and making a functor out of it, unless you want to manage state. Either way, in C++, it is a slim line between the two. C++11 lambdas have this quality of "a type for every function" as well.