An implementation of generic lambdas (request for feedback) -- Faisal Vali

This week, Faisal Vali shared an initial "alpha" implementation of generic lambdas in Clang. Faisal is the lead author of the proposal (N3418), with Herb Sutter and Dave Abrahams.

To read and participate in the active discussion, see the message thread on std-proposals.

Here is a copy of Faisal's announcement:

Motivated by the positive feedback we received regarding Generic Lambdas (during the October 2012 ISO C++ Standards Meeting in Portland), I have implemented a reasonably complete (unless I am missing something obvious) implementation (http://faisalv.github.com/clang-glambda/) of most of the proposal (named lambda syntax for functions has not been attempted) using a fork of Clang.

 

We would like to encourage developers who are interested in this feature, to either compile the code or download the binaries (sorry, only Windows for now) and play with the implementation and provide us with feedback so that we can incorporate it within our next revision of the document.

The following link: http://faisalv.github.com/clang-glambda/, has some instructions (towards the bottom) on how to compile the code or use the binaries for Windows.

 

Any and all constructive feedback will be greatly appreciated.

The current version (12/2012) implements subproposals 2.1, 2.2, 2.3 and 2.5.

 

2.1 Allow the type-specifier within a parameter declaration of a lambda to be auto (i.e. auto is mandatory)

auto Sum = [](auto a, decltype(a) b) { return a + b; };
int i = Sum(3, 4);
double d = Sum(3.14, 2.77);

 

2.2 Allow the use of familiar template syntax in lambda expressions

auto NumElements = []<int N>(auto (&a)[N]) { return N; };
int arri[]{1, 2, 3};
double arrd[]{3.14, 2.77, 6.626};
auto total = NumElements(arri) + NumElements(arrd);

 

2.3 Permit a lambda body to be an expression

int local = 10;
auto L = [&](auto a) a + ++local;

 

2.5 Autogenerate a conversion to function pointer in captureless generic lambdas

auto L = [](auto a, decltype(a) b) { return a + b; };
int (*fp)(int, int) = L;

 

Thank you and looking forward to the feedback!

Add a Comment

Comments are closed.

Comments (8)

2 0

NoSenseEtAl said on Dec 21, 2012 02:37 AM:

Cool, regarding lambdas any chance we'll get to put lambdas directly as comparator in the sort, map... without very ugly decltype hackery?
2 2

NoSenseEtAl said on Dec 21, 2012 04:53 AM:

auto L = [](auto a, decltype(a) b) { return a + b; };
I would prefer:
auto L = [](auto a, b) { return a + b; };
0 0

DanielaE said on Dec 21, 2012 05:47 AM:

+1!

While this '[](auto a, decltype(a) b)' thing may look like hacky wizardry to many C++ users, your '[](auto a, b)' proposal seems much more consistent and pleasant to read - which is A Good Thing!
3 0

Alek said on Dec 21, 2012 10:32 AM:

I would prefer []<T>(T a, T b) { return a + b; };
If we have [](auto a, b) { return a + b; }; this would not work for the case with 3 parameters, 2 of which have the same type.
In my case I would write []<T1, T2>(T1 a, T1 b, T2 c) { return c.f(a + b); };
2 0

DanielaE said on Dec 21, 2012 11:25 AM:

This more template-like syntax is fine with me as well. It's this 'decltype(a) b' thing that makes me cringe. Both a and b have the same type, so they should be declared by the same syntax.
0 0

Alex Sinyakov said on Dec 24, 2012 01:55 AM:

this is very good and needed proposal. please approve it!
1 0

Enis Bayramoglu said on Dec 25, 2012 12:36 AM:

The lambda syntax is converging towards emoticons smile (which I have no problem with)
1 0

Kos said on Dec 27, 2012 05:13 AM:

-1 for auto syntax (2.1), +1 for `[]<T>(T a, T b)` syntax (2.2) because
a) it should be clear we're defining a function template here, and
b) this syntax makes it explicit whether a and b shall have the same type or not.
+1 for 2.3 and 2.5.

Question: What would be the type of Sum in `auto Sum = [](auto a, decltype(a) b){return a+b};` (whatever the syntax)?
I think `std::function` isn't enough to express that and we'd need an even more generic type on top of that. Conversion from that type to `std::function` (or a function pointer like in 2.5) resembles template instantiation.