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::function
s as theboost::bind
return 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());
}