3 ms·
This article somehow omits the 80% of both the complexity and the benefits of the GCC nested functions which makes them pointless. Namely you can cast them to f
by alexey-salmin 26d ago
This article somehow omits the 80% of both the complexity and the benefits of the GCC nested functions which makes them pointless. Namely you can cast them to function pointers and pass them e.g. as a comparator to the sort() routine. Substituting the right parent frame parameter at the time the nested function is called down the stack is tricky and requires either an explicit support in the ABI (ia-64) or an executable stack to build a trampoline or a special logic to wrap function pointers with a special but set [1].
Without all this nested functions are as useful as the "rewritten" examples in the article, one can easily do that by hand without any compiler or language support.
This problem doesn't arise with C++ lambdas because you pass them around as special objects, not as bare function pointers.
[1] https://gcc.gnu.org/onlinedocs/gccint/Trampolines.html https://gcc.gnu.org/onlinedocs/gccint/Trampolines.html
- einpoklum 26d ago> This problem doesn't arise with C++ lambdas because you pass them around as special objects, not as bare function pointers. If they don't capture anything, I believe you _can_ pass them as bare function pointers.
- lpribis 26d agoSure, but in that case they are equivalent to a static function so there's no benefit of lambda other than syntax sugar.
- 6yyyyyy 26d agoThe benefit is that you don't have to solve one of the hard problems in computer science (naming things).
- moxxymiller 26d agoCapturing lambdas in a language without a GC (or a borrow checker) are kinda fraught with footguns.
- uecker 25d agoThis is not my experience.
- jcranmer 26d agoThe author has been trying to push for the inclusion of nested functions into the C standard, and a lot of the resistance comes from the existence of trampolines and all of the issues that causes. His response to those issues is... to basically go "nested functions, Objective-C blocks, and C++ lambdas are all the same thing if you squint at them hard enough" and ignore all of the very real semantic differences between all of them. For my part, I'll point out that there is one rather important difference between nested functions and C++ lambdas that the author completely ignores, as exhibited by this godbolt example: https://godbolt.org/z/35beWrrTe https://godbolt.org/z/35beWrrTe (note the differences in the generated assembly, especially that which cannot be explained merely by -O0 code generation).
- aw1621107 25d ago> note the differences in the generated assembly, especially that which cannot be explained merely by -O0 code generation Do you mind elaborating for those of us who aren't familiar with what to expect from the compiler?
- jcranmer 25d agoIn the nested function, the variable x is passed in edi, and the pointer to the nested stack frame is passed in r10. In the lambda, the variable x is passed in esi, and the 'this' pointer for the lambda is passed in rdi. The function-level ABIs end up being quite different.
- cenamus 26d agoCan't you convert non-capturing lambdas to function pointers?
- alexey-salmin 26d agoYes. The non-capturing case is equivalent to a static function for both lambdas and GCC nested functions.
- pjmlp 26d agoStarting by not being portable. Also misses that the way C++ lambdas work is that one design requirement was that they should be syntax sugar for the functor[0] pattern from C++98. [0] - Not to mix with ML functors, rather classes with call operator overloaded.
- uecker 25d agoWell, these things were discussed in previous articles the previous one is linked at the very top. This one explains how you can avoid the use of trampolines in GCC 17: https://uecker.codeberg.page/2026-07-14.html https://uecker.codeberg.page/2026-07-14.html The documentation is here: https://gcc.gnu.org/onlinedocs/gcc/Constructing-Calls.html https://gcc.gnu.org/onlinedocs/gcc/Constructing-Calls.html There are many language features can be rewritten by hand into other simpler forms, you could rewrite loops into gotos, C++ objects into structures, etc. This does not show that those things are not useful. But the point of the article was not to show why nested functions are useful, for which I would certainly have used more interesting examples.