I’ve got a function that I want to take an optional boost::function argument as a callback for reporting an error condition. Is there some special value I can use a the default value to make it optional?
For example, with a regular function pointer I can do:
void my_func(int a, int b, t_func_ptr err_callback=NULL) {
if (error && (err_callback != NULL))
err_callback();
}
Can I do something similar with boost::function replacing the function pointer?
You can use
0(C++’s equivalent ofNULL) for the default value of a boost::function argument, which will result to an empty function object. You can test if it’s empty by calling itsempty()method, comparing it to 0, or simply using it in a boolean context:boost::functionobjects work pretty much like plain function pointers in this respect.