This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Nested-Functions
On 01/03/2018 07:52 PM, Austin T wrote:
By nested functions, I'm assuming that means raw function definitions that
are valid inside a temporary scope of a function. If I'm not mistaken,
they're equivalent to C++ lambda expressions but just written in a
syntactic sugar syntax.
GNU-C nested functions are less powerful than lambdas, but there's a
performance tradeoff, that it is not clear to me which wins.
lambdas: the lambda gets hold of values and objects in the containing
function's scope by means of captures. These are members of a
constructed object type (the closure type). The capture may be by
reference, or value. Constructing the closure object involves
initializing pointers and/or invoking copy ctors. It happens once. You
can pass a functor which essentially decays to a plain pointer-to-fn if
there are no captures. You get a compile error if you ask not to
capture things, but do. You cannot convert a capturing lambda to a
pointer-to-regular-function (but you don't need to do that in C++ code).
nested fns: a hidden 'static chain' parameter is passed to them via a
spare register. They know the layout of their ancestors' stack frames
and directly access it to get outer scope entities. To create a
pointer-to-fn from one of these, the compiler constructs a
stack-resident trampoline, to initialize the static chain pointer. You
silently get such a thing, if you 'accidentally' capture an outer scope
object.
lambdas can be templates.
nathan
--
Nathan Sidwell