If the function pointer embedded in a boost::bind return object is NULL/nullptr/0, I need to take action other than calling it. How can I determine if the object contains a null function pointer?
Addenda
- I don’t believe I can use and compare
boost::functions as theboost::bindreturn object is used with varying call signatures in a template function. - Simplified example:
template <typename BRO> Retval do_stuff(BRO func, enum Fallback fallback) { if (func == NULL) { return do_fallback(fallback); } else { return use_retval(func()); } } do_stuff(boost::bind(FuncPtrThatMightBeNull, var1, var2), fallback);
Solution
Since the arity of the function in the callee does not change, I can “cast” the bind return object into a boost::function and call .empty()
Retval do_stuff(boost::function<Retval()> func, enum Fallback fallback)
{
if (func.empty())
return do_fallback(fallback);
else
return use_retval(func());
}
You can either bind to a dummy function:
… or, assuming you’re using
Boost.Bindtogether withBoost.Function, return a default constructed function object and check forempty()before calling it:Regarding the update:
I still don’t see what the problem with binding to a different function like the following would be:
If you’d want to move that handling out of the calling code, you could emulate variadic template function to support variable arities:
… or you use the approach above to generate
boost::function<>objects or your own wrappers and check forfunctor.empty()or similar indo_stuff().