Re: Lambda's in GCC

"Ben Bacarisse" <ben.usenet@xxxxxxxxx> wrote in message
Joe D <joe.davis512@xxxxxxxxx> writes:

I'm posting this in case someone finds it useful, I've already posted
it a couple of times in answers to stack overflow. It's an macro to
create lambda expressions in GCC, however it only works in GCC no
other compilers support it (not even clang).

#define lambda(return_type, body_and_args) \
({ \
return_type __fn__ body_and_args \
__fn__; \

Use it like this:

int (*max)(int, int) = lambda (int , (int x, int y) { return x > y ?
x : y; });

I don't think that is enough like a normal lambda to call it a lambda.
As I understand the GCC extension you are using, it can't capture
bindings that are in scope when the lambda returns its pointer.



I once considered something similar (although possibly with variable
capture) as a compiler extension with a syntax like:
<type>(<args>) { <body> }

so, for example:
int(int x, int y) { x+y; }
would behave similarly to a lambda.

or, a keyword like "__fun" could reduce the addition of parser complexity
(or possible ambiguity).

however, I never really implemented this feature (I started later becomming
a bit more conservative with compiler extensions, mostly since I tend to
share a lot of code between my compiler and native compilers such as GCC or
MSVC, and more "extreme" compiler extensions essentially get in the way of

the result then is that most of my extensions either:
exist in existing compilers;
serve some "necessary" role (can't be reasonably done by other means, rare);
offer a performance advantage and can be trivially emulated (such as my
vector extensions, which can use built in vectors in my compiler, and fall
back to generic SIMD types or structs for other compilers, and are generally
wrapped in macros or inline functions which themselves use the
compiler-specific mechanisms).

since a lambda does none of the above, I haven't added it.

or such...