From Jason Turner

[expr.prim.lambda]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpl5rmox84/{from.md → to.md} +185 -107
tmp/tmpl5rmox84/{from.md → to.md} RENAMED
@@ -1,22 +1,42 @@
1
  ### Lambda expressions <a id="expr.prim.lambda">[[expr.prim.lambda]]</a>
2
 
 
 
3
  ``` bnf
4
  lambda-expression:
5
- lambda-introducer lambda-declaratorₒₚₜ compound-statement
6
- lambda-introducer '<' template-parameter-list '>' requires-clauseₒₚₜ lambda-declaratorₒₚₜ compound-statement
 
7
  ```
8
 
9
  ``` bnf
10
  lambda-introducer:
11
  '[' lambda-captureₒₚₜ ']'
12
  ```
13
 
14
  ``` bnf
15
  lambda-declarator:
16
- '(' parameter-declaration-clause ')' decl-specifier-seqₒₚₜ
17
- noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ requires-clauseₒₚₜ
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  ```
19
 
20
  A *lambda-expression* provides a concise way to create a simple function
21
  object.
22
 
@@ -36,29 +56,46 @@ A *lambda-expression* is a prvalue whose result object is called the
36
  *closure object*.
37
 
38
  [*Note 1*: A closure object behaves like a function object
39
  [[function.objects]]. — *end note*]
40
 
41
- In the *decl-specifier-seq* of the *lambda-declarator*, each
42
- *decl-specifier* shall be one of `mutable`, `constexpr`, or `consteval`.
 
 
 
43
 
44
- [*Note 2*: The trailing *requires-clause* is described in
 
 
 
 
 
 
 
 
 
 
 
 
45
  [[dcl.decl]]. — *end note*]
46
 
47
- If a *lambda-expression* does not include a *lambda-declarator*, it is
48
- as if the *lambda-declarator* were `()`. The lambda return type is
49
- `auto`, which is replaced by the type specified by the
50
- *trailing-return-type* if provided and/or deduced from `return`
51
- statements as described in  [[dcl.spec.auto]].
 
 
52
 
53
  [*Example 2*:
54
 
55
  ``` cpp
56
- auto x1 = [](int i){ return i; }; // OK: return type is int
57
  auto x2 = []{ return { 1, 2 }; }; // error: deducing return type from braced-init-list
58
  int j;
59
- auto x3 = []()->auto&& { return j; }; // OK: return type is int&
60
  ```
61
 
62
  — *end example*]
63
 
64
  A lambda is a *generic lambda* if the *lambda-expression* has any
@@ -66,12 +103,12 @@ generic parameter type placeholders [[dcl.spec.auto]], or if the lambda
66
  has a *template-parameter-list*.
67
 
68
  [*Example 3*:
69
 
70
  ``` cpp
71
- int i = [](int i, auto a) { return i; }(3, 4); // OK: a generic lambda
72
- int j = []<class T>(T t, int i) { return i; }(3, 4); // OK: a generic lambda
73
  ```
74
 
75
  — *end example*]
76
 
77
  #### Closure types <a id="expr.prim.lambda.closure">[[expr.prim.lambda.closure]]</a>
@@ -110,56 +147,90 @@ and whose *template-parameter-list* consists of the specified
110
  call operator template is the *requires-clause* immediately following
111
  `<` *template-parameter-list* `>`, if any. The trailing
112
  *requires-clause* of the function call operator or operator template is
113
  the *requires-clause* of the *lambda-declarator*, if any.
114
 
115
- [*Note 2*: The function call operator template for a generic lambda
116
- might be an abbreviated function template [[dcl.fct]]. — *end note*]
117
 
118
  [*Example 1*:
119
 
120
  ``` cpp
121
  auto glambda = [](auto a, auto&& b) { return a < b; };
122
  bool b = glambda(3, 3.14); // OK
123
 
124
  auto vglambda = [](auto printer) {
125
- return [=](auto&& ... ts) { // OK: ts is a function parameter pack
126
  printer(std::forward<decltype(ts)>(ts)...);
127
 
128
  return [=]() {
129
  printer(ts ...);
130
  };
131
  };
132
  };
133
  auto p = vglambda( [](auto v1, auto v2, auto v3)
134
  { std::cout << v1 << v2 << v3; } );
135
- auto q = p(1, 'a', 3.14); // OK: outputs 1a3.14
136
- q(); // OK: outputs 1a3.14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  ```
138
 
139
  — *end example*]
140
 
141
- The function call operator or operator template is declared `const` (
142
- [[class.mfct.non-static]]) if and only if the *lambda-expression*’s
143
- *parameter-declaration-clause* is not followed by `mutable`. It is
 
 
 
 
 
144
  neither virtual nor declared `volatile`. Any *noexcept-specifier*
145
  specified on a *lambda-expression* applies to the corresponding function
146
  call operator or operator template. An *attribute-specifier-seq* in a
147
  *lambda-declarator* appertains to the type of the corresponding function
148
- call operator or operator template. The function call operator or any
149
- given operator template specialization is a constexpr function if either
150
- the corresponding *lambda-expression*'s *parameter-declaration-clause*
151
- is followed by `constexpr` or `consteval`, or it satisfies the
152
- requirements for a constexpr function [[dcl.constexpr]]. It is an
 
 
153
  immediate function [[dcl.constexpr]] if the corresponding
154
  *lambda-expression*'s *parameter-declaration-clause* is followed by
155
  `consteval`.
156
 
157
- [*Note 3*: Names referenced in the *lambda-declarator* are looked up in
158
- the context in which the *lambda-expression* appears. — *end note*]
159
-
160
- [*Example 2*:
161
 
162
  ``` cpp
163
  auto ID = [](auto a) { return a; };
164
  static_assert(ID(3) == 3); // OK
165
 
@@ -170,11 +241,11 @@ struct NonLiteral {
170
  static_assert(ID(NonLiteral{3}).n == 3); // error
171
  ```
172
 
173
  — *end example*]
174
 
175
- [*Example 3*:
176
 
177
  ``` cpp
178
  auto monoid = [](auto v) { return [=] { return v; }; };
179
  auto add = [](auto m1) constexpr {
180
  auto ret = m1();
@@ -199,18 +270,18 @@ static_assert(add(one)(one)() == two()); // error: two() is not a constan
199
  static_assert(add(one)(one)() == monoid(2)()); // OK
200
  ```
201
 
202
  — *end example*]
203
 
204
- [*Note 4*:
205
 
206
- The function call operator or operator template may be constrained
207
  [[temp.constr.decl]] by a *type-constraint* [[temp.param]], a
208
  *requires-clause* [[temp.pre]], or a trailing *requires-clause*
209
  [[dcl.decl]].
210
 
211
- [*Example 4*:
212
 
213
  ``` cpp
214
  template <typename T> concept C1 = ...;
215
  template <std::size_t N> concept C2 = ...;
216
  template <typename A, typename B> concept C3 = ...;
@@ -231,27 +302,29 @@ The closure type for a non-generic *lambda-expression* with no
231
  *lambda-capture* whose constraints (if any) are satisfied has a
232
  conversion function to pointer to function with C++ language linkage
233
  [[dcl.link]] having the same parameter and return types as the closure
234
  type’s function call operator. The conversion is to “pointer to
235
  `noexcept` function” if the function call operator has a non-throwing
236
- exception specification. The value returned by this conversion function
237
- is the address of a function `F` that, when invoked, has the same effect
238
- as invoking the closure type’s function call operator on a
239
- default-constructed instance of the closure type. `F` is a constexpr
240
- function if the function call operator is a constexpr function and is an
241
- immediate function if the function call operator is an immediate
242
- function.
 
 
243
 
244
  For a generic lambda with no *lambda-capture*, the closure type has a
245
  conversion function template to pointer to function. The conversion
246
  function template has the same invented template parameter list, and the
247
  pointer to function has the same parameter types, as the function call
248
  operator template. The return type of the pointer to function shall
249
  behave as if it were a *decltype-specifier* denoting the return type of
250
  the corresponding function call operator template specialization.
251
 
252
- [*Note 5*:
253
 
254
  If the generic lambda has no *trailing-return-type* or the
255
  *trailing-return-type* contains a placeholder type, return type
256
  deduction of the corresponding function call operator template
257
  specialization has to be done. The corresponding specialization is that
@@ -283,11 +356,11 @@ struct Closure {
283
  };
284
  ```
285
 
286
  — *end note*]
287
 
288
- [*Example 5*:
289
 
290
  ``` cpp
291
  void f1(int (*)(int)) { }
292
  void f2(char (*)(int)) { }
293
 
@@ -299,45 +372,49 @@ void h(char (*)(int)) { } // #4
299
 
300
  auto glambda = [](auto a) { return a; };
301
  f1(glambda); // OK
302
  f2(glambda); // error: ID is not convertible
303
  g(glambda); // error: ambiguous
304
- h(glambda); // OK: calls #3 since it is convertible from ID
305
  int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
306
  ```
307
 
308
  — *end example*]
309
 
310
- The value returned by any given specialization of this conversion
311
- function template is the address of a function `F` that, when invoked,
312
- has the same effect as invoking the generic lambda’s corresponding
313
- function call operator template specialization on a default-constructed
314
- instance of the closure type. `F` is a constexpr function if the
315
- corresponding specialization is a constexpr function and `F` is an
316
- immediate function if the function call operator template specialization
317
- is an immediate function.
 
 
 
 
318
 
319
- [*Note 6*: This will result in the implicit instantiation of the
320
  generic lambda’s body. The instantiated generic lambda’s return type and
321
  parameter types are required to match the return type and parameter
322
  types of the pointer to function. — *end note*]
323
 
324
- [*Example 6*:
325
 
326
  ``` cpp
327
  auto GL = [](auto a) { std::cout << a; return a; };
328
- int (*GL_int)(int) = GL; // OK: through conversion function template
329
- GL_int(3); // OK: same as GL(3)
330
  ```
331
 
332
  — *end example*]
333
 
334
  The conversion function or conversion function template is public,
335
  constexpr, non-virtual, non-explicit, const, and has a non-throwing
336
  exception specification [[except.spec]].
337
 
338
- [*Example 7*:
339
 
340
  ``` cpp
341
  auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
342
  auto C = [](auto a) { return a; };
343
 
@@ -349,18 +426,14 @@ static_assert(Fwd(NC,3) == 3); // error
349
  ```
350
 
351
  — *end example*]
352
 
353
  The *lambda-expression*’s *compound-statement* yields the
354
- *function-body* [[dcl.fct.def]] of the function call operator, but for
355
- purposes of name lookup [[basic.lookup]], determining the type and value
356
- of `this` [[class.this]] and transforming *id-expression*s referring to
357
- non-static class members into class member access expressions using
358
- `(*this)` ([[class.mfct.non-static]]), the *compound-statement* is
359
- considered in the context of the *lambda-expression*.
360
 
361
- [*Example 8*:
362
 
363
  ``` cpp
364
  struct S1 {
365
  int x, y;
366
  int operator()(int);
@@ -385,12 +458,12 @@ defaulted default constructor otherwise. It has a defaulted copy
385
  constructor and a defaulted move constructor [[class.copy.ctor]]. It has
386
  a deleted copy assignment operator if the *lambda-expression* has a
387
  *lambda-capture* and defaulted copy and move assignment operators
388
  otherwise [[class.copy.assign]].
389
 
390
- [*Note 7*: These special member functions are implicitly defined as
391
- usual, and might therefore be defined as deleted. — *end note*]
392
 
393
  The closure type associated with a *lambda-expression* has an
394
  implicitly-declared destructor [[class.dtor]].
395
 
396
  A member of a closure type shall not be explicitly instantiated
@@ -436,13 +509,12 @@ simple-capture:
436
  init-capture:
437
  '...'ₒₚₜ identifier initializer
438
  '&' '...'ₒₚₜ identifier initializer
439
  ```
440
 
441
- The body of a *lambda-expression* may refer to variables with automatic
442
- storage duration and the `*this` object (if any) of enclosing block
443
- scopes by capturing those entities, as described below.
444
 
445
  If a *lambda-capture* includes a *capture-default* that is `&`, no
446
  identifier in a *simple-capture* of that *lambda-capture* shall be
447
  preceded by `&`. If a *lambda-capture* includes a *capture-default* that
448
  is `=`, each *simple-capture* of that *lambda-capture* shall be of the
@@ -475,35 +547,37 @@ A *lambda-expression* shall not have a *capture-default* or
475
  *simple-capture* in its *lambda-introducer* unless its innermost
476
  enclosing scope is a block scope [[basic.scope.block]] or it appears
477
  within a default member initializer and its innermost enclosing scope is
478
  the corresponding class scope [[basic.scope.class]].
479
 
480
- The *identifier* in a *simple-capture* is looked up using the usual
481
- rules for unqualified name lookup [[basic.lookup.unqual]]; each such
482
- lookup shall find a local entity. The *simple-capture*s `this` and
483
  `* this` denote the local entity `*this`. An entity that is designated
484
  by a *simple-capture* is said to be *explicitly captured*.
485
 
486
- If an *identifier* in a *simple-capture* appears as the *declarator-id*
487
- of a parameter of the *lambda-declarator*'s
488
- *parameter-declaration-clause*, the program is ill-formed.
 
489
 
490
  [*Example 2*:
491
 
492
  ``` cpp
493
  void f() {
494
  int x = 0;
495
- auto g = [x](int x) { return 0; }; // error: parameter and simple-capture have the same name
 
 
496
  }
497
  ```
498
 
499
  — *end example*]
500
 
501
- An *init-capture* without ellipsis behaves as if it declares and
502
- explicitly captures a variable of the form “`auto` *init-capture* `;`”
503
- whose declarative region is the *lambda-expression*’s
504
- *compound-statement*, except that:
505
 
506
  - if the capture is by copy (see below), the non-static data member
507
  declared for the capture and the variable are treated as two different
508
  ways of referring to the same object, which has the lifetime of the
509
  non-static data member, and no additional copy and destruction is
@@ -522,11 +596,14 @@ int x = 4;
522
  auto y = [&r = x, x = x+1]()->int {
523
  r += 2;
524
  return x+2;
525
  }(); // Updates ::x to 6, and initializes y to 7.
526
 
527
- auto z = [a = 42](int a) { return 1; }; // error: parameter and local variable have the same name
 
 
 
528
  ```
529
 
530
  — *end example*]
531
 
532
  For the purposes of lambda capture, an expression potentially references
@@ -540,13 +617,13 @@ local entities as follows:
540
  *id-expression*. — *end note*]
541
  - A `this` expression potentially references `*this`.
542
  - A *lambda-expression* potentially references the local entities named
543
  by its *simple-capture*s.
544
 
545
- If an expression potentially references a local entity within a
546
- declarative region in which it is odr-usable, and the expression would
547
- be potentially evaluated if the effect of any enclosing `typeid`
548
  expressions [[expr.typeid]] were ignored, the entity is said to be
549
  *implicitly captured* by each intervening *lambda-expression* with an
550
  associated *capture-default* that does not explicitly capture it. The
551
  implicit capture of `*this` is deprecated when the *capture-default* is
552
  `=`; see [[depr.capture.this]].
@@ -557,38 +634,38 @@ implicit capture of `*this` is deprecated when the *capture-default* is
557
  void f(int, const int (&)[2] = {}); // #1
558
  void f(const int&, const int (&)[1]); // #2
559
  void test() {
560
  const int x = 17;
561
  auto g = [](auto a) {
562
- f(x); // OK: calls #1, does not capture x
563
  };
564
 
565
  auto g1 = [=](auto a) {
566
- f(x); // OK: calls #1, captures x
567
  };
568
 
569
  auto g2 = [=](auto a) {
570
  int selector[sizeof(a) == 1 ? 1 : 2]{};
571
- f(x, selector); // OK: captures x, might call #1 or #2
572
  };
573
 
574
  auto g3 = [=](auto a) {
575
  typeid(a + x); // captures x regardless of whether a + x is an unevaluated operand
576
  };
577
  }
578
  ```
579
 
580
- Within `g1`, an implementation might optimize away the capture of `x` as
581
  it is not odr-used.
582
 
583
  — *end example*]
584
 
585
  [*Note 4*:
586
 
587
  The set of captured entities is determined syntactically, and entities
588
- might be implicitly captured even if the expression denoting a local
589
- entity is within a discarded statement [[stmt.if]].
590
 
591
  [*Example 5*:
592
 
593
  ``` cpp
594
  template<bool B>
@@ -604,12 +681,12 @@ void f(int n) {
604
  — *end example*]
605
 
606
  — *end note*]
607
 
608
  An entity is *captured* if it is captured explicitly or implicitly. An
609
- entity captured by a *lambda-expression* is odr-used [[basic.def.odr]]
610
- in the scope containing the *lambda-expression*.
611
 
612
  [*Note 5*: As a consequence, if a *lambda-expression* explicitly
613
  captures an entity that is not odr-usable, the program is ill-formed
614
  [[basic.def.odr]]. — *end note*]
615
 
@@ -619,26 +696,26 @@ captures an entity that is not odr-usable, the program is ill-formed
619
  void f1(int i) {
620
  int const N = 20;
621
  auto m1 = [=]{
622
  int const M = 30;
623
  auto m2 = [i]{
624
- int x[N][M]; // OK: N and M are not odr-used
625
- x[0][0] = i; // OK: i is explicitly captured by m2 and implicitly captured by m1
626
  };
627
  };
628
  struct s1 {
629
  int f;
630
  void work(int n) {
631
  int m = n*n;
632
  int j = 40;
633
  auto m3 = [this,m] {
634
  auto m4 = [&,j] { // error: j not odr-usable due to intervening lambda m3
635
  int x = n; // error: n is odr-used but not odr-usable due to intervening lambda m3
636
- x += m; // OK: m implicitly captured by m4 and explicitly captured by m3
637
  x += i; // error: i is odr-used but not odr-usable
638
  // due to intervening function and class scopes
639
- x += f; // OK: this captured implicitly by m4 and explicitly by m3
640
  };
641
  };
642
  }
643
  };
644
  }
@@ -701,11 +778,11 @@ the entity is a reference to an object, an lvalue reference to the
701
  referenced function type if the entity is a reference to a function, or
702
  the type of the corresponding captured entity otherwise. A member of an
703
  anonymous union shall not be captured by copy.
704
 
705
  Every *id-expression* within the *compound-statement* of a
706
- *lambda-expression* that is an odr-use [[basic.def.odr]] of an entity
707
  captured by copy is transformed into an access to the corresponding
708
  unnamed data member of the closure type.
709
 
710
  [*Note 7*: An *id-expression* that is not an odr-use refers to the
711
  original entity, never to a member of the closure type. However, such an
@@ -721,12 +798,12 @@ the closure type.
721
  ``` cpp
722
  void f(const int*);
723
  void g() {
724
  const int N = 10;
725
  [=] {
726
- int arr[N]; // OK: not an odr-use, refers to automatic variable
727
- f(&N); // OK: causes N to be captured; &N points to
728
  // the corresponding member of the closure type
729
  };
730
  }
731
  ```
732
 
@@ -774,13 +851,15 @@ auto h(int &r) {
774
 
775
  If a *lambda-expression* `m2` captures an entity and that entity is
776
  captured by an immediately enclosing *lambda-expression* `m1`, then
777
  `m2`’s capture is transformed as follows:
778
 
779
- - if `m1` captures the entity by copy, `m2` captures the corresponding
780
- non-static data member of `m1`’s closure type;
781
- - if `m1` captures the entity by reference, `m2` captures the same
 
 
782
  entity captured by `m1`.
783
 
784
  [*Example 11*:
785
 
786
  The nested *lambda-expression*s and invocations below will output
@@ -821,12 +900,11 @@ captured by reference, invoking the function call operator of the
821
  corresponding *lambda-expression* after the lifetime of the entity has
822
  ended is likely to result in undefined behavior. — *end note*]
823
 
824
  A *simple-capture* containing an ellipsis is a pack expansion
825
  [[temp.variadic]]. An *init-capture* containing an ellipsis is a pack
826
- expansion that introduces an *init-capture* pack [[temp.variadic]] whose
827
- declarative region is the *lambda-expression*’s *compound-statement*.
828
 
829
  [*Example 12*:
830
 
831
  ``` cpp
832
  template<class... Args>
 
1
  ### Lambda expressions <a id="expr.prim.lambda">[[expr.prim.lambda]]</a>
2
 
3
+ #### General <a id="expr.prim.lambda.general">[[expr.prim.lambda.general]]</a>
4
+
5
  ``` bnf
6
  lambda-expression:
7
+ lambda-introducer attribute-specifier-seqₒₚₜ lambda-declarator compound-statement
8
+ lambda-introducer '<' template-parameter-list '>' requires-clauseₒₚₜ attribute-specifier-seqₒₚₜ
9
+ lambda-declarator compound-statement
10
  ```
11
 
12
  ``` bnf
13
  lambda-introducer:
14
  '[' lambda-captureₒₚₜ ']'
15
  ```
16
 
17
  ``` bnf
18
  lambda-declarator:
19
+ lambda-specifier-seq noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ
20
+ noexcept-specifier attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ
21
+ trailing-return-typeₒₚₜ
22
+ '(' parameter-declaration-clause ')' lambda-specifier-seqₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
23
+ trailing-return-typeₒₚₜ requires-clauseₒₚₜ
24
+ ```
25
+
26
+ ``` bnf
27
+ lambda-specifier:
28
+ consteval
29
+ constexpr
30
+ mutable
31
+ static
32
+ ```
33
+
34
+ ``` bnf
35
+ lambda-specifier-seq:
36
+ lambda-specifier
37
+ lambda-specifier lambda-specifier-seq
38
  ```
39
 
40
  A *lambda-expression* provides a concise way to create a simple function
41
  object.
42
 
 
56
  *closure object*.
57
 
58
  [*Note 1*: A closure object behaves like a function object
59
  [[function.objects]]. — *end note*]
60
 
61
+ An ambiguity can arise because a *requires-clause* can end in an
62
+ *attribute-specifier-seq*, which collides with the
63
+ *attribute-specifier-seq* in *lambda-expression*. In such cases, any
64
+ attributes are treated as *attribute-specifier-seq* in
65
+ *lambda-expression*.
66
 
67
+ [*Note 2*: Such ambiguous cases cannot have valid semantics because the
68
+ constraint expression would not have type `bool`. — *end note*]
69
+
70
+ A *lambda-specifier-seq* shall contain at most one of each
71
+ *lambda-specifier* and shall not contain both `constexpr` and
72
+ `consteval`. If the *lambda-declarator* contains an explicit object
73
+ parameter [[dcl.fct]], then no *lambda-specifier* in the
74
+ *lambda-specifier-seq* shall be `mutable` or `static`. The
75
+ *lambda-specifier-seq* shall not contain both `mutable` and `static`. If
76
+ the *lambda-specifier-seq* contains `static`, there shall be no
77
+ *lambda-capture*.
78
+
79
+ [*Note 3*: The trailing *requires-clause* is described in
80
  [[dcl.decl]]. — *end note*]
81
 
82
+ If a *lambda-declarator* does not include a
83
+ *parameter-declaration-clause*, it is as if `()` were inserted at the
84
+ start of the *lambda-declarator*. If the *lambda-declarator* does not
85
+ include a *trailing-return-type*, it is considered to be `-> auto`.
86
+
87
+ [*Note 4*: In that case, the return type is deduced from `return`
88
+ statements as described in [[dcl.spec.auto]]. — *end note*]
89
 
90
  [*Example 2*:
91
 
92
  ``` cpp
93
+ auto x1 = [](int i) { return i; }; // OK, return type is int
94
  auto x2 = []{ return { 1, 2 }; }; // error: deducing return type from braced-init-list
95
  int j;
96
+ auto x3 = [&]()->auto&& { return j; }; // OK, return type is int&
97
  ```
98
 
99
  — *end example*]
100
 
101
  A lambda is a *generic lambda* if the *lambda-expression* has any
 
103
  has a *template-parameter-list*.
104
 
105
  [*Example 3*:
106
 
107
  ``` cpp
108
+ int i = [](int i, auto a) { return i; }(3, 4); // OK, a generic lambda
109
+ int j = []<class T>(T t, int i) { return i; }(3, 4); // OK, a generic lambda
110
  ```
111
 
112
  — *end example*]
113
 
114
  #### Closure types <a id="expr.prim.lambda.closure">[[expr.prim.lambda.closure]]</a>
 
147
  call operator template is the *requires-clause* immediately following
148
  `<` *template-parameter-list* `>`, if any. The trailing
149
  *requires-clause* of the function call operator or operator template is
150
  the *requires-clause* of the *lambda-declarator*, if any.
151
 
152
+ [*Note 2*: The function call operator template for a generic lambda can
153
+ be an abbreviated function template [[dcl.fct]]. — *end note*]
154
 
155
  [*Example 1*:
156
 
157
  ``` cpp
158
  auto glambda = [](auto a, auto&& b) { return a < b; };
159
  bool b = glambda(3, 3.14); // OK
160
 
161
  auto vglambda = [](auto printer) {
162
+ return [=](auto&& ... ts) { // OK, ts is a function parameter pack
163
  printer(std::forward<decltype(ts)>(ts)...);
164
 
165
  return [=]() {
166
  printer(ts ...);
167
  };
168
  };
169
  };
170
  auto p = vglambda( [](auto v1, auto v2, auto v3)
171
  { std::cout << v1 << v2 << v3; } );
172
+ auto q = p(1, 'a', 3.14); // OK, outputs 1a3.14
173
+ q(); // OK, outputs 1a3.14
174
+
175
+ auto fact = [](this auto self, int n) -> int { // OK, explicit object parameter
176
+ return (n <= 1) ? 1 : n * self(n-1);
177
+ };
178
+ std::cout << fact(5); // OK, outputs 120
179
+ ```
180
+
181
+ — *end example*]
182
+
183
+ Given a lambda with a *lambda-capture*, the type of the explicit object
184
+ parameter, if any, of the lambda’s function call operator (possibly
185
+ instantiated from a function call operator template) shall be either:
186
+
187
+ - the closure type,
188
+ - a class type derived from the closure type, or
189
+ - a reference to a possibly cv-qualified such type.
190
+
191
+ [*Example 2*:
192
+
193
+ ``` cpp
194
+ struct C {
195
+ template <typename T>
196
+ C(T);
197
+ };
198
+
199
+ void func(int i) {
200
+ int x = [=](this auto&&) { return i; }(); // OK
201
+ int y = [=](this C) { return i; }(); // error
202
+ int z = [](this C) { return 42; }(); // OK
203
+ }
204
  ```
205
 
206
  — *end example*]
207
 
208
+ The function call operator or operator template is a static member
209
+ function or static member function template [[class.static.mfct]] if the
210
+ *lambda-expression*’s *parameter-declaration-clause* is followed by
211
+ `static`. Otherwise, it is a non-static member function or member
212
+ function template [[class.mfct.non.static]] that is declared `const`
213
+ [[class.mfct.non.static]] if and only if the *lambda-expression*’s
214
+ *parameter-declaration-clause* is not followed by `mutable` and the
215
+ *lambda-declarator* does not contain an explicit object parameter. It is
216
  neither virtual nor declared `volatile`. Any *noexcept-specifier*
217
  specified on a *lambda-expression* applies to the corresponding function
218
  call operator or operator template. An *attribute-specifier-seq* in a
219
  *lambda-declarator* appertains to the type of the corresponding function
220
+ call operator or operator template. An *attribute-specifier-seq* in a
221
+ *lambda-expression* preceding a *lambda-declarator* appertains to the
222
+ corresponding function call operator or operator template. The function
223
+ call operator or any given operator template specialization is a
224
+ constexpr function if either the corresponding *lambda-expression*'s
225
+ *parameter-declaration-clause* is followed by `constexpr` or
226
+ `consteval`, or it is constexpr-suitable [[dcl.constexpr]]. It is an
227
  immediate function [[dcl.constexpr]] if the corresponding
228
  *lambda-expression*'s *parameter-declaration-clause* is followed by
229
  `consteval`.
230
 
231
+ [*Example 3*:
 
 
 
232
 
233
  ``` cpp
234
  auto ID = [](auto a) { return a; };
235
  static_assert(ID(3) == 3); // OK
236
 
 
241
  static_assert(ID(NonLiteral{3}).n == 3); // error
242
  ```
243
 
244
  — *end example*]
245
 
246
+ [*Example 4*:
247
 
248
  ``` cpp
249
  auto monoid = [](auto v) { return [=] { return v; }; };
250
  auto add = [](auto m1) constexpr {
251
  auto ret = m1();
 
270
  static_assert(add(one)(one)() == monoid(2)()); // OK
271
  ```
272
 
273
  — *end example*]
274
 
275
+ [*Note 3*:
276
 
277
+ The function call operator or operator template can be constrained
278
  [[temp.constr.decl]] by a *type-constraint* [[temp.param]], a
279
  *requires-clause* [[temp.pre]], or a trailing *requires-clause*
280
  [[dcl.decl]].
281
 
282
+ [*Example 5*:
283
 
284
  ``` cpp
285
  template <typename T> concept C1 = ...;
286
  template <std::size_t N> concept C2 = ...;
287
  template <typename A, typename B> concept C3 = ...;
 
302
  *lambda-capture* whose constraints (if any) are satisfied has a
303
  conversion function to pointer to function with C++ language linkage
304
  [[dcl.link]] having the same parameter and return types as the closure
305
  type’s function call operator. The conversion is to “pointer to
306
  `noexcept` function” if the function call operator has a non-throwing
307
+ exception specification. If the function call operator is a static
308
+ member function, then the value returned by this conversion function is
309
+ the address of the function call operator. Otherwise, the value returned
310
+ by this conversion function is the address of a function `F` that, when
311
+ invoked, has the same effect as invoking the closure type’s function
312
+ call operator on a default-constructed instance of the closure type. `F`
313
+ is a constexpr function if the function call operator is a constexpr
314
+ function and is an immediate function if the function call operator is
315
+ an immediate function.
316
 
317
  For a generic lambda with no *lambda-capture*, the closure type has a
318
  conversion function template to pointer to function. The conversion
319
  function template has the same invented template parameter list, and the
320
  pointer to function has the same parameter types, as the function call
321
  operator template. The return type of the pointer to function shall
322
  behave as if it were a *decltype-specifier* denoting the return type of
323
  the corresponding function call operator template specialization.
324
 
325
+ [*Note 4*:
326
 
327
  If the generic lambda has no *trailing-return-type* or the
328
  *trailing-return-type* contains a placeholder type, return type
329
  deduction of the corresponding function call operator template
330
  specialization has to be done. The corresponding specialization is that
 
356
  };
357
  ```
358
 
359
  — *end note*]
360
 
361
+ [*Example 6*:
362
 
363
  ``` cpp
364
  void f1(int (*)(int)) { }
365
  void f2(char (*)(int)) { }
366
 
 
372
 
373
  auto glambda = [](auto a) { return a; };
374
  f1(glambda); // OK
375
  f2(glambda); // error: ID is not convertible
376
  g(glambda); // error: ambiguous
377
+ h(glambda); // OK, calls #3 since it is convertible from ID
378
  int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
379
  ```
380
 
381
  — *end example*]
382
 
383
+ If the function call operator template is a static member function
384
+ template, then the value returned by any given specialization of this
385
+ conversion function template is the address of the corresponding
386
+ function call operator template specialization. Otherwise, the value
387
+ returned by any given specialization of this conversion function
388
+ template is the address of a function `F` that, when invoked, has the
389
+ same effect as invoking the generic lambda’s corresponding function call
390
+ operator template specialization on a default-constructed instance of
391
+ the closure type. `F` is a constexpr function if the corresponding
392
+ specialization is a constexpr function and `F` is an immediate function
393
+ if the function call operator template specialization is an immediate
394
+ function.
395
 
396
+ [*Note 5*: This will result in the implicit instantiation of the
397
  generic lambda’s body. The instantiated generic lambda’s return type and
398
  parameter types are required to match the return type and parameter
399
  types of the pointer to function. — *end note*]
400
 
401
+ [*Example 7*:
402
 
403
  ``` cpp
404
  auto GL = [](auto a) { std::cout << a; return a; };
405
+ int (*GL_int)(int) = GL; // OK, through conversion function template
406
+ GL_int(3); // OK, same as GL(3)
407
  ```
408
 
409
  — *end example*]
410
 
411
  The conversion function or conversion function template is public,
412
  constexpr, non-virtual, non-explicit, const, and has a non-throwing
413
  exception specification [[except.spec]].
414
 
415
+ [*Example 8*:
416
 
417
  ``` cpp
418
  auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
419
  auto C = [](auto a) { return a; };
420
 
 
426
  ```
427
 
428
  — *end example*]
429
 
430
  The *lambda-expression*’s *compound-statement* yields the
431
+ *function-body* [[dcl.fct.def]] of the function call operator, but it is
432
+ not within the scope of the closure type.
 
 
 
 
433
 
434
+ [*Example 9*:
435
 
436
  ``` cpp
437
  struct S1 {
438
  int x, y;
439
  int operator()(int);
 
458
  constructor and a defaulted move constructor [[class.copy.ctor]]. It has
459
  a deleted copy assignment operator if the *lambda-expression* has a
460
  *lambda-capture* and defaulted copy and move assignment operators
461
  otherwise [[class.copy.assign]].
462
 
463
+ [*Note 6*: These special member functions are implicitly defined as
464
+ usual, which can result in them being defined as deleted. — *end note*]
465
 
466
  The closure type associated with a *lambda-expression* has an
467
  implicitly-declared destructor [[class.dtor]].
468
 
469
  A member of a closure type shall not be explicitly instantiated
 
509
  init-capture:
510
  '...'ₒₚₜ identifier initializer
511
  '&' '...'ₒₚₜ identifier initializer
512
  ```
513
 
514
+ The body of a *lambda-expression* may refer to local entities of
515
+ enclosing block scopes by capturing those entities, as described below.
 
516
 
517
  If a *lambda-capture* includes a *capture-default* that is `&`, no
518
  identifier in a *simple-capture* of that *lambda-capture* shall be
519
  preceded by `&`. If a *lambda-capture* includes a *capture-default* that
520
  is `=`, each *simple-capture* of that *lambda-capture* shall be of the
 
547
  *simple-capture* in its *lambda-introducer* unless its innermost
548
  enclosing scope is a block scope [[basic.scope.block]] or it appears
549
  within a default member initializer and its innermost enclosing scope is
550
  the corresponding class scope [[basic.scope.class]].
551
 
552
+ The *identifier* in a *simple-capture* shall denote a local entity
553
+ [[basic.lookup.unqual]], [[basic.pre]]. The *simple-capture*s `this` and
 
554
  `* this` denote the local entity `*this`. An entity that is designated
555
  by a *simple-capture* is said to be *explicitly captured*.
556
 
557
+ If an *identifier* in a *capture* appears as the *declarator-id* of a
558
+ parameter of the *lambda-declarator*s *parameter-declaration-clause* or
559
+ as the name of a template parameter of the *lambda-expression*’s
560
+ *template-parameter-list*, the program is ill-formed.
561
 
562
  [*Example 2*:
563
 
564
  ``` cpp
565
  void f() {
566
  int x = 0;
567
+ auto g = [x](int x) { return 0; }; // error: parameter and capture have the same name
568
+ auto h = [y = 0]<typename y>(y) { return 0; }; // error: template parameter and capture
569
+ // have the same name
570
  }
571
  ```
572
 
573
  — *end example*]
574
 
575
+ An *init-capture* inhabits the lambda scope [[basic.scope.lambda]] of
576
+ the *lambda-expression*. An *init-capture* without ellipsis behaves as
577
+ if it declares and explicitly captures a variable of the form “`auto`
578
+ *init-capture* `;`”, except that:
579
 
580
  - if the capture is by copy (see below), the non-static data member
581
  declared for the capture and the variable are treated as two different
582
  ways of referring to the same object, which has the lifetime of the
583
  non-static data member, and no additional copy and destruction is
 
596
  auto y = [&r = x, x = x+1]()->int {
597
  r += 2;
598
  return x+2;
599
  }(); // Updates ::x to 6, and initializes y to 7.
600
 
601
+ auto z = [a = 42](int a) { return 1; }; // error: parameter and conceptual local variable have the same name
602
+ auto counter = [i=0]() mutable -> decltype(i) { // OK, returns int
603
+ return i++;
604
+ };
605
  ```
606
 
607
  — *end example*]
608
 
609
  For the purposes of lambda capture, an expression potentially references
 
617
  *id-expression*. — *end note*]
618
  - A `this` expression potentially references `*this`.
619
  - A *lambda-expression* potentially references the local entities named
620
  by its *simple-capture*s.
621
 
622
+ If an expression potentially references a local entity within a scope in
623
+ which it is odr-usable [[basic.def.odr]], and the expression would be
624
+ potentially evaluated if the effect of any enclosing `typeid`
625
  expressions [[expr.typeid]] were ignored, the entity is said to be
626
  *implicitly captured* by each intervening *lambda-expression* with an
627
  associated *capture-default* that does not explicitly capture it. The
628
  implicit capture of `*this` is deprecated when the *capture-default* is
629
  `=`; see [[depr.capture.this]].
 
634
  void f(int, const int (&)[2] = {}); // #1
635
  void f(const int&, const int (&)[1]); // #2
636
  void test() {
637
  const int x = 17;
638
  auto g = [](auto a) {
639
+ f(x); // OK, calls #1, does not capture x
640
  };
641
 
642
  auto g1 = [=](auto a) {
643
+ f(x); // OK, calls #1, captures x
644
  };
645
 
646
  auto g2 = [=](auto a) {
647
  int selector[sizeof(a) == 1 ? 1 : 2]{};
648
+ f(x, selector); // OK, captures x, can call #1 or #2
649
  };
650
 
651
  auto g3 = [=](auto a) {
652
  typeid(a + x); // captures x regardless of whether a + x is an unevaluated operand
653
  };
654
  }
655
  ```
656
 
657
+ Within `g1`, an implementation can optimize away the capture of `x` as
658
  it is not odr-used.
659
 
660
  — *end example*]
661
 
662
  [*Note 4*:
663
 
664
  The set of captured entities is determined syntactically, and entities
665
+ are implicitly captured even if the expression denoting a local entity
666
+ is within a discarded statement [[stmt.if]].
667
 
668
  [*Example 5*:
669
 
670
  ``` cpp
671
  template<bool B>
 
681
  — *end example*]
682
 
683
  — *end note*]
684
 
685
  An entity is *captured* if it is captured explicitly or implicitly. An
686
+ entity captured by a *lambda-expression* is odr-used [[term.odr.use]] by
687
+ the *lambda-expression*.
688
 
689
  [*Note 5*: As a consequence, if a *lambda-expression* explicitly
690
  captures an entity that is not odr-usable, the program is ill-formed
691
  [[basic.def.odr]]. — *end note*]
692
 
 
696
  void f1(int i) {
697
  int const N = 20;
698
  auto m1 = [=]{
699
  int const M = 30;
700
  auto m2 = [i]{
701
+ int x[N][M]; // OK, N and M are not odr-used
702
+ x[0][0] = i; // OK, i is explicitly captured by m2 and implicitly captured by m1
703
  };
704
  };
705
  struct s1 {
706
  int f;
707
  void work(int n) {
708
  int m = n*n;
709
  int j = 40;
710
  auto m3 = [this,m] {
711
  auto m4 = [&,j] { // error: j not odr-usable due to intervening lambda m3
712
  int x = n; // error: n is odr-used but not odr-usable due to intervening lambda m3
713
+ x += m; // OK, m implicitly captured by m4 and explicitly captured by m3
714
  x += i; // error: i is odr-used but not odr-usable
715
  // due to intervening function and class scopes
716
+ x += f; // OK, this captured implicitly by m4 and explicitly by m3
717
  };
718
  };
719
  }
720
  };
721
  }
 
778
  referenced function type if the entity is a reference to a function, or
779
  the type of the corresponding captured entity otherwise. A member of an
780
  anonymous union shall not be captured by copy.
781
 
782
  Every *id-expression* within the *compound-statement* of a
783
+ *lambda-expression* that is an odr-use [[term.odr.use]] of an entity
784
  captured by copy is transformed into an access to the corresponding
785
  unnamed data member of the closure type.
786
 
787
  [*Note 7*: An *id-expression* that is not an odr-use refers to the
788
  original entity, never to a member of the closure type. However, such an
 
798
  ``` cpp
799
  void f(const int*);
800
  void g() {
801
  const int N = 10;
802
  [=] {
803
+ int arr[N]; // OK, not an odr-use, refers to automatic variable
804
+ f(&N); // OK, causes N to be captured; &N points to
805
  // the corresponding member of the closure type
806
  };
807
  }
808
  ```
809
 
 
851
 
852
  If a *lambda-expression* `m2` captures an entity and that entity is
853
  captured by an immediately enclosing *lambda-expression* `m1`, then
854
  `m2`’s capture is transformed as follows:
855
 
856
+ - If `m1` captures the entity by copy, `m2` captures the corresponding
857
+ non-static data member of `m1`’s closure type; if `m1` is not
858
+ `mutable`, the non-static data member is considered to be
859
+ const-qualified.
860
+ - If `m1` captures the entity by reference, `m2` captures the same
861
  entity captured by `m1`.
862
 
863
  [*Example 11*:
864
 
865
  The nested *lambda-expression*s and invocations below will output
 
900
  corresponding *lambda-expression* after the lifetime of the entity has
901
  ended is likely to result in undefined behavior. — *end note*]
902
 
903
  A *simple-capture* containing an ellipsis is a pack expansion
904
  [[temp.variadic]]. An *init-capture* containing an ellipsis is a pack
905
+ expansion that declares an *init-capture* pack [[temp.variadic]].
 
906
 
907
  [*Example 12*:
908
 
909
  ``` cpp
910
  template<class... Args>