Re: Recursion, Yo

Liste des GroupesRevenir à l c 
Sujet : Re: Recursion, Yo
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 10. Apr 2024, 16:46:43
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <uv68ok$11080$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 10/04/2024 14:42, bart wrote:
On 10/04/2024 10:18, David Brown wrote:
On 10/04/2024 09:52, Lawrence D'Oliveiro wrote:
On Wed, 10 Apr 2024 09:11:49 +0200, David Brown wrote:
>
It is not much used in practice, AFAIK.  For some cases the code
generation for nested functions was fine and straight-forward.  In other
cases, however, it required a trampoline generated on the stack, and
that became a real pain once non-executable stacks came into fashion.
>
That would be true of those other languages that require the feature, too.
>
>
There may be other differences in the languages that reduce that effect - or it could be a problem there too.  I don't know the details. (Perhaps those on an Ada newsgroup would know better.)
>
The problem with trampolines comes about when you want to take the address of the nested function and use that outside of the surrounding function, while you also have variables captured by reference.
>
Nested functions were never as interesting for C++ as you already have
better mechanisms for controlling scope and data access - classes and
their methods, including nested classes.
>
Python does both. Just because you have classes doesn’t mean functions
can’t be first-class objects, too.
>
True.  But Python is a very different language from C++.  In Python, not only are functions objects in themselves, but so are classes (the definition of the classes, rather than just instances).  Python is a lot more "meta" than C++.
>
Basically, anything you could do with a nested function in gcc C you can do in C++:
>
int sum_square(int x, int y) {
     int square(z) {               // int z ??
         return z * z;
     }
     return square(x) + square(y);
}
 That's not an interesting use of a local function! You can move square() outside, and it would still work, putting aside any clashes with existing names called 'square'.
Sure.  It was not intended to be anything other than a very simple nested function written in gcc C extension syntax, and the same thing written in standard C++, to demonstrate how you can write nested functions in C++ without this extension.  Local functions without capturing local data are perhaps not interesting, but they can be neat from the viewpoint of limiting the scope of identifiers.

 The challenges of local functions are to do with accessing transient variables belonging to their enclosing function.
Yes.  And you can do that with the gcc extension, and also with C++ lambdas.

Especially when a reference to the local function is called from outside the lexical scope of the enclosing function, with extra difficulties when the enclosing function is no longer active.
 
