Home:ALL Converter>Is it possible to define a lambda in C++ with default generic argument?

Is it possible to define a lambda in C++ with default generic argument?

Ask Time:2018-06-08T22:03:45         Author:Paolo Crosetto

Json Formatter

Is it possible to define a lambda in C++ with default generic argument?

int main(){
    auto lambda = [](auto i = 0){return i;};
    std::cout<<lambda()<<"\n"; // this does not compile
    std::cout<<lambda(4)<<"\n"; // this does compile
    auto lambda2 = [](int i = 0){return i;};
    std::cout<<lambda2()<<"\n"; // this is also OK
}

I wonder if it's possible to reproduce something like this functor, and why not

struct Lambda{
    template<typename T=int>
    auto operator()(T i=1){ return i;}
};

Author:Paolo Crosetto,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762415/is-it-possible-to-define-a-lambda-in-c-with-default-generic-argument
yy