You can either bind to a dummy function:
void dummy() { }
... or, assuming you use Boost.Bind along with Boost.Function , return the constructed function object by default and check empty() before calling it:
typedef boost::function<void (void)> F; F create() { return F(); } void use() { F f = create(); if(f.empty()) { } }
Regarding the update:
I still donโt see that there is a problem with binding to another function, such as the following:
template <typename BRO> Retval do_stuff(BRO func) { return func(); } if(funcPtr) { do_stuff(boost::bind(&use_retval, boost::bind(funcPtr, a, b))); } else { do_stuff(boost::bind(&do_fallback, fallback)); }
If you want to move this processing from the calling code, you can emulate the variational function of the template to support arities variables:
template<class R, class T1> boost::function<R (T1)> bind_wrap(R (*fnPtr)(), T1& t1, Fallback fallback) { if(fnPtr) return boost::bind(&use_retval, boost::bind(funcPtr, t1)); else return boost::bind(&do_fallback, fallback); } template<class R, class T1, class T2> boost::function<R (T1, T2)> bind_wrap(R (*fnPtr)(T1, T2), T1& t1, T2& t2, Fallback fallback) { if(fnPtr) return boost::bind(&use_retval, boost::bind(funcPtr, t1, t2)); else return boost::bind(&do_fallback, fallback); } // ... etc. for all needed arities do_stuff(bind_wrap(funcPtr, var1, var2, fallback));
... or you use the approach described above to create boost::function<> objects or your own wrappers and check functor.empty() or similar in do_stuff() .