Reputation: 3652
I read over the boost::function
function wrappers, and the examples cited in the tutorial section (http://www.boost.org/doc/libs/1_55_0/doc/html/function/tutorial.html). I am trying to understand the use cases of function wrappers, as opposed to just using function pointers. I am not necessarily looking for code samples, but more of cases where functions wrappers are way more appropriate to use than function pointers.
Thank you, Ahmed.
Upvotes: 1
Views: 256
Reputation: 956
A function wrapper wraps any callable entity, this includes function pointers as well as function objects and lambda functions.
A function object can be any class that overloads the operator()
.
Function objects are also the result of calls such as boost::bind
or std::bind
.
The use of a function wrapper then would be to allow any type of callable object to be used instead of just a function pointer.
Function objects are mostly used to bind values to the object such as maintaining an internal counter to determine how many times the function object was called (tricky to do with just function pointers requiring static variables in the scope of the function pointer), or binding a class instance to a member function for ease of calling later.
By having your class constructor or function take as a parameter a boost::function
(or std::function
) function wrapper instead of a function pointer, you allow users of your class or function to be able to decide whether they would prefer to pass you a function pointer or a function object or a lambda, and you don't have to care what choice they make.
Upvotes: 2