I would like to have a default lambda for a functor argument in my function.
I am aware it is possible using a struct
and operator()
like this:
struct AddOne {
int operator()(int a) {
return a+1;
}
};
template <typename Functor = AddOne>
int run_old(int x, Functor func = AddOne())
{
return func(x);
}
But I was wondering if there was a modern way, given the changes in the standard in either c++14/17/20, to make this work?
template <typename Functor>
int run_new(int x, Functor func = [](int a){ return a+1; })
{
return func(x);
}
I'm not sure what one would use as the default type to Functor, or if there is syntax i'm unaware of.