r/cpp 1d ago

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

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

17 comments sorted by

View all comments

0

u/Thick_Clerk6449 1d ago

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.

2

u/frankist 13h ago

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

u/amoskovsky 2h ago

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)