Passing function pointer in a variable
We are continuing with the previous example, and now we want to pass a pointer to a function in our process_integers()
method. Shall we add an overload for just function pointers, or is there a more elegant way?
Getting ready
This recipe is continuing the previous one. You must read the previous recipe first.
How to do it...
Nothing needs to be done as boost::function<>
is also constructible from the function pointers:
void my_ints_function(int i); int main() { process_integers(&my_ints_function); }
How it works...
A pointer to my_ints_function
will be stored inside the boost::function
class, and calls to boost::function
will be forwarded to the stored pointer.
There's more...
The Boost.Function
library provides a good performance for pointers to functions, and it will not allocate memory on heap. Standard library std::function
is also effective in storing function pointers. Since Boost 1.58, the Boost.Function
library can store functions and...