+ 3
Recursive lambda
Hi I read somewhere that recursive lambda is possible but when I try to implement, I am not getting how to solve error compilation provides... Below is code : https://code.sololearn.com/c3PQoq9y6LxT/?ref=app Also I don't understand need of recursive lambda as lambda is useful when we don't want to create function and simply call it while defining the same... correct?
7 ответов
+ 5
CarrieForle your approach works only because the function object is a global variable, which is accesible inside a lambda. If instead one wants to define a lamda inside a function (which is the common use case) , local variables have to be either explicitly captured, or passed as parameters like Ketan Lalcheta does.
Also saving to std::function is not really needed here. What is important is to define the return type.
Edit: sorry my previous statement about c++14 allowing the code without return type was wrong - a type declaration is still needed, or the code has to be rewritten to have a return before calling recursive function (like attached)
https://code.sololearn.com/c4l4kBgQNDke/?ref=app
+ 5
A very nice way to make a recursive lambda indeed Volodymyr Chelnokov. I also wanna tell you that the method I provide can also be used in main() and any other function. Also you mentioned the issue is auto deduction. That's why std::function is used in my method so it can fully specify the type of lambda rather than auto.
But your method is better than mine since you don't need additional std::function for this.
+ 4
First, why do you need display parameter which you never use it. You don't need a parameter of itself for recursive function.
You have to specify the function return type in lambda recursive because when calling the lambda in the lambda, the lambda inside of it have no idea what its type is since the lambda itself hasn't end and can't be deduced.
You will need a function object from <functional>
+ 4
For some reason (maybe someone more versed in c++ can explain) the compiler fails to notice that your function never returns anything, so its return type should be void, despite the fact that we don't really know the return type of the parameter.
So as CarrieForle told, you just need to specify the return type like this:
constexpr auto display = [] (const auto display, int intCnt) -> void {...} ;
Having a recursive lambda can be useful for example when parametrizing the graph algorithms. Unfortunately the way you can do it now makes them less useful and less clear to use -- you need to give a name to your lambda. In most cases you are better off just creating a callable class (like people did before lambdas were introduced)
+ 4
Instead of auto, use function<void(int)> where function is from <fucntional>
And remove the first parameter because you never use it.
+ 2
Thanks for helping me understand Volodymyr Chelnokov and CarrieForle ... appreciate your help
+ 1
I am sorry but not getting what you mean to say by specify the function return type in lambda recursive... Also what is related to functional header ? Could you please elaborate ?