Implementation of GCC's Nested Functions (vs. C++ Lambdas)

(uecker.codeberg.page)

44 points | by uecker 3 days ago

2 comments

  • torginus 1 minute ago
    > In GCC, nested functions are lowered in an early middle-end pass. During this pass, all variables of the parent that are accessed by the nested function are collected into a single synthetic structure, and a pointer to this structure is passed to the nested function in a hidden argument

    Generally this is a bit nicer than having explicit lambdas, but I thought the 'best-case' scenario would be if GCC saw into the stack layout of the calling function and could manipulate the calling functions stored stack variables (and saved registers). After all, a debugger can track what variable goes where at every line of code, so this can be done.

    Not sure if this would be useful or practical, but would be a nice bit of nerd cred.

  • alexey-salmin 1 hour 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

    • jcranmer 13 minutes ago
      The 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 (note the differences in the generated assembly, especially that which cannot be explained merely by -O0 code generation).

    • einpoklum 1 hour 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 37 minutes ago
        Sure, but in that case they are equivalent to a static function so there's no benefit of lambda other than syntax sugar.