Search code examples
c++functionc++11argumentsdefault-value

Default value of function as a function argument


How do I properly define a default return value of a function that is an argument of another function?

Let's say I have a function like this:

bool x( ... , std::function<bool( ... )> func ) { ... ; return func( ... ); }

I would like x to return true if it's called without the last argument.


Solution

  • You can specify a lambda as the default value of func, e.g.

    bool x( ... , std::function<bool( ... )> func = []( ... ) { return true; } ) { ... ; return func( ... ); }