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

Show parent comment

8

u/parkotron Sep 09 '26

I always found gcc's nested functions visually cleaner - that is, subfunctions syntactically just feel like they belong in the language alongside any other function

In C++98, it always felt incredibly bizarre that one could declare and define an entire class (or class template!) that was local to a function, but one couldn't define a pure, local helper function.

Lambdas obviously alleviated a lot of pain, but I do sometimes find myself debating whether some pure helper function longer than a few lines should be made a lambda in the function in which it is used or a free function in an anonymous namespace. Free functions feel cleaner to me, but require placing the function further from its point of use. Faced with the same situation in Rust, I'd just use a nested function.