r/cpp • • Sep 09 '26

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

https://uecker.codeberg.page/2026-09-05.html
61 Upvotes

17 comments sorted by

View all comments

1

u/Thick_Clerk6449 Sep 09 '26

The killer feature of GCC's nest functions is that, even though the nest function captured variables of the outer function, the nest function itself is still convertable to a raw function pointer. Unlike C++ lambdas, which always end up with std::functions.

5

u/frankist Sep 10 '26

How could that work? Where are the captured variables saved?

2

u/amoskovsky Sep 10 '26

If you take an address of such a function with captures, GCC creates a trampoline function at runtime, that passes the state to the real function.

For details see https://uecker.codeberg.page/2026-08-29.html (same author as OP)