These are the kinds of situations where the gcc extension needs to use trampolines.  C++ lambdas are closures, and can hold more information - each lambda is its own type, and the size of that type can be different for different lambdas (it can include pointers to the variables captured by reference, for example).  This makes lambdas much easier, safer and more efficient to use within a translation unit - but makes them a bit more fiddly if they capture local variables and you need to pass them to something defined outside the current TU.
(You can't use any local function outside the scope of the enclosing code if the local function has reference captures - that applies to all types of local functions.)

 
int sum_square(int x, int y) {
     auto square = [](int z) {
         return z * z;
     }
     return square(x) + square(y);
}
 What's the significance of the '[]' here?
 
It's the syntax for a C++ lambda.  You can put captures in there, or leave it empty for no captures.
Just for your entertainment, with C++ lambdas this is now legal code:
void foo(void) {
[](){}();
}
(It defines a lambda with no captures and no parameters, that does nothing, then it invokes it.)

Date Sujet#  Auteur
7 Apr 24 * Recursion, Yo102Lawrence D'Oliveiro
7 Apr 24 +* Re: Recursion, Yo100fir
9 Apr 24 i`* Re: Recursion, Yo99Janis Papanagnou
9 Apr 24 i +* Re: Recursion, Yo7Lawrence D'Oliveiro
9 Apr 24 i i+* Re: Recursion, Yo5Janis Papanagnou
9 Apr 24 i ii`* Re: Recursion, Yo4Ben Bacarisse
9 Apr 24 i ii `* Re: Recursion, Yo3Janis Papanagnou
9 Apr 24 i ii  `* Re: Recursion, Yo2Keith Thompson
10 Apr 24 i ii   `- Re: Recursion, Yo1Janis Papanagnou
9 Apr 24 i i`- Re: Recursion, Yo1bart
9 Apr 24 i `* Re: Recursion, Yo91Ben Bacarisse
9 Apr 24 i  +- Re: Recursion, Yo1Ben Bacarisse
10 Apr 24 i  `* Re: Recursion, Yo89Lawrence D'Oliveiro
10 Apr 24 i   +- Re: Recursion, Yo1Chris M. Thomasson
10 Apr 24 i   +* Re: Recursion, Yo86David Brown
10 Apr 24 i   i+* Re: Recursion, Yo84Lawrence D'Oliveiro
10 Apr 24 i   ii`* Re: Recursion, Yo83David Brown
10 Apr 24 i   ii +* Re: Recursion, Yo73bart
10 Apr 24 i   ii i+* Re: Recursion, Yo71David Brown
11 Apr 24 i   ii ii`* Re: Recursion, Yo70Lawrence D'Oliveiro
11 Apr 24 i   ii ii +* Re: Recursion, Yo4Kaz Kylheku
11 Apr 24 i   ii ii i`* Heh heh... (Was: Recursion, Yo)3Kenny McCormack
11 Apr 24 i   ii ii i `* Re: Heh heh... (Was: Recursion, Yo)2Kaz Kylheku
11 Apr 24 i   ii ii i  `- Re: Heh heh... (Was: Recursion, Yo)1Kenny McCormack
11 Apr 24 i   ii ii `* Re: Recursion, Yo65David Brown
11 Apr 24 i   ii ii  +* Re: Recursion, Yo62Kaz Kylheku
11 Apr 24 i   ii ii  i+- Re: Recursion, Yo1David Brown
12 Apr 24 i   ii ii  i`* Re: Recursion, Yo60Lawrence D'Oliveiro
12 Apr 24 i   ii ii  i +* Re: Recursion, Yo2Kaz Kylheku
12 Apr 24 i   ii ii  i i`- Re: Recursion, Yo1Dan Cross
12 Apr 24 i   ii ii  i `* Re: Recursion, Yo57Janis Papanagnou
12 Apr 24 i   ii ii  i  +* Re: Recursion, Yo6David Brown
12 Apr 24 i   ii ii  i  i`* Re: Recursion, Yo5Janis Papanagnou
12 Apr 24 i   ii ii  i  i +* Re: Recursion, Yo3David Brown
13 Apr 24 i   ii ii  i  i i`* Re: Recursion, Yo2Janis Papanagnou
13 Apr 24 i   ii ii  i  i i `- Re: Recursion, Yo1David Brown
13 Apr 24 i   ii ii  i  i `- Re: Recursion, Yo1Lawrence D'Oliveiro
12 Apr 24 i   ii ii  i  +* Re: Recursion, Yo45Lawrence D'Oliveiro
12 Apr 24 i   ii ii  i  i`* Re: Recursion, Yo44Janis Papanagnou
12 Apr 24 i   ii ii  i  i +- Re: Recursion, Yo1bart
13 Apr 24 i   ii ii  i  i `* Re: Recursion, Yo42Lawrence D'Oliveiro
13 Apr 24 i   ii ii  i  i  +* Re: Recursion, Yo14Michael S
14 Apr 24 i   ii ii  i  i  i+* Re: Recursion, Yo11Ben Bacarisse
14 Apr 24 i   ii ii  i  i  ii`* Re: Recursion, Yo10Michael S
15 Apr 24 i   ii ii  i  i  ii `* Re: Recursion, Yo9Janis Papanagnou
15 Apr 24 i   ii ii  i  i  ii  `* Re: Recursion, Yo8Keith Thompson
15 Apr 24 i   ii ii  i  i  ii   `* Re: Recursion, Yo7Ben Bacarisse
15 Apr 24 i   ii ii  i  i  ii    `* Re: Recursion, Yo6Keith Thompson
15 Apr 24 i   ii ii  i  i  ii     +* Re: Recursion, Yo2bart
15 Apr 24 i   ii ii  i  i  ii     i`- Re: Recursion, Yo1Ben Bacarisse
15 Apr 24 i   ii ii  i  i  ii     `* Re: Recursion, Yo3Janis Papanagnou
15 Apr 24 i   ii ii  i  i  ii      +- Re: Recursion, Yo1Janis Papanagnou
15 Apr 24 i   ii ii  i  i  ii      `- Re: Recursion, Yo1Keith Thompson
14 Apr 24 i   ii ii  i  i  i`* Re: Recursion, Yo2Lawrence D'Oliveiro
14 Apr 24 i   ii ii  i  i  i `- Re: Recursion, Yo1Keith Thompson
13 Apr 24 i   ii ii  i  i  `* Re: Recursion, Yo27Janis Papanagnou
14 Apr 24 i   ii ii  i  i   `* Re: Recursion, Yo26Lawrence D'Oliveiro
14 Apr 24 i   ii ii  i  i    `* Re: Recursion, Yo25Ben Bacarisse
14 Apr 24 i   ii ii  i  i     +* Re: Recursion, Yo2bart
14 Apr 24 i   ii ii  i  i     i`- Re: Recursion, Yo1Ben Bacarisse
15 Apr 24 i   ii ii  i  i     `* Re: Recursion, Yo22Lawrence D'Oliveiro
15 Apr 24 i   ii ii  i  i      +* Re: Recursion, Yo20Chris M. Thomasson
15 Apr 24 i   ii ii  i  i      i+* Re: Recursion, Yo5Ben Bacarisse
16 Apr 24 i   ii ii  i  i      ii`* Re: Recursion, Yo4Lawrence D'Oliveiro
17 Apr 24 i   ii ii  i  i      ii `* Re: Recursion, Yo3Ben Bacarisse
18 Apr 24 i   ii ii  i  i      ii  `* Re: Recursion, Yo2Lawrence D'Oliveiro
22 Apr 24 i   ii ii  i  i      ii   `- Re: Recursion, Yo1Janis Papanagnou
15 Apr 24 i   ii ii  i  i      i`* Re: Recursion, Yo14Janis Papanagnou
16 Apr 24 i   ii ii  i  i      i +* Re: Recursion, Yo2Lawrence D'Oliveiro
22 Apr 24 i   ii ii  i  i      i i`- Re: Recursion, Yo1Janis Papanagnou
16 Apr 24 i   ii ii  i  i      i `* Re: Recursion, Yo11Michael S
19 Apr 24 i   ii ii  i  i      i  +* Re: Recursion, Yo9Tim Rentsch
19 Apr 24 i   ii ii  i  i      i  i+* Re: Recursion, Yo4bart
20 Apr 24 i   ii ii  i  i      i  ii`* Re: Recursion, Yo3Ben Bacarisse
20 Apr 24 i   ii ii  i  i      i  ii +- Re: Recursion, Yo1Keith Thompson
20 Apr 24 i   ii ii  i  i      i  ii `- Re: Recursion, Yo1Kaz Kylheku
19 Apr 24 i   ii ii  i  i      i  i+- Re: Recursion, Yo1Keith Thompson
19 Apr 24 i   ii ii  i  i      i  i+* Re: Recursion, Yo2Keith Thompson
20 Apr 24 i   ii ii  i  i      i  ii`- Re: Recursion, Yo1Tim Rentsch
20 Apr 24 i   ii ii  i  i      i  i`- Re: Recursion, Yo1Tim Rentsch
22 Apr 24 i   ii ii  i  i      i  `- Re: Recursion, Yo1Janis Papanagnou
15 Apr 24 i   ii ii  i  i      `- Re: Recursion, Yo1Ben Bacarisse
12 Apr 24 i   ii ii  i  +* Re: Recursion, Yo4bart
12 Apr 24 i   ii ii  i  i+* Re: Recursion, Yo2Janis Papanagnou
12 Apr 24 i   ii ii  i  ii`- Re: Recursion, Yo1bart
13 Apr 24 i   ii ii  i  i`- Re: Recursion, Yo1Keith Thompson
13 Apr 24 i   ii ii  i  `- Re: Recursion, Yo1Tim Rentsch
11 Apr 24 i   ii ii  `* Re: Recursion, Yo2Keith Thompson
12 Apr 24 i   ii ii   `- Re: Recursion, Yo1David Brown
14 Apr 24 i   ii i`- Re: Recursion, Yo1fir
10 Apr 24 i   ii +- Re: Recursion, Yo1Janis Papanagnou
10 Apr 24 i   ii +* Re: Recursion, Yo2Kaz Kylheku
10 Apr 24 i   ii i`- Re: Recursion, Yo1David Brown
11 Apr 24 i   ii +* Re: Recursion, Yo2Lawrence D'Oliveiro
12 Apr 24 i   ii i`- Re: Recursion, Yo1Ben Bacarisse
11 Apr 24 i   ii `* Re: Recursion, Yo4Lawrence D'Oliveiro
11 Apr 24 i   ii  +- Re: Recursion, Yo1Kaz Kylheku
11 Apr 24 i   ii  `* Re: Recursion, Yo2David Brown
11 Apr 24 i   ii   `- Re: Recursion, Yo1Lawrence D'Oliveiro
10 Apr 24 i   i`- Re: Recursion, Yo1Kaz Kylheku
10 Apr 24 i   `- Re: Recursion, Yo1Tim Rentsch
9 Apr 24 `- Re: Recursion, Yo1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal