Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is (or will be) the use of familiar template syntax in lambda expressions allowed?

C++14 introduced generic lambdas. While skimming through the related proposals I found N3418 by Faisal Vali, Herb Sutter and Dave Abrahams. Therein section 2.2 is titled :

2.2 Allow the use of familiar template syntax in lambda expressions

and the following code examples include snippets like this one

[]<int N>(int (&a)[N]) {}

Since such things fail to compile (with gcc, clang and Visual Studio), some questions come up :

  • Is this an implementation issue ?
  • What stopped this part from being accepted ?
  • Which is the proposal that finally brought generic lambdas into the language ?
like image 954
Nikos Athanasiou Avatar asked Jul 02 '15 21:07

Nikos Athanasiou


People also ask

What is the correct syntax for Lambda Expression in C++11?

Lambdas can both capture variables and accept input parameters. A parameter list (lambda declarator in the Standard syntax) is optional and in most aspects resembles the parameter list for a function. auto y = [] (int first, int second) { return first + second; };

Can lambda be templated?

Lambda-expressions are not allowed in unevaluated expressions, template arguments, alias declarations, typedef declarations, and anywhere in a function (or function template) declaration except the function body and the function's default arguments.

What is the type of lambda function in C++?

A lambda is an object (hence why we're referring to it as a functor, rather than a function) so has a type and can be stored. However, the type of the lambda is only known by the compiler (since it is compiler-generated), so you must use auto for declaration instances of the lambda.

How do you call a lambda function in C++?

A lambda is also just a function object, so you need to have a () to call it, there is no way around it (except of course some function that invokes the lambda like std::invoke ). If you want you can drop the () after the capture list, because your lambda doesn't take any parameters.


2 Answers

The version of the paper that was accepted was N3649, we can see this by going to Evolution Working Group(EWG) Completed Issue 16: N3649, N3560, N3559, N3418 Proposal for Generic (Polymorphic) Lambda Expressions:

Reviewed by EWG in Portland 2012, proceeding with a follow-up paper.

Accepted into the Working Draft in Bristol 2013, as N3649.

Bristol 2013: Do not re-open proposals 2.1 and 2.2 in N3560, they are considered NAD. The proposals 2.3 and 2.4 are covered by N3649.

Note this references proposal 2.1 and 2.2 as being NAD(Not A Defect) and that they won't be reopened. N3560 was split off from N3418 which was the main proposal and proposal 2.1 in N3560 was:

Allow the use of familiar template syntax in lambda expressions

that paper notes proposal 2.1 was considered controversial:

We admit that supporting the full template parameter list feature has been deemed controversial (the Portland 2012 straw-poll outcomes were: 7 SF, 5 F, 3 N, 1 A, 1 SA 1 ) by a few committee members, and therefore conclude this sub-section with some quotes from a committee member who was not present in the room during EWG's discussion of this feature in Portland.

and we can see that N3649 does not contain this proposal my guess from the quote in paper N3560:

" I think we need more than just auto. I'm not sure how much more, but I think having just auto would be too limiting ".

was that auto was considered sufficient in the end which would be consistent with saying that the proposal is NAD meaning the issue it attempted to resolve is not really an issue.

like image 116
Shafik Yaghmour Avatar answered Oct 29 '22 19:10

Shafik Yaghmour


Note: I would comment on this but don't have the reputation to.

That precise construct that you quote works fine if I compile the following:

auto foo = []<int N>(int (&a)[N]) {};

with:

g++-4.9 -c foo.cpp -std=c++1y

However, using g++-4.8 or clang-3.6 both fail. Therefore, I would assume that it is an implementation issue. However, I am not familiar enough with the C++14 standard to know why/if it was accepted (for instance, g++ 4.9 supporting it may be non-standard).

like image 43
forkrul Avatar answered Oct 29 '22 20:10

forkrul