I have this problem where a C++ function pointer is saved, together with a set of arguments to call it on, such that it can be called later. The calling code is unaware of the type and arguments of the functions. Saving and calling function pointers and arguments is extremely performance critical in my application. The interface should look something like this:
void func( int a, char * b );
call_this_later( func, 5, "abc" );
A straightforward solution is to put all of this information in a functor, requiring a different typedef per called function. C++11 would allow me to do that using a variadic template, so that is ok.
Since the type of the functor is not known at the point of call, it seems necessary to create a virtual base class for these functors and to invoke the functors using virtual function calls. The performance overhead of virtual function calls + heap allocation is too high (I am pushing the boundaries of implementing this idiom with as few assembly instructions as possible). So I need a different solution.
Any ideas?
You could do this with a lambda expression which captures the types.
template <typename Func, typename Arg1, typename Arg2>
std::function<void(void)> call_this_later(Func f, Arg1 a1, Arg2 a2) {
return [=]() { f(a1, a2); }; // Capture arguments by value
}
A few things to note:
const char*
then it's OK if you use it with a string literal but not if you pass someStdString.c_str()
and the string is going to die any-time soon.What about a solution using boost::function / boost::bind:
void func( int a, char * b );
boost::function<void ()> my_proc =
boost::bind(&func, 5, "abc");
// then later...
my_proc(); // calls func(5, "abc");
You're going to need a custom allocator to match your allocation pattern. Alternatively, use compile-time polymorphism to pass the type of the functor in to the call site.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With