void func(const std::function<void()>& f = empty) { if(f) f(); }
what is the 'empty' should be? I use [](){}
. But technically, that is not empty, the f() will execute.
So, why do std::function instances have a default constructor? Without default constructor you wouldn't have to add checks, just like for other, normal arguments of a function. And in those 'rare' cases where you want to pass an 'optional' std::function, you can still pass a pointer to it (or use boost::optional).
The stored callable object is called the target of std::function . If a std::function contains no target, it is called empty.
A default argument is a value in the function declaration automatically assigned by the compiler if the calling function does not pass any value to that argument. The values passed in the default arguments are not constant.
void func(const std::function<void()>& f = {}) { if(f) f(); }
LIVE DEMO
const std::function<void()>& f = nullptr
or
const std::function<void()>& f = std::function<void()>()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With