3 ms·
Oof, just a whole lot of oof. So much developer thought and mental energy expended to do something as simple as recursion.
by dleslie 4y ago
Oof, just a whole lot of oof.
So much developer thought and mental energy expended to do something as simple as recursion.
- klyrs 4y agoTo be fair, most of the examples (and all of the ugly ones, IMO) are warts in today's C++ standard that are being greatly improved in C++23. That said, unless I use it frequently (spoiler: I won't), "this auto&& self" is gonna look weird and I really hope that searching the web for "this auto self" will produce useful hits by the time C++23 is in common use.
- amluto 4y agoI find it quite bizarre that ‘this’ ends up with two names in this construct.
- lionkor 4y agoIt really is bizarre, and this is the first time i've seen "deducing this" used. However, I get the naming so far: `this` can be captured, like [this](int x) { return x + this->x; } which we can use, in thise case, to discern between the local x, and this's x. Super contrived, but it illustrates that "this" may be there, too. Then, if you want to capture the lambda, and `this`. Then you need a new name, you can name it `self`, you can name it `this_lambda`, you can name it `ben` - but not `this` since it's reserved and can actually be used in that scope.
- stinos 4y agoRecursion is just one of the things being used as a means to explain/show lambdas here, i.e. the energy spent is for the explanation, not for recursion.
- einpoklum 4y agoRecursion in C++ is quite simple: int factorial (int n) { return (n > 1) ? n * factorial(n - 1) : 1; } it gets more complicated for more esoteric use cases: * Recursion in an anonymous function. Are you sure this is so simple in other languages? * Compile-time recursion with anonymous functions. Which other languages even have this to begin with? Both of these cases are rarely used in practice. The C++23 addition which facilitates them is actually due to another issue entirely, which is avoiding code duplication when you want to specialize methods according to features of the object they're running for (const/non-const, lvalue/rvalue). See: https://devblogs.microsoft.com/cppblog/cpp23-deducing-this/ https://devblogs.microsoft.com/cppblog/cpp23-deducing-this/
- SnowHill9902 4y agoCan the compiler statically prove that it’ll not cause stack overflow? Otherwise recursion seems quite unsafe.
- einpoklum 4y agoNaturally (if we remember our Turing machine computational capability analysis), the compiler cannot generally prove things about recursive functions, including how deep the recursion will go. What it can do in practice is use (configurable) hard limits on the depth and on the amount of time for evaluating constexpr expressions. That way it doesn't overflow its own stack/buffers, nor will it hang on such evaluations. This is good enough, since the purpose of this feature is not perform heavy computations using the compiler, but rather preventing not-so-heavy computations from being performed repeatedly at run-time (and to be able to use their results in template instantiation etc.)
- jeffbee 4y agoA theoretical compiler could statically prove that this program would have undefined behavior for any argument larger than 12, because signed overflow is UB, and it could arrive at the worst-case stack size from that.
- dleslie 4y agoIt's a named lambda, using self capture, and that's been in other languages for decades. Scheme is a good example, with letrec. Compile time recursive functions are likewise available in common lisp via recursive macros. Some schemes have them, too. And the syntax is much more elegant.