Skip to content

Comment on Curious C++ Lambda Examples: Recursion, constexpr, Containers C++23 included

Comments

Instead of using "this auto&&" trick to get recursive lambdas, I wonder why they didn't go with named lambdas (Clojure made it really nice). E.g.

    auto factorial23 = [] fact_recur (int n) {
        if (n <= 1)
            return 1;
        return n * fact_recur(n - 1);
    };
Maybe this has to do with the usual C++ complicated parsing rules... But, on the other hand, named lambdas, besides recursion, in the language mentioned above helps with stacktraces, so you get the idea where the issue could be, instead of getting an anonymous mangled name mess.

Honestly approximately nobody cares about recursive lambdas in c++, it is mostly a party trick. The this auto syntax is just a side effect of the new 'explicit this' syntax that affects all member functions.

Because `this auto&&` was not introduced to allow for recursive lambads. That's just a happy (though not very interesting) side effect. It was introduced so that you don't have to write duplicated code such as:

    class Awesome {
        // ...
        foo& my_method() { /* ... */ }    
        const foo& my_method() const { /* ... */ }
    };
and the extra duplication for lvalues and rvalues.
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.