From Jason Turner

[expr.prim]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp5xfb2kww/{from.md → to.md} +768 -290
tmp/tmp5xfb2kww/{from.md → to.md} RENAMED
@@ -1,28 +1,31 @@
1
  ## Primary expressions <a id="expr.prim">[[expr.prim]]</a>
2
 
3
  ``` bnf
4
  primary-expression:
5
  literal
6
- 'this'
7
  '(' expression ')'
8
  id-expression
9
  lambda-expression
10
  fold-expression
 
11
  ```
12
 
13
  ### Literals <a id="expr.prim.literal">[[expr.prim.literal]]</a>
14
 
15
- A *literal* is a primary expression. Its type depends on its form (
16
- [[lex.literal]]). A string literal is an lvalue; all other literals are
17
- prvalues.
 
 
18
 
19
  ### This <a id="expr.prim.this">[[expr.prim.this]]</a>
20
 
21
  The keyword `this` names a pointer to the object for which a non-static
22
- member function ([[class.this]]) is invoked or a non-static data
23
- member’s initializer ([[class.mem]]) is evaluated.
24
 
25
  If a declaration declares a member function or member function template
26
  of a class `X`, the expression `this` is a prvalue of type “pointer to
27
  *cv-qualifier-seq* `X`” between the optional *cv-qualifier-seq* and the
28
  end of the *function-definition*, *member-declarator*, or *declarator*.
@@ -32,16 +35,15 @@ its type and value category are defined within a static member function
32
  as they are within a non-static member function).
33
 
34
  [*Note 1*: This is because declaration matching does not occur until
35
  the complete declarator is known. — *end note*]
36
 
37
- Unlike the object expression in other contexts, `*this` is not required
38
- to be of complete type for purposes of class member access (
39
- [[expr.ref]]) outside the member function body.
40
 
41
- [*Note 2*: Only class members declared prior to the declaration are
42
- visible. *end note*]
 
43
 
44
  [*Example 1*:
45
 
46
  ``` cpp
47
  struct A {
@@ -52,15 +54,16 @@ struct A {
52
  template auto A::f(int t) -> decltype(t + g());
53
  ```
54
 
55
  — *end example*]
56
 
57
- Otherwise, if a *member-declarator* declares a non-static data member (
58
- [[class.mem]]) of a class `X`, the expression `this` is a prvalue of
59
- type “pointer to `X`” within the optional default member initializer (
60
- [[class.mem]]). It shall not appear elsewhere in the
61
- *member-declarator*.
 
62
 
63
  The expression `this` shall not appear in any other context.
64
 
65
  [*Example 2*:
66
 
@@ -82,14 +85,13 @@ class Outer {
82
  — *end example*]
83
 
84
  ### Parentheses <a id="expr.prim.paren">[[expr.prim.paren]]</a>
85
 
86
  A parenthesized expression `(E)` is a primary expression whose type,
87
- value, and value category are identical to those of `E`. The
88
- parenthesized expression can be used in exactly the same contexts as
89
- those where `E` can be used, and with the same meaning, except as
90
- otherwise indicated.
91
 
92
  ### Names <a id="expr.prim.id">[[expr.prim.id]]</a>
93
 
94
  ``` bnf
95
  id-expression:
@@ -97,20 +99,20 @@ id-expression:
97
  qualified-id
98
  ```
99
 
100
  An *id-expression* is a restricted form of a *primary-expression*.
101
 
102
- [*Note 1*: An *id-expression* can appear after `.` and `->` operators (
103
- [[expr.ref]]). — *end note*]
104
 
105
  An *id-expression* that denotes a non-static data member or non-static
106
  member function of a class can only be used:
107
 
108
- - as part of a class member access ([[expr.ref]]) in which the object
109
- expression refers to the member’s class[^4] or a class derived from
110
  that class, or
111
- - to form a pointer to member ([[expr.unary.op]]), or
112
  - if that *id-expression* denotes a non-static data member and it
113
  appears in an unevaluated operand.
114
  \[*Example 1*:
115
  ``` cpp
116
  struct S {
@@ -120,124 +122,234 @@ member function of a class can only be used:
120
  int j = sizeof(S::m + 42); // OK
121
  ```
122
 
123
  — *end example*]
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  #### Unqualified names <a id="expr.prim.id.unqual">[[expr.prim.id.unqual]]</a>
126
 
127
  ``` bnf
128
  unqualified-id:
129
  identifier
130
  operator-function-id
131
  conversion-function-id
132
  literal-operator-id
133
- '~' class-name
134
  '~' decltype-specifier
135
  template-id
136
  ```
137
 
138
- An *identifier* is an *id-expression* provided it has been suitably
139
- declared (Clause  [[dcl.dcl]]).
 
 
140
 
141
  [*Note 1*: For *operator-function-id*s, see  [[over.oper]]; for
142
  *conversion-function-id*s, see  [[class.conv.fct]]; for
143
  *literal-operator-id*s, see  [[over.literal]]; for *template-id*s, see 
144
- [[temp.names]]. A *class-name* or *decltype-specifier* prefixed by `~`
145
- denotes a destructor; see  [[class.dtor]]. Within the definition of a
146
- non-static member function, an *identifier* that names a non-static
147
- member is transformed to a class member access expression (
148
- [[class.mfct.non-static]]). — *end note*]
149
-
150
- The type of the expression is the type of the *identifier*. The result
151
- is the entity denoted by the identifier. The expression is an lvalue if
152
- the entity is a function, variable, or data member and a prvalue
153
- otherwise; it is a bit-field if the identifier designates a bit-field (
154
- [[dcl.struct.bind]]).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
  #### Qualified names <a id="expr.prim.id.qual">[[expr.prim.id.qual]]</a>
157
 
158
  ``` bnf
159
  qualified-id:
160
- nested-name-specifier 'template'ₒₚₜ unqualified-id
161
  ```
162
 
163
  ``` bnf
164
  nested-name-specifier:
165
  '::'
166
  type-name '::'
167
  namespace-name '::'
168
  decltype-specifier '::'
169
  nested-name-specifier identifier '::'
170
- nested-name-specifier 'template'ₒₚₜ simple-template-id '::'
171
  ```
172
 
173
  The type denoted by a *decltype-specifier* in a *nested-name-specifier*
174
  shall be a class or enumeration type.
175
 
176
  A *nested-name-specifier* that denotes a class, optionally followed by
177
- the keyword `template` ([[temp.names]]), and then followed by the name
178
- of a member of either that class ([[class.mem]]) or one of its base
179
- classes (Clause  [[class.derived]]), is a *qualified-id*; 
180
- [[class.qual]] describes name lookup for class members that appear in
181
- *qualified-id*s. The result is the member. The type of the result is the
182
- type of the member. The result is an lvalue if the member is a static
183
- member function or a data member and a prvalue otherwise.
184
 
185
  [*Note 1*: A class member can be referred to using a *qualified-id* at
186
- any point in its potential scope (
187
- [[basic.scope.class]]). — *end note*]
188
 
189
- Where *class-name* `::~` *class-name* is used, the two *class-name*s
190
- shall refer to the same class; this notation names the destructor (
191
- [[class.dtor]]). The form `~` *decltype-specifier* also denotes the
192
- destructor, but it shall not be used as the *unqualified-id* in a
193
- *qualified-id*.
194
-
195
- [*Note 2*: A *typedef-name* that names a class is a *class-name* (
196
- [[class.name]]). — *end note*]
197
 
198
  The *nested-name-specifier* `::` names the global namespace. A
199
- *nested-name-specifier* that names a namespace ([[basic.namespace]]),
200
- optionally followed by the keyword `template` ([[temp.names]]), and
201
- then followed by the name of a member of that namespace (or the name of
202
- a member of a namespace made visible by a *using-directive*), is a
203
  *qualified-id*;  [[namespace.qual]] describes name lookup for namespace
204
  members that appear in *qualified-id*s. The result is the member. The
205
  type of the result is the type of the member. The result is an lvalue if
206
- the member is a function or a variable and a prvalue otherwise.
 
207
 
208
- A *nested-name-specifier* that denotes an enumeration ([[dcl.enum]]),
209
  followed by the name of an enumerator of that enumeration, is a
210
  *qualified-id* that refers to the enumerator. The result is the
211
  enumerator. The type of the result is the type of the enumeration. The
212
  result is a prvalue.
213
 
214
  In a *qualified-id*, if the *unqualified-id* is a
215
- *conversion-function-id*, its *conversion-type-id* shall denote the same
216
- type in both the context in which the entire *qualified-id* occurs and
217
- in the context of the class denoted by the *nested-name-specifier*.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
219
  ### Lambda expressions <a id="expr.prim.lambda">[[expr.prim.lambda]]</a>
220
 
221
  ``` bnf
222
  lambda-expression:
223
  lambda-introducer lambda-declaratorₒₚₜ compound-statement
 
224
  ```
225
 
226
  ``` bnf
227
  lambda-introducer:
228
  '[' lambda-captureₒₚₜ ']'
229
  ```
230
 
231
  ``` bnf
232
  lambda-declarator:
233
  '(' parameter-declaration-clause ')' decl-specifier-seqₒₚₜ
234
- noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ
235
  ```
236
 
237
- Lambda expressions provide a concise way to create simple function
238
- objects.
239
 
240
  [*Example 1*:
241
 
242
  ``` cpp
243
  #include <algorithm>
@@ -248,24 +360,20 @@ void abssort(float* x, unsigned N) {
248
  ```
249
 
250
  — *end example*]
251
 
252
  A *lambda-expression* is a prvalue whose result object is called the
253
- *closure object*. A *lambda-expression* shall not appear in an
254
- unevaluated operand (Clause  [[expr]]), in a *template-argument*, in an
255
- *alias-declaration*, in a typedef declaration, or in the declaration of
256
- a function or function template outside its function body and default
257
- arguments.
258
 
259
- [*Note 1*: The intention is to prevent lambdas from appearing in a
260
- signature. — *end note*]
261
-
262
- [*Note 2*: A closure object behaves like a function object (
263
- [[function.objects]]). — *end note*]
264
 
265
  In the *decl-specifier-seq* of the *lambda-declarator*, each
266
- *decl-specifier* shall either be `mutable` or `constexpr`.
 
 
 
267
 
268
  If a *lambda-expression* does not include a *lambda-declarator*, it is
269
  as if the *lambda-declarator* were `()`. The lambda return type is
270
  `auto`, which is replaced by the type specified by the
271
  *trailing-return-type* if provided and/or deduced from `return`
@@ -280,54 +388,63 @@ int j;
280
  auto x3 = []()->auto&& { return j; }; // OK: return type is int&
281
  ```
282
 
283
  — *end example*]
284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285
  #### Closure types <a id="expr.prim.lambda.closure">[[expr.prim.lambda.closure]]</a>
286
 
287
  The type of a *lambda-expression* (which is also the type of the closure
288
  object) is a unique, unnamed non-union class type, called the *closure
289
  type*, whose properties are described below.
290
 
291
  The closure type is declared in the smallest block scope, class scope,
292
  or namespace scope that contains the corresponding *lambda-expression*.
293
 
294
  [*Note 1*: This determines the set of namespaces and classes associated
295
- with the closure type ([[basic.lookup.argdep]]). The parameter types of
296
- a *lambda-declarator* do not affect these associated namespaces and
297
  classes. — *end note*]
298
 
299
- The closure type is not an aggregate type ([[dcl.init.aggr]]). An
300
  implementation may define the closure type differently from what is
301
  described below provided this does not alter the observable behavior of
302
  the program other than by changing:
303
 
304
  - the size and/or alignment of the closure type,
305
- - whether the closure type is trivially copyable (Clause  [[class]]),
306
- - whether the closure type is a standard-layout class (Clause 
307
- [[class]]), or
308
- - whether the closure type is a POD class (Clause  [[class]]).
309
 
310
  An implementation shall not add members of rvalue reference type to the
311
  closure type.
312
 
313
- The closure type for a non-generic *lambda-expression* has a public
314
- inline function call operator ([[over.call]]) whose parameters and
 
315
  return type are described by the *lambda-expression*’s
316
- *parameter-declaration-clause* and *trailing-return-type* respectively.
317
- For a generic lambda, the closure type has a public inline function call
318
- operator member template ([[temp.mem]]) whose *template-parameter-list*
319
- consists of one invented type *template-parameter* for each occurrence
320
- of `auto` in the lambda’s *parameter-declaration-clause*, in order of
321
- appearance. The invented type *template-parameter* is a parameter pack
322
- if the corresponding *parameter-declaration* declares a function
323
- parameter pack ([[dcl.fct]]). The return type and function parameters
324
- of the function call operator template are derived from the
325
- *lambda-expression*'s *trailing-return-type* and
326
- *parameter-declaration-clause* by replacing each occurrence of `auto` in
327
- the *decl-specifier*s of the *parameter-declaration-clause* with the
328
- name of the corresponding invented *template-parameter*.
329
 
330
  [*Example 1*:
331
 
332
  ``` cpp
333
  auto glambda = [](auto a, auto&& b) { return a < b; };
@@ -358,14 +475,17 @@ specified on a *lambda-expression* applies to the corresponding function
358
  call operator or operator template. An *attribute-specifier-seq* in a
359
  *lambda-declarator* appertains to the type of the corresponding function
360
  call operator or operator template. The function call operator or any
361
  given operator template specialization is a constexpr function if either
362
  the corresponding *lambda-expression*'s *parameter-declaration-clause*
363
- is followed by `constexpr`, or it satisfies the requirements for a
364
- constexpr function ([[dcl.constexpr]]).
 
 
 
365
 
366
- [*Note 2*: Names referenced in the *lambda-declarator* are looked up in
367
  the context in which the *lambda-expression* appears. — *end note*]
368
 
369
  [*Example 2*:
370
 
371
  ``` cpp
@@ -374,11 +494,11 @@ static_assert(ID(3) == 3); // OK
374
 
375
  struct NonLiteral {
376
  NonLiteral(int n) : n(n) { }
377
  int n;
378
  };
379
- static_assert(ID(NonLiteral{3}).n == 3); // ill-formed
380
  ```
381
 
382
  — *end example*]
383
 
384
  [*Example 3*:
@@ -402,35 +522,65 @@ static_assert(add(one)(zero)() == one()); // OK
402
  // Since two below is not declared constexpr, an evaluation of its constexpr member function call operator
403
  // cannot perform an lvalue-to-rvalue conversion on one of its subobjects (that represents its capture)
404
  // in a constant expression.
405
  auto two = monoid(2);
406
  assert(two() == 2); // OK, not a constant expression.
407
- static_assert(add(one)(one)() == two()); // ill-formed: two() is not a constant expression
408
  static_assert(add(one)(one)() == monoid(2)()); // OK
409
  ```
410
 
411
  — *end example*]
412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
413
  The closure type for a non-generic *lambda-expression* with no
414
- *lambda-capture* has a conversion function to pointer to function with
415
- C++language linkage ([[dcl.link]]) having the same parameter and return
416
- types as the closure type’s function call operator. The conversion is to
417
- “pointer to `noexcept` function if the function call operator has a
418
- non-throwing exception specification. The value returned by this
419
- conversion function is the address of a function `F` that, when invoked,
420
- has the same effect as invoking the closure type’s function call
421
- operator. `F` is a constexpr function if the function call operator is a
422
- constexpr function. For a generic lambda with no *lambda-capture*, the
423
- closure type has a conversion function template to pointer to function.
424
- The conversion function template has the same invented
425
- *template-parameter-list*, and the pointer to function has the same
426
- parameter types, as the function call operator template. The return type
427
- of the pointer to function shall behave as if it were a
428
- *decltype-specifier* denoting the return type of the corresponding
429
- function call operator template specialization.
430
 
431
- [*Note 3*:
 
 
 
 
 
 
 
 
432
 
433
  If the generic lambda has no *trailing-return-type* or the
434
  *trailing-return-type* contains a placeholder type, return type
435
  deduction of the corresponding function call operator template
436
  specialization has to be done. The corresponding specialization is that
@@ -462,11 +612,11 @@ struct Closure {
462
  };
463
  ```
464
 
465
  — *end note*]
466
 
467
- [*Example 4*:
468
 
469
  ``` cpp
470
  void f1(int (*)(int)) { }
471
  void f2(char (*)(int)) { }
472
 
@@ -487,19 +637,22 @@ int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
487
  — *end example*]
488
 
489
  The value returned by any given specialization of this conversion
490
  function template is the address of a function `F` that, when invoked,
491
  has the same effect as invoking the generic lambda’s corresponding
492
- function call operator template specialization. `F` is a constexpr
493
- function if the corresponding specialization is a constexpr function.
 
 
 
494
 
495
- [*Note 4*: This will result in the implicit instantiation of the
496
  generic lambda’s body. The instantiated generic lambda’s return type and
497
- parameter types shall match the return type and parameter types of the
498
- pointer to function. — *end note*]
499
 
500
- [*Example 5*:
501
 
502
  ``` cpp
503
  auto GL = [](auto a) { std::cout << a; return a; };
504
  int (*GL_int)(int) = GL; // OK: through conversion function template
505
  GL_int(3); // OK: same as GL(3)
@@ -507,37 +660,36 @@ GL_int(3); // OK: same as GL(3)
507
 
508
  — *end example*]
509
 
510
  The conversion function or conversion function template is public,
511
  constexpr, non-virtual, non-explicit, const, and has a non-throwing
512
- exception specification ([[except.spec]]).
513
 
514
- [*Example 6*:
515
 
516
  ``` cpp
517
  auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
518
  auto C = [](auto a) { return a; };
519
 
520
  static_assert(Fwd(C,3) == 3); // OK
521
 
522
  // No specialization of the function call operator template can be constexpr (due to the local static).
523
  auto NC = [](auto a) { static int s; return a; };
524
- static_assert(Fwd(NC,3) == 3); // ill-formed
525
  ```
526
 
527
  — *end example*]
528
 
529
  The *lambda-expression*’s *compound-statement* yields the
530
- *function-body* ([[dcl.fct.def]]) of the function call operator, but
531
- for purposes of name lookup ([[basic.lookup]]), determining the type
532
- and value of `this` ([[class.this]]) and transforming *id-expression*s
533
- referring to non-static class members into class member access
534
- expressions using `(*this)` ([[class.mfct.non-static]]), the
535
- *compound-statement* is considered in the context of the
536
- *lambda-expression*.
537
 
538
- [*Example 7*:
539
 
540
  ``` cpp
541
  struct S1 {
542
  int x, y;
543
  int operator()(int);
@@ -555,22 +707,26 @@ struct S1 {
555
  Further, a variable `__func__` is implicitly defined at the beginning of
556
  the *compound-statement* of the *lambda-expression*, with semantics as
557
  described in  [[dcl.fct.def.general]].
558
 
559
  The closure type associated with a *lambda-expression* has no default
560
- constructor and a deleted copy assignment operator. It has a defaulted
561
- copy constructor and a defaulted move constructor ([[class.copy]]).
 
 
 
 
562
 
563
- [*Note 5*: These special member functions are implicitly defined as
564
  usual, and might therefore be defined as deleted. — *end note*]
565
 
566
  The closure type associated with a *lambda-expression* has an
567
- implicitly-declared destructor ([[class.dtor]]).
568
 
569
- A member of a closure type shall not be explicitly instantiated (
570
- [[temp.explicit]]), explicitly specialized ([[temp.expl.spec]]), or
571
- named in a `friend` declaration ([[class.friend]]).
572
 
573
  #### Captures <a id="expr.prim.lambda.capture">[[expr.prim.lambda.capture]]</a>
574
 
575
  ``` bnf
576
  lambda-capture:
@@ -585,43 +741,43 @@ capture-default:
585
  '='
586
  ```
587
 
588
  ``` bnf
589
  capture-list:
590
- capture '...'ₒₚₜ
591
- capture-list ',' capture '...'ₒₚₜ
592
  ```
593
 
594
  ``` bnf
595
  capture:
596
  simple-capture
597
  init-capture
598
  ```
599
 
600
  ``` bnf
601
  simple-capture:
602
- identifier
603
- '&' identifier
604
- 'this'
605
- '* this'
606
  ```
607
 
608
  ``` bnf
609
  init-capture:
610
- identifier initializer
611
- '&' identifier initializer
612
  ```
613
 
614
  The body of a *lambda-expression* may refer to variables with automatic
615
  storage duration and the `*this` object (if any) of enclosing block
616
  scopes by capturing those entities, as described below.
617
 
618
  If a *lambda-capture* includes a *capture-default* that is `&`, no
619
  identifier in a *simple-capture* of that *lambda-capture* shall be
620
  preceded by `&`. If a *lambda-capture* includes a *capture-default* that
621
  is `=`, each *simple-capture* of that *lambda-capture* shall be of the
622
- form “`&` *identifier*” or “`* this`”.
623
 
624
  [*Note 1*: The form `[&,this]` is redundant but accepted for
625
  compatibility with ISO C++14. — *end note*]
626
 
627
  Ignoring appearances in *initializer*s of *init-capture*s, an identifier
@@ -631,66 +787,62 @@ or `this` shall not appear more than once in a *lambda-capture*.
631
 
632
  ``` cpp
633
  struct S2 { void f(int i); };
634
  void S2::f(int i) {
635
  [&, i]{ }; // OK
 
636
  [&, &i]{ }; // error: i preceded by & when & is the default
637
  [=, *this]{ }; // OK
638
- [=, this]{ }; // error: this when = is the default
639
  [i, i]{ }; // error: i repeated
640
  [this, *this]{ }; // error: this appears twice
641
  }
642
  ```
643
 
644
  — *end example*]
645
 
646
- A *lambda-expression* whose smallest enclosing scope is a block scope (
647
- [[basic.scope.block]]) is a *local lambda expression*; any other
648
- *lambda-expression* shall not have a *capture-default* or
649
- *simple-capture* in its *lambda-introducer*. The *reaching scope* of a
650
- local lambda expression is the set of enclosing scopes up to and
651
- including the innermost enclosing function and its parameters.
652
-
653
- [*Note 2*: This reaching scope includes any intervening
654
- *lambda-expression*s. — *end note*]
655
 
656
  The *identifier* in a *simple-capture* is looked up using the usual
657
- rules for unqualified name lookup ([[basic.lookup.unqual]]); each such
658
- lookup shall find an entity. An entity that is designated by a
659
- *simple-capture* is said to be *explicitly captured*, and shall be
660
- `*this` (when the *simple-capture* is “`this`” or “`* this`”) or a
661
- variable with automatic storage duration declared in the reaching scope
662
- of the local lambda expression.
663
 
664
  If an *identifier* in a *simple-capture* appears as the *declarator-id*
665
  of a parameter of the *lambda-declarator*'s
666
  *parameter-declaration-clause*, the program is ill-formed.
667
 
668
  [*Example 2*:
669
 
670
  ``` cpp
671
  void f() {
672
  int x = 0;
673
- auto g = [x](int x) { return 0; } // error: parameter and simple-capture have the same name
674
  }
675
  ```
676
 
677
  — *end example*]
678
 
679
- An *init-capture* behaves as if it declares and explicitly captures a
680
- variable of the form “`auto` *init-capture* `;`” whose declarative
681
- region is the *lambda-expression*’s *compound-statement*, except that:
 
682
 
683
  - if the capture is by copy (see below), the non-static data member
684
  declared for the capture and the variable are treated as two different
685
  ways of referring to the same object, which has the lifetime of the
686
  non-static data member, and no additional copy and destruction is
687
  performed, and
688
  - if the capture is by reference, the variable’s lifetime ends when the
689
  closure object’s lifetime ends.
690
 
691
- [*Note 3*: This enables an *init-capture* like “`x = std::move(x)`”;
692
  the second “`x`” must bind to a declaration in the surrounding
693
  context. — *end note*]
694
 
695
  [*Example 3*:
696
 
@@ -699,71 +851,100 @@ int x = 4;
699
  auto y = [&r = x, x = x+1]()->int {
700
  r += 2;
701
  return x+2;
702
  }(); // Updates ::x to 6, and initializes y to 7.
703
 
704
- auto z = [a = 42](int a) { return 1; } // error: parameter and local variable have the same name
705
  ```
706
 
707
  — *end example*]
708
 
709
- A *lambda-expression* with an associated *capture-default* that does not
710
- explicitly capture `*this` or a variable with automatic storage duration
711
- (this excludes any *id-expression* that has been found to refer to an
712
- *init-capture*'s associated non-static data member), is said to
713
- *implicitly capture* the entity (i.e., `*this` or a variable) if the
714
- *compound-statement*:
715
 
716
- - odr-uses ([[basic.def.odr]]) the entity (in the case of a variable),
717
- - odr-uses ([[basic.def.odr]]) `this` (in the case of the object
718
- designated by `*this`), or
719
- - names the entity in a potentially-evaluated expression (
720
- [[basic.def.odr]]) where the enclosing full-expression depends on a
721
- generic lambda parameter declared within the reaching scope of the
722
- *lambda-expression*.
 
 
 
 
 
 
 
 
 
 
 
723
 
724
  [*Example 4*:
725
 
726
  ``` cpp
727
- void f(int, const int (&)[2] = {}) { } // #1
728
- void f(const int&, const int (&)[1]) { } // #2
729
  void test() {
730
  const int x = 17;
731
  auto g = [](auto a) {
732
  f(x); // OK: calls #1, does not capture x
733
  };
734
 
 
 
 
 
735
  auto g2 = [=](auto a) {
736
  int selector[sizeof(a) == 1 ? 1 : 2]{};
737
- f(x, selector); // OK: is a dependent expression, so captures x
 
 
 
 
738
  };
739
  }
740
  ```
741
 
 
 
 
742
  — *end example*]
743
 
744
- All such implicitly captured entities shall be declared within the
745
- reaching scope of the lambda expression.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
746
 
747
- [*Note 4*: The implicit capture of an entity by a nested
748
- *lambda-expression* can cause its implicit capture by the containing
749
- *lambda-expression* (see below). Implicit odr-uses of `this` can result
750
- in implicit capture. — *end note*]
751
 
752
  An entity is *captured* if it is captured explicitly or implicitly. An
753
- entity captured by a *lambda-expression* is odr-used (
754
- [[basic.def.odr]]) in the scope containing the *lambda-expression*. If
755
- `*this` is captured by a local lambda expression, its nearest enclosing
756
- function shall be a non-static member function. If a *lambda-expression*
757
- or an instantiation of the function call operator template of a generic
758
- lambda odr-uses ([[basic.def.odr]]) `this` or a variable with automatic
759
- storage duration from its reaching scope, that entity shall be captured
760
- by the *lambda-expression*. If a *lambda-expression* captures an entity
761
- and that entity is not defined or captured in the immediately enclosing
762
- lambda expression or function, the program is ill-formed.
763
 
764
- [*Example 5*:
 
 
 
 
765
 
766
  ``` cpp
767
  void f1(int i) {
768
  int const N = 20;
769
  auto m1 = [=]{
@@ -777,14 +958,15 @@ void f1(int i) {
777
  int f;
778
  void work(int n) {
779
  int m = n*n;
780
  int j = 40;
781
  auto m3 = [this,m] {
782
- auto m4 = [&,j] { // error: j not captured by m3
783
- int x = n; // error: n implicitly captured by m4 but not captured by m3
784
  x += m; // OK: m implicitly captured by m4 and explicitly captured by m3
785
- x += i; // error: i is outside of the reaching scope
 
786
  x += f; // OK: this captured implicitly by m4 and explicitly by m3
787
  };
788
  };
789
  }
790
  };
@@ -794,11 +976,11 @@ struct s2 {
794
  double ohseven = .007;
795
  auto f() {
796
  return [this] {
797
  return [*this] {
798
  return ohseven; // OK
799
- }
800
  }();
801
  }
802
  auto g() {
803
  return [] {
804
  return [*this] { }; // error: *this not captured by outer lambda-expression
@@ -807,23 +989,30 @@ struct s2 {
807
  };
808
  ```
809
 
810
  — *end example*]
811
 
812
- A *lambda-expression* appearing in a default argument shall not
813
- implicitly or explicitly capture any entity.
 
 
 
 
 
814
 
815
- [*Example 6*:
816
 
817
  ``` cpp
818
  void f2() {
819
  int i = 1;
820
- void g1(int = ([i]{ return i; })()); // ill-formed
821
- void g2(int = ([i]{ return 0; })()); // ill-formed
822
- void g3(int = ([=]{ return i; })()); // ill-formed
823
  void g4(int = ([=]{ return 0; })()); // OK
824
  void g5(int = ([]{ return sizeof i; })()); // OK
 
 
825
  }
826
  ```
827
 
828
  — *end example*]
829
 
@@ -841,36 +1030,24 @@ the entity is a reference to an object, an lvalue reference to the
841
  referenced function type if the entity is a reference to a function, or
842
  the type of the corresponding captured entity otherwise. A member of an
843
  anonymous union shall not be captured by copy.
844
 
845
  Every *id-expression* within the *compound-statement* of a
846
- *lambda-expression* that is an odr-use ([[basic.def.odr]]) of an entity
847
  captured by copy is transformed into an access to the corresponding
848
  unnamed data member of the closure type.
849
 
850
- [*Note 5*: An *id-expression* that is not an odr-use refers to the
851
- original entity, never to a member of the closure type. Furthermore,
852
- such an *id-expression* does not cause the implicit capture of the
853
  entity. — *end note*]
854
 
855
- If `*this` is captured by copy, each odr-use of `this` is transformed
856
- into a pointer to the corresponding unnamed data member of the closure
857
- type, cast ([[expr.cast]]) to the type of `this`.
858
 
859
- [*Note 6*: The cast ensures that the transformed expression is a
860
- prvalue. — *end note*]
861
-
862
- An *id-expression* within the *compound-statement* of a
863
- *lambda-expression* that is an odr-use of a reference captured by
864
- reference refers to the entity to which the captured reference is bound
865
- and not to the captured reference.
866
-
867
- [*Note 7*: The validity of such captures is determined by the lifetime
868
- of the object to which the reference refers, not by the lifetime of the
869
- reference itself. — *end note*]
870
-
871
- [*Example 7*:
872
 
873
  ``` cpp
874
  void f(const int*);
875
  void g() {
876
  const int N = 10;
@@ -878,27 +1055,21 @@ void g() {
878
  int arr[N]; // OK: not an odr-use, refers to automatic variable
879
  f(&N); // OK: causes N to be captured; &N points to
880
  // the corresponding member of the closure type
881
  };
882
  }
883
- auto h(int &r) {
884
- return [&] {
885
- ++r; // Valid after h returns if the lifetime of the
886
- // object to which r is bound has not ended
887
- };
888
- }
889
  ```
890
 
891
  — *end example*]
892
 
893
  An entity is *captured by reference* if it is implicitly or explicitly
894
  captured but not captured by copy. It is unspecified whether additional
895
  unnamed non-static data members are declared in the closure type for
896
  entities captured by reference. If declared, such non-static data
897
  members shall be of literal type.
898
 
899
- [*Example 8*:
900
 
901
  ``` cpp
902
  // The inner closure type must be a literal type regardless of how reference captures are represented.
903
  static_assert([](int n) { return [&n] { return ++n; }(); }(3) == 4);
904
  ```
@@ -906,22 +1077,44 @@ static_assert([](int n) { return [&n] { return ++n; }(); }(3) == 4);
906
  — *end example*]
907
 
908
  A bit-field or a member of an anonymous union shall not be captured by
909
  reference.
910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
911
  If a *lambda-expression* `m2` captures an entity and that entity is
912
  captured by an immediately enclosing *lambda-expression* `m1`, then
913
  `m2`’s capture is transformed as follows:
914
 
915
  - if `m1` captures the entity by copy, `m2` captures the corresponding
916
  non-static data member of `m1`’s closure type;
917
  - if `m1` captures the entity by reference, `m2` captures the same
918
  entity captured by `m1`.
919
 
920
- [*Example 9*:
921
 
922
- The nested lambda expressions and invocations below will output
923
  `123234`.
924
 
925
  ``` cpp
926
  int a = 1, b = 1, c = 1;
927
  auto m1 = [a, &b, &c]() mutable {
@@ -937,70 +1130,52 @@ m1();
937
  std::cout << a << b << c;
938
  ```
939
 
940
  — *end example*]
941
 
942
- Every occurrence of `decltype((x))` where `x` is a possibly
943
- parenthesized *id-expression* that names an entity of automatic storage
944
- duration is treated as if `x` were transformed into an access to a
945
- corresponding data member of the closure type that would have been
946
- declared if `x` were an odr-use of the denoted entity.
947
-
948
- [*Example 10*:
949
-
950
- ``` cpp
951
- void f3() {
952
- float x, &r = x;
953
- [=] { // x and r are not captured (appearance in a decltype operand is not an odr-use)
954
- decltype(x) y1; // y1 has type float
955
- decltype((x)) y2 = y1; // y2 has type float const& because this lambda is not mutable and x is an lvalue
956
- decltype(r) r1 = y1; // r1 has type float& (transformation not considered)
957
- decltype((r)) r2 = y2; // r2 has type float const&
958
- };
959
- }
960
- ```
961
-
962
- — *end example*]
963
-
964
  When the *lambda-expression* is evaluated, the entities that are
965
  captured by copy are used to direct-initialize each corresponding
966
  non-static data member of the resulting closure object, and the
967
  non-static data members corresponding to the *init-capture*s are
968
  initialized as indicated by the corresponding *initializer* (which may
969
  be copy- or direct-initialization). (For array members, the array
970
  elements are direct-initialized in increasing subscript order.) These
971
  initializations are performed in the (unspecified) order in which the
972
  non-static data members are declared.
973
 
974
- [*Note 8*: This ensures that the destructions will occur in the reverse
975
  order of the constructions. — *end note*]
976
 
977
- [*Note 9*: If a non-reference entity is implicitly or explicitly
978
  captured by reference, invoking the function call operator of the
979
  corresponding *lambda-expression* after the lifetime of the entity has
980
  ended is likely to result in undefined behavior. — *end note*]
981
 
982
- A *simple-capture* followed by an ellipsis is a pack expansion (
983
- [[temp.variadic]]). An *init-capture* followed by an ellipsis is
984
- ill-formed.
 
985
 
986
- [*Example 11*:
987
 
988
  ``` cpp
989
  template<class... Args>
990
  void f(Args... args) {
991
  auto lm = [&, args...] { return g(args...); };
992
  lm();
 
 
 
993
  }
994
  ```
995
 
996
  — *end example*]
997
 
998
  ### Fold expressions <a id="expr.prim.fold">[[expr.prim.fold]]</a>
999
 
1000
- A fold expression performs a fold of a template parameter pack (
1001
- [[temp.variadic]]) over a binary operator.
1002
 
1003
  ``` bnf
1004
  fold-expression:
1005
  '(' cast-expression fold-operator '...' ')'
1006
  '(' '...' fold-operator cast-expression ')'
@@ -1019,20 +1194,19 @@ fold-operator: one of
1019
  An expression of the form `(...` *op* `e)` where *op* is a
1020
  *fold-operator* is called a *unary left fold*. An expression of the form
1021
  `(e` *op* `...)` where *op* is a *fold-operator* is called a *unary
1022
  right fold*. Unary left folds and unary right folds are collectively
1023
  called *unary folds*. In a unary fold, the *cast-expression* shall
1024
- contain an unexpanded parameter pack ([[temp.variadic]]).
1025
 
1026
  An expression of the form `(e1` *op1* `...` *op2* `e2)` where *op1* and
1027
  *op2* are *fold-operator*s is called a *binary fold*. In a binary fold,
1028
  *op1* and *op2* shall be the same *fold-operator*, and either `e1` shall
1029
- contain an unexpanded parameter pack or `e2` shall contain an unexpanded
1030
- parameter pack, but not both. If `e2` contains an unexpanded parameter
1031
- pack, the expression is called a *binary left fold*. If `e1` contains an
1032
- unexpanded parameter pack, the expression is called a *binary right
1033
- fold*.
1034
 
1035
  [*Example 1*:
1036
 
1037
  ``` cpp
1038
  template<typename ...Args>
@@ -1040,11 +1214,315 @@ bool f(Args ...args) {
1040
  return (true && ... && args); // OK
1041
  }
1042
 
1043
  template<typename ...Args>
1044
  bool f(Args ...args) {
1045
- return (args + ... + args); // error: both operands contain unexpanded parameter packs
1046
  }
1047
  ```
1048
 
1049
  — *end example*]
1050
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ## Primary expressions <a id="expr.prim">[[expr.prim]]</a>
2
 
3
  ``` bnf
4
  primary-expression:
5
  literal
6
+ this
7
  '(' expression ')'
8
  id-expression
9
  lambda-expression
10
  fold-expression
11
+ requires-expression
12
  ```
13
 
14
  ### Literals <a id="expr.prim.literal">[[expr.prim.literal]]</a>
15
 
16
+ A *literal* is a primary expression. The type of a *literal* is
17
+ determined based on its form as specified in [[lex.literal]]. A
18
+ *string-literal* is an lvalue, a *user-defined-literal* has the same
19
+ value category as the corresponding operator call expression described
20
+ in [[lex.ext]], and any other *literal* is a prvalue.
21
 
22
  ### This <a id="expr.prim.this">[[expr.prim.this]]</a>
23
 
24
  The keyword `this` names a pointer to the object for which a non-static
25
+ member function [[class.this]] is invoked or a non-static data member’s
26
+ initializer [[class.mem]] is evaluated.
27
 
28
  If a declaration declares a member function or member function template
29
  of a class `X`, the expression `this` is a prvalue of type “pointer to
30
  *cv-qualifier-seq* `X`” between the optional *cv-qualifier-seq* and the
31
  end of the *function-definition*, *member-declarator*, or *declarator*.
 
35
  as they are within a non-static member function).
36
 
37
  [*Note 1*: This is because declaration matching does not occur until
38
  the complete declarator is known. — *end note*]
39
 
40
+ [*Note 2*:
 
 
41
 
42
+ In a *trailing-return-type*, the class being defined is not required to
43
+ be complete for purposes of class member access [[expr.ref]]. Class
44
+ members declared later are not visible.
45
 
46
  [*Example 1*:
47
 
48
  ``` cpp
49
  struct A {
 
54
  template auto A::f(int t) -> decltype(t + g());
55
  ```
56
 
57
  — *end example*]
58
 
59
+ *end note*]
60
+
61
+ Otherwise, if a *member-declarator* declares a non-static data member
62
+ [[class.mem]] of a class `X`, the expression `this` is a prvalue of type
63
+ “pointer to `X`” within the optional default member initializer
64
+ [[class.mem]]. It shall not appear elsewhere in the *member-declarator*.
65
 
66
  The expression `this` shall not appear in any other context.
67
 
68
  [*Example 2*:
69
 
 
85
  — *end example*]
86
 
87
  ### Parentheses <a id="expr.prim.paren">[[expr.prim.paren]]</a>
88
 
89
  A parenthesized expression `(E)` is a primary expression whose type,
90
+ value, and value category are identical to those of E. The parenthesized
91
+ expression can be used in exactly the same contexts as those where E can
92
+ be used, and with the same meaning, except as otherwise indicated.
 
93
 
94
  ### Names <a id="expr.prim.id">[[expr.prim.id]]</a>
95
 
96
  ``` bnf
97
  id-expression:
 
99
  qualified-id
100
  ```
101
 
102
  An *id-expression* is a restricted form of a *primary-expression*.
103
 
104
+ [*Note 1*: An *id-expression* can appear after `.` and `->` operators
105
+ [[expr.ref]]. — *end note*]
106
 
107
  An *id-expression* that denotes a non-static data member or non-static
108
  member function of a class can only be used:
109
 
110
+ - as part of a class member access [[expr.ref]] in which the object
111
+ expression refers to the member’s class[^10] or a class derived from
112
  that class, or
113
+ - to form a pointer to member [[expr.unary.op]], or
114
  - if that *id-expression* denotes a non-static data member and it
115
  appears in an unevaluated operand.
116
  \[*Example 1*:
117
  ``` cpp
118
  struct S {
 
122
  int j = sizeof(S::m + 42); // OK
123
  ```
124
 
125
  — *end example*]
126
 
127
+ A potentially-evaluated *id-expression* that denotes an immediate
128
+ function [[dcl.constexpr]] shall appear only
129
+
130
+ - as a subexpression of an immediate invocation, or
131
+ - in an immediate function context [[expr.const]].
132
+
133
+ For an *id-expression* that denotes an overload set, overload resolution
134
+ is performed to select a unique function ([[over.match]],
135
+ [[over.over]]).
136
+
137
+ [*Note 2*:
138
+
139
+ A program cannot refer to a function with a trailing *requires-clause*
140
+ whose *constraint-expression* is not satisfied, because such functions
141
+ are never selected by overload resolution.
142
+
143
+ [*Example 2*:
144
+
145
+ ``` cpp
146
+ template<typename T> struct A {
147
+ static void f(int) requires false;
148
+ }
149
+
150
+ void g() {
151
+ A<int>::f(0); // error: cannot call f
152
+ void (*p1)(int) = A<int>::f; // error: cannot take the address of f
153
+ decltype(A<int>::f)* p2 = nullptr; // error: the type decltype(A<int>::f) is invalid
154
+ }
155
+ ```
156
+
157
+ In each case, the constraints of `f` are not satisfied. In the
158
+ declaration of `p2`, those constraints are required to be satisfied even
159
+ though `f` is an unevaluated operand [[expr.prop]].
160
+
161
+ — *end example*]
162
+
163
+ — *end note*]
164
+
165
  #### Unqualified names <a id="expr.prim.id.unqual">[[expr.prim.id.unqual]]</a>
166
 
167
  ``` bnf
168
  unqualified-id:
169
  identifier
170
  operator-function-id
171
  conversion-function-id
172
  literal-operator-id
173
+ '~' type-name
174
  '~' decltype-specifier
175
  template-id
176
  ```
177
 
178
+ An *identifier* is only an *id-expression* if it has been suitably
179
+ declared [[dcl.dcl]] or if it appears as part of a *declarator-id*
180
+ [[dcl.decl]]. An *identifier* that names a coroutine parameter refers to
181
+ the copy of the parameter [[dcl.fct.def.coroutine]].
182
 
183
  [*Note 1*: For *operator-function-id*s, see  [[over.oper]]; for
184
  *conversion-function-id*s, see  [[class.conv.fct]]; for
185
  *literal-operator-id*s, see  [[over.literal]]; for *template-id*s, see 
186
+ [[temp.names]]. A *type-name* or *decltype-specifier* prefixed by `~`
187
+ denotes the destructor of the type so named; see  [[expr.prim.id.dtor]].
188
+ Within the definition of a non-static member function, an *identifier*
189
+ that names a non-static member is transformed to a class member access
190
+ expression ([[class.mfct.non-static]]). — *end note*]
191
+
192
+ The result is the entity denoted by the identifier. If the entity is a
193
+ local entity and naming it from outside of an unevaluated operand within
194
+ the declarative region where the *unqualified-id* appears would result
195
+ in some intervening *lambda-expression* capturing it by copy
196
+ [[expr.prim.lambda.capture]], the type of the expression is the type of
197
+ a class member access expression [[expr.ref]] naming the non-static data
198
+ member that would be declared for such a capture in the closure object
199
+ of the innermost such intervening *lambda-expression*.
200
+
201
+ [*Note 2*: If that *lambda-expression* is not declared `mutable`, the
202
+ type of such an identifier will typically be `const`
203
+ qualified. — *end note*]
204
+
205
+ The type of the expression is the type of the result.
206
+
207
+ [*Note 3*: If the entity is a template parameter object for a template
208
+ parameter of type `T` [[temp.param]], the type of the expression is
209
+ `const T`. — *end note*]
210
+
211
+ [*Note 4*: The type will be adjusted as described in [[expr.type]] if
212
+ it is cv-qualified or is a reference type. — *end note*]
213
+
214
+ The expression is an lvalue if the entity is a function, variable,
215
+ structured binding [[dcl.struct.bind]], data member, or template
216
+ parameter object and a prvalue otherwise [[basic.lval]]; it is a
217
+ bit-field if the identifier designates a bit-field.
218
+
219
+ [*Example 1*:
220
+
221
+ ``` cpp
222
+ void f() {
223
+ float x, &r = x;
224
+ [=] {
225
+ decltype(x) y1; // y1 has type float
226
+ decltype((x)) y2 = y1; // y2 has type float const& because this lambda
227
+ // is not mutable and x is an lvalue
228
+ decltype(r) r1 = y1; // r1 has type float&
229
+ decltype((r)) r2 = y2; // r2 has type float const&
230
+ };
231
+ }
232
+ ```
233
+
234
+ — *end example*]
235
 
236
  #### Qualified names <a id="expr.prim.id.qual">[[expr.prim.id.qual]]</a>
237
 
238
  ``` bnf
239
  qualified-id:
240
+ nested-name-specifier templateₒₚₜ unqualified-id
241
  ```
242
 
243
  ``` bnf
244
  nested-name-specifier:
245
  '::'
246
  type-name '::'
247
  namespace-name '::'
248
  decltype-specifier '::'
249
  nested-name-specifier identifier '::'
250
+ nested-name-specifier templateₒₚₜ simple-template-id '::'
251
  ```
252
 
253
  The type denoted by a *decltype-specifier* in a *nested-name-specifier*
254
  shall be a class or enumeration type.
255
 
256
  A *nested-name-specifier* that denotes a class, optionally followed by
257
+ the keyword `template` [[temp.names]], and then followed by the name of
258
+ a member of either that class [[class.mem]] or one of its base classes
259
+ [[class.derived]], is a *qualified-id*;  [[class.qual]] describes name
260
+ lookup for class members that appear in *qualified-id*s. The result is
261
+ the member. The type of the result is the type of the member. The result
262
+ is an lvalue if the member is a static member function or a data member
263
+ and a prvalue otherwise.
264
 
265
  [*Note 1*: A class member can be referred to using a *qualified-id* at
266
+ any point in its potential scope [[basic.scope.class]]. — *end note*]
 
267
 
268
+ Where *type-name* `::~` *type-name* is used, the two *type-name*s shall
269
+ refer to the same type (ignoring cv-qualifications); this notation
270
+ denotes the destructor of the type so named [[expr.prim.id.dtor]]. The
271
+ *unqualified-id* in a *qualified-id* shall not be of the form
272
+ `~`*decltype-specifier*.
 
 
 
273
 
274
  The *nested-name-specifier* `::` names the global namespace. A
275
+ *nested-name-specifier* that names a namespace [[basic.namespace]],
276
+ optionally followed by the keyword `template` [[temp.names]], and then
277
+ followed by the name of a member of that namespace (or the name of a
278
+ member of a namespace made visible by a *using-directive*), is a
279
  *qualified-id*;  [[namespace.qual]] describes name lookup for namespace
280
  members that appear in *qualified-id*s. The result is the member. The
281
  type of the result is the type of the member. The result is an lvalue if
282
+ the member is a function, a variable, or a structured binding
283
+ [[dcl.struct.bind]] and a prvalue otherwise.
284
 
285
+ A *nested-name-specifier* that denotes an enumeration [[dcl.enum]],
286
  followed by the name of an enumerator of that enumeration, is a
287
  *qualified-id* that refers to the enumerator. The result is the
288
  enumerator. The type of the result is the type of the enumeration. The
289
  result is a prvalue.
290
 
291
  In a *qualified-id*, if the *unqualified-id* is a
292
+ *conversion-function-id*, its *conversion-type-id* is first looked up in
293
+ the class denoted by the *nested-name-specifier* of the *qualified-id*
294
+ and the name, if found, is used. Otherwise, it is looked up in the
295
+ context in which the entire *qualified-id* occurs. In each of these
296
+ lookups, only names that denote types or templates whose specializations
297
+ are types are considered.
298
+
299
+ #### Destruction <a id="expr.prim.id.dtor">[[expr.prim.id.dtor]]</a>
300
+
301
+ An *id-expression* that denotes the destructor of a type `T` names the
302
+ destructor of `T` if `T` is a class type [[class.dtor]], otherwise the
303
+ *id-expression* is said to name a *pseudo-destructor*.
304
+
305
+ If the *id-expression* names a pseudo-destructor, `T` shall be a scalar
306
+ type and the *id-expression* shall appear as the right operand of a
307
+ class member access [[expr.ref]] that forms the *postfix-expression* of
308
+ a function call [[expr.call]].
309
+
310
+ [*Note 1*: Such a call ends the lifetime of the object ([[expr.call]],
311
+ [[basic.life]]). — *end note*]
312
+
313
+ [*Example 1*:
314
+
315
+ ``` cpp
316
+ struct C { };
317
+ void f() {
318
+ C * pc = new C;
319
+ using C2 = C;
320
+ pc->C::~C2(); // OK, destroys *pc
321
+ C().C::~C(); // undefined behavior: temporary of type C destroyed twice
322
+ using T = int;
323
+ 0 .T::~T(); // OK, no effect
324
+ 0.T::~T(); // error: 0.T is a user-defined-floating-point-literal[lex.ext]
325
+ }
326
+ ```
327
+
328
+ — *end example*]
329
 
330
  ### Lambda expressions <a id="expr.prim.lambda">[[expr.prim.lambda]]</a>
331
 
332
  ``` bnf
333
  lambda-expression:
334
  lambda-introducer lambda-declaratorₒₚₜ compound-statement
335
+ lambda-introducer '<' template-parameter-list '>' requires-clauseₒₚₜ lambda-declaratorₒₚₜ compound-statement
336
  ```
337
 
338
  ``` bnf
339
  lambda-introducer:
340
  '[' lambda-captureₒₚₜ ']'
341
  ```
342
 
343
  ``` bnf
344
  lambda-declarator:
345
  '(' parameter-declaration-clause ')' decl-specifier-seqₒₚₜ
346
+ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ requires-clauseₒₚₜ
347
  ```
348
 
349
+ A *lambda-expression* provides a concise way to create a simple function
350
+ object.
351
 
352
  [*Example 1*:
353
 
354
  ``` cpp
355
  #include <algorithm>
 
360
  ```
361
 
362
  — *end example*]
363
 
364
  A *lambda-expression* is a prvalue whose result object is called the
365
+ *closure object*.
 
 
 
 
366
 
367
+ [*Note 1*: A closure object behaves like a function object
368
+ [[function.objects]]. — *end note*]
 
 
 
369
 
370
  In the *decl-specifier-seq* of the *lambda-declarator*, each
371
+ *decl-specifier* shall be one of `mutable`, `constexpr`, or `consteval`.
372
+
373
+ [*Note 2*: The trailing *requires-clause* is described in
374
+ [[dcl.decl]]. — *end note*]
375
 
376
  If a *lambda-expression* does not include a *lambda-declarator*, it is
377
  as if the *lambda-declarator* were `()`. The lambda return type is
378
  `auto`, which is replaced by the type specified by the
379
  *trailing-return-type* if provided and/or deduced from `return`
 
388
  auto x3 = []()->auto&& { return j; }; // OK: return type is int&
389
  ```
390
 
391
  — *end example*]
392
 
393
+ A lambda is a *generic lambda* if the *lambda-expression* has any
394
+ generic parameter type placeholders [[dcl.spec.auto]], or if the lambda
395
+ has a *template-parameter-list*.
396
+
397
+ [*Example 3*:
398
+
399
+ ``` cpp
400
+ int i = [](int i, auto a) { return i; }(3, 4); // OK: a generic lambda
401
+ int j = []<class T>(T t, int i) { return i; }(3, 4); // OK: a generic lambda
402
+ ```
403
+
404
+ — *end example*]
405
+
406
  #### Closure types <a id="expr.prim.lambda.closure">[[expr.prim.lambda.closure]]</a>
407
 
408
  The type of a *lambda-expression* (which is also the type of the closure
409
  object) is a unique, unnamed non-union class type, called the *closure
410
  type*, whose properties are described below.
411
 
412
  The closure type is declared in the smallest block scope, class scope,
413
  or namespace scope that contains the corresponding *lambda-expression*.
414
 
415
  [*Note 1*: This determines the set of namespaces and classes associated
416
+ with the closure type [[basic.lookup.argdep]]. The parameter types of a
417
+ *lambda-declarator* do not affect these associated namespaces and
418
  classes. — *end note*]
419
 
420
+ The closure type is not an aggregate type [[dcl.init.aggr]]. An
421
  implementation may define the closure type differently from what is
422
  described below provided this does not alter the observable behavior of
423
  the program other than by changing:
424
 
425
  - the size and/or alignment of the closure type,
426
+ - whether the closure type is trivially copyable [[class.prop]], or
427
+ - whether the closure type is a standard-layout class [[class.prop]].
 
 
428
 
429
  An implementation shall not add members of rvalue reference type to the
430
  closure type.
431
 
432
+ The closure type for a *lambda-expression* has a public inline function
433
+ call operator (for a non-generic lambda) or function call operator
434
+ template (for a generic lambda) [[over.call]] whose parameters and
435
  return type are described by the *lambda-expression*’s
436
+ *parameter-declaration-clause* and *trailing-return-type* respectively,
437
+ and whose *template-parameter-list* consists of the specified
438
+ *template-parameter-list*, if any. The *requires-clause* of the function
439
+ call operator template is the *requires-clause* immediately following
440
+ `<` *template-parameter-list* `>`, if any. The trailing
441
+ *requires-clause* of the function call operator or operator template is
442
+ the *requires-clause* of the *lambda-declarator*, if any.
443
+
444
+ [*Note 2*: The function call operator template for a generic lambda
445
+ might be an abbreviated function template [[dcl.fct]]. — *end note*]
 
 
 
446
 
447
  [*Example 1*:
448
 
449
  ``` cpp
450
  auto glambda = [](auto a, auto&& b) { return a < b; };
 
475
  call operator or operator template. An *attribute-specifier-seq* in a
476
  *lambda-declarator* appertains to the type of the corresponding function
477
  call operator or operator template. The function call operator or any
478
  given operator template specialization is a constexpr function if either
479
  the corresponding *lambda-expression*'s *parameter-declaration-clause*
480
+ is followed by `constexpr` or `consteval`, or it satisfies the
481
+ requirements for a constexpr function [[dcl.constexpr]]. It is an
482
+ immediate function [[dcl.constexpr]] if the corresponding
483
+ *lambda-expression*'s *parameter-declaration-clause* is followed by
484
+ `consteval`.
485
 
486
+ [*Note 3*: Names referenced in the *lambda-declarator* are looked up in
487
  the context in which the *lambda-expression* appears. — *end note*]
488
 
489
  [*Example 2*:
490
 
491
  ``` cpp
 
494
 
495
  struct NonLiteral {
496
  NonLiteral(int n) : n(n) { }
497
  int n;
498
  };
499
+ static_assert(ID(NonLiteral{3}).n == 3); // error
500
  ```
501
 
502
  — *end example*]
503
 
504
  [*Example 3*:
 
522
  // Since two below is not declared constexpr, an evaluation of its constexpr member function call operator
523
  // cannot perform an lvalue-to-rvalue conversion on one of its subobjects (that represents its capture)
524
  // in a constant expression.
525
  auto two = monoid(2);
526
  assert(two() == 2); // OK, not a constant expression.
527
+ static_assert(add(one)(one)() == two()); // error: two() is not a constant expression
528
  static_assert(add(one)(one)() == monoid(2)()); // OK
529
  ```
530
 
531
  — *end example*]
532
 
533
+ [*Note 4*:
534
+
535
+ The function call operator or operator template may be constrained
536
+ [[temp.constr.decl]] by a *type-constraint* [[temp.param]], a
537
+ *requires-clause* [[temp.pre]], or a trailing *requires-clause*
538
+ [[dcl.decl]].
539
+
540
+ [*Example 4*:
541
+
542
+ ``` cpp
543
+ template <typename T> concept C1 = ...;
544
+ template <std::size_t N> concept C2 = ...;
545
+ template <typename A, typename B> concept C3 = ...;
546
+
547
+ auto f = []<typename T1, C1 T2> requires C2<sizeof(T1) + sizeof(T2)>
548
+ (T1 a1, T1 b1, T2 a2, auto a3, auto a4) requires C3<decltype(a4), T2> {
549
+ // T2 is constrained by a type-constraint.
550
+ // T1 and T2 are constrained by a requires-clause, and
551
+ // T2 and the type of a4 are constrained by a trailing requires-clause.
552
+ };
553
+ ```
554
+
555
+ — *end example*]
556
+
557
+ — *end note*]
558
+
559
  The closure type for a non-generic *lambda-expression* with no
560
+ *lambda-capture* whose constraints (if any) are satisfied has a
561
+ conversion function to pointer to function with C++ language linkage
562
+ [[dcl.link]] having the same parameter and return types as the closure
563
+ type’s function call operator. The conversion is to “pointer to
564
+ `noexcept` function” if the function call operator has a non-throwing
565
+ exception specification. The value returned by this conversion function
566
+ is the address of a function `F` that, when invoked, has the same effect
567
+ as invoking the closure type’s function call operator on a
568
+ default-constructed instance of the closure type. `F` is a constexpr
569
+ function if the function call operator is a constexpr function and is an
570
+ immediate function if the function call operator is an immediate
571
+ function.
 
 
 
 
572
 
573
+ For a generic lambda with no *lambda-capture*, the closure type has a
574
+ conversion function template to pointer to function. The conversion
575
+ function template has the same invented template parameter list, and the
576
+ pointer to function has the same parameter types, as the function call
577
+ operator template. The return type of the pointer to function shall
578
+ behave as if it were a *decltype-specifier* denoting the return type of
579
+ the corresponding function call operator template specialization.
580
+
581
+ [*Note 5*:
582
 
583
  If the generic lambda has no *trailing-return-type* or the
584
  *trailing-return-type* contains a placeholder type, return type
585
  deduction of the corresponding function call operator template
586
  specialization has to be done. The corresponding specialization is that
 
612
  };
613
  ```
614
 
615
  — *end note*]
616
 
617
+ [*Example 5*:
618
 
619
  ``` cpp
620
  void f1(int (*)(int)) { }
621
  void f2(char (*)(int)) { }
622
 
 
637
  — *end example*]
638
 
639
  The value returned by any given specialization of this conversion
640
  function template is the address of a function `F` that, when invoked,
641
  has the same effect as invoking the generic lambda’s corresponding
642
+ function call operator template specialization on a default-constructed
643
+ instance of the closure type. `F` is a constexpr function if the
644
+ corresponding specialization is a constexpr function and `F` is an
645
+ immediate function if the function call operator template specialization
646
+ is an immediate function.
647
 
648
+ [*Note 6*: This will result in the implicit instantiation of the
649
  generic lambda’s body. The instantiated generic lambda’s return type and
650
+ parameter types are required to match the return type and parameter
651
+ types of the pointer to function. — *end note*]
652
 
653
+ [*Example 6*:
654
 
655
  ``` cpp
656
  auto GL = [](auto a) { std::cout << a; return a; };
657
  int (*GL_int)(int) = GL; // OK: through conversion function template
658
  GL_int(3); // OK: same as GL(3)
 
660
 
661
  — *end example*]
662
 
663
  The conversion function or conversion function template is public,
664
  constexpr, non-virtual, non-explicit, const, and has a non-throwing
665
+ exception specification [[except.spec]].
666
 
667
+ [*Example 7*:
668
 
669
  ``` cpp
670
  auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
671
  auto C = [](auto a) { return a; };
672
 
673
  static_assert(Fwd(C,3) == 3); // OK
674
 
675
  // No specialization of the function call operator template can be constexpr (due to the local static).
676
  auto NC = [](auto a) { static int s; return a; };
677
+ static_assert(Fwd(NC,3) == 3); // error
678
  ```
679
 
680
  — *end example*]
681
 
682
  The *lambda-expression*’s *compound-statement* yields the
683
+ *function-body* [[dcl.fct.def]] of the function call operator, but for
684
+ purposes of name lookup [[basic.lookup]], determining the type and value
685
+ of `this` [[class.this]] and transforming *id-expression*s referring to
686
+ non-static class members into class member access expressions using
687
+ `(*this)` ([[class.mfct.non-static]]), the *compound-statement* is
688
+ considered in the context of the *lambda-expression*.
 
689
 
690
+ [*Example 8*:
691
 
692
  ``` cpp
693
  struct S1 {
694
  int x, y;
695
  int operator()(int);
 
707
  Further, a variable `__func__` is implicitly defined at the beginning of
708
  the *compound-statement* of the *lambda-expression*, with semantics as
709
  described in  [[dcl.fct.def.general]].
710
 
711
  The closure type associated with a *lambda-expression* has no default
712
+ constructor if the *lambda-expression* has a *lambda-capture* and a
713
+ defaulted default constructor otherwise. It has a defaulted copy
714
+ constructor and a defaulted move constructor [[class.copy.ctor]]. It has
715
+ a deleted copy assignment operator if the *lambda-expression* has a
716
+ *lambda-capture* and defaulted copy and move assignment operators
717
+ otherwise [[class.copy.assign]].
718
 
719
+ [*Note 7*: These special member functions are implicitly defined as
720
  usual, and might therefore be defined as deleted. — *end note*]
721
 
722
  The closure type associated with a *lambda-expression* has an
723
+ implicitly-declared destructor [[class.dtor]].
724
 
725
+ A member of a closure type shall not be explicitly instantiated
726
+ [[temp.explicit]], explicitly specialized [[temp.expl.spec]], or named
727
+ in a friend declaration [[class.friend]].
728
 
729
  #### Captures <a id="expr.prim.lambda.capture">[[expr.prim.lambda.capture]]</a>
730
 
731
  ``` bnf
732
  lambda-capture:
 
741
  '='
742
  ```
743
 
744
  ``` bnf
745
  capture-list:
746
+ capture
747
+ capture-list ',' capture
748
  ```
749
 
750
  ``` bnf
751
  capture:
752
  simple-capture
753
  init-capture
754
  ```
755
 
756
  ``` bnf
757
  simple-capture:
758
+ identifier '...'ₒₚₜ
759
+ '&' identifier '...'ₒₚₜ
760
+ this
761
+ '*' 'this'
762
  ```
763
 
764
  ``` bnf
765
  init-capture:
766
+ '...'ₒₚₜ identifier initializer
767
+ '&' '...'ₒₚₜ identifier initializer
768
  ```
769
 
770
  The body of a *lambda-expression* may refer to variables with automatic
771
  storage duration and the `*this` object (if any) of enclosing block
772
  scopes by capturing those entities, as described below.
773
 
774
  If a *lambda-capture* includes a *capture-default* that is `&`, no
775
  identifier in a *simple-capture* of that *lambda-capture* shall be
776
  preceded by `&`. If a *lambda-capture* includes a *capture-default* that
777
  is `=`, each *simple-capture* of that *lambda-capture* shall be of the
778
+ form “`&` *identifier* `...`ₒₚₜ , “`this`”, or “`* this`”.
779
 
780
  [*Note 1*: The form `[&,this]` is redundant but accepted for
781
  compatibility with ISO C++14. — *end note*]
782
 
783
  Ignoring appearances in *initializer*s of *init-capture*s, an identifier
 
787
 
788
  ``` cpp
789
  struct S2 { void f(int i); };
790
  void S2::f(int i) {
791
  [&, i]{ }; // OK
792
+ [&, this, i]{ }; // OK, equivalent to [&, i]
793
  [&, &i]{ }; // error: i preceded by & when & is the default
794
  [=, *this]{ }; // OK
795
+ [=, this]{ }; // OK, equivalent to [=]
796
  [i, i]{ }; // error: i repeated
797
  [this, *this]{ }; // error: this appears twice
798
  }
799
  ```
800
 
801
  — *end example*]
802
 
803
+ A *lambda-expression* shall not have a *capture-default* or
804
+ *simple-capture* in its *lambda-introducer* unless its innermost
805
+ enclosing scope is a block scope [[basic.scope.block]] or it appears
806
+ within a default member initializer and its innermost enclosing scope is
807
+ the corresponding class scope [[basic.scope.class]].
 
 
 
 
808
 
809
  The *identifier* in a *simple-capture* is looked up using the usual
810
+ rules for unqualified name lookup [[basic.lookup.unqual]]; each such
811
+ lookup shall find a local entity. The *simple-capture*s `this` and
812
+ `* this` denote the local entity `*this`. An entity that is designated
813
+ by a *simple-capture* is said to be *explicitly captured*.
 
 
814
 
815
  If an *identifier* in a *simple-capture* appears as the *declarator-id*
816
  of a parameter of the *lambda-declarator*'s
817
  *parameter-declaration-clause*, the program is ill-formed.
818
 
819
  [*Example 2*:
820
 
821
  ``` cpp
822
  void f() {
823
  int x = 0;
824
+ auto g = [x](int x) { return 0; }; // error: parameter and simple-capture have the same name
825
  }
826
  ```
827
 
828
  — *end example*]
829
 
830
+ An *init-capture* without ellipsis behaves as if it declares and
831
+ explicitly captures a variable of the form “`auto` *init-capture* `;`”
832
+ whose declarative region is the *lambda-expression*’s
833
+ *compound-statement*, except that:
834
 
835
  - if the capture is by copy (see below), the non-static data member
836
  declared for the capture and the variable are treated as two different
837
  ways of referring to the same object, which has the lifetime of the
838
  non-static data member, and no additional copy and destruction is
839
  performed, and
840
  - if the capture is by reference, the variable’s lifetime ends when the
841
  closure object’s lifetime ends.
842
 
843
+ [*Note 2*: This enables an *init-capture* like “`x = std::move(x)`”;
844
  the second “`x`” must bind to a declaration in the surrounding
845
  context. — *end note*]
846
 
847
  [*Example 3*:
848
 
 
851
  auto y = [&r = x, x = x+1]()->int {
852
  r += 2;
853
  return x+2;
854
  }(); // Updates ::x to 6, and initializes y to 7.
855
 
856
+ auto z = [a = 42](int a) { return 1; }; // error: parameter and local variable have the same name
857
  ```
858
 
859
  — *end example*]
860
 
861
+ For the purposes of lambda capture, an expression potentially references
862
+ local entities as follows:
 
 
 
 
863
 
864
+ - An *id-expression* that names a local entity potentially references
865
+ that entity; an *id-expression* that names one or more non-static
866
+ class members and does not form a pointer to member [[expr.unary.op]]
867
+ potentially references `*this`. \[*Note 3*: This occurs even if
868
+ overload resolution selects a static member function for the
869
+ *id-expression*. *end note*]
870
+ - A `this` expression potentially references `*this`.
871
+ - A *lambda-expression* potentially references the local entities named
872
+ by its *simple-capture*s.
873
+
874
+ If an expression potentially references a local entity within a
875
+ declarative region in which it is odr-usable, and the expression would
876
+ be potentially evaluated if the effect of any enclosing `typeid`
877
+ expressions [[expr.typeid]] were ignored, the entity is said to be
878
+ *implicitly captured* by each intervening *lambda-expression* with an
879
+ associated *capture-default* that does not explicitly capture it. The
880
+ implicit capture of `*this` is deprecated when the *capture-default* is
881
+ `=`; see [[depr.capture.this]].
882
 
883
  [*Example 4*:
884
 
885
  ``` cpp
886
+ void f(int, const int (&)[2] = {}); // #1
887
+ void f(const int&, const int (&)[1]); // #2
888
  void test() {
889
  const int x = 17;
890
  auto g = [](auto a) {
891
  f(x); // OK: calls #1, does not capture x
892
  };
893
 
894
+ auto g1 = [=](auto a) {
895
+ f(x); // OK: calls #1, captures x
896
+ };
897
+
898
  auto g2 = [=](auto a) {
899
  int selector[sizeof(a) == 1 ? 1 : 2]{};
900
+ f(x, selector); // OK: captures x, might call #1 or #2
901
+ };
902
+
903
+ auto g3 = [=](auto a) {
904
+ typeid(a + x); // captures x regardless of whether a + x is an unevaluated operand
905
  };
906
  }
907
  ```
908
 
909
+ Within `g1`, an implementation might optimize away the capture of `x` as
910
+ it is not odr-used.
911
+
912
  — *end example*]
913
 
914
+ [*Note 4*:
915
+
916
+ The set of captured entities is determined syntactically, and entities
917
+ might be implicitly captured even if the expression denoting a local
918
+ entity is within a discarded statement [[stmt.if]].
919
+
920
+ [*Example 5*:
921
+
922
+ ``` cpp
923
+ template<bool B>
924
+ void f(int n) {
925
+ [=](auto a) {
926
+ if constexpr (B && sizeof(a) > 4) {
927
+ (void)n; // captures n regardless of the value of B and sizeof(int)
928
+ }
929
+ }(0);
930
+ }
931
+ ```
932
+
933
+ — *end example*]
934
 
935
+ *end note*]
 
 
 
936
 
937
  An entity is *captured* if it is captured explicitly or implicitly. An
938
+ entity captured by a *lambda-expression* is odr-used [[basic.def.odr]]
939
+ in the scope containing the *lambda-expression*.
 
 
 
 
 
 
 
 
940
 
941
+ [*Note 5*: As a consequence, if a *lambda-expression* explicitly
942
+ captures an entity that is not odr-usable, the program is ill-formed
943
+ [[basic.def.odr]]. — *end note*]
944
+
945
+ [*Example 6*:
946
 
947
  ``` cpp
948
  void f1(int i) {
949
  int const N = 20;
950
  auto m1 = [=]{
 
958
  int f;
959
  void work(int n) {
960
  int m = n*n;
961
  int j = 40;
962
  auto m3 = [this,m] {
963
+ auto m4 = [&,j] { // error: j not odr-usable due to intervening lambda m3
964
+ int x = n; // error: n is odr-used but not odr-usable due to intervening lambda m3
965
  x += m; // OK: m implicitly captured by m4 and explicitly captured by m3
966
+ x += i; // error: i is odr-used but not odr-usable
967
+ // due to intervening function and class scopes
968
  x += f; // OK: this captured implicitly by m4 and explicitly by m3
969
  };
970
  };
971
  }
972
  };
 
976
  double ohseven = .007;
977
  auto f() {
978
  return [this] {
979
  return [*this] {
980
  return ohseven; // OK
981
+ };
982
  }();
983
  }
984
  auto g() {
985
  return [] {
986
  return [*this] { }; // error: *this not captured by outer lambda-expression
 
989
  };
990
  ```
991
 
992
  — *end example*]
993
 
994
+ [*Note 6*: Because local entities are not odr-usable within a default
995
+ argument [[basic.def.odr]], a *lambda-expression* appearing in a default
996
+ argument cannot implicitly or explicitly capture any local entity. Such
997
+ a *lambda-expression* can still have an *init-capture* if any
998
+ full-expression in its *initializer* satisfies the constraints of an
999
+ expression appearing in a default argument
1000
+ [[dcl.fct.default]]. — *end note*]
1001
 
1002
+ [*Example 7*:
1003
 
1004
  ``` cpp
1005
  void f2() {
1006
  int i = 1;
1007
+ void g1(int = ([i]{ return i; })()); // error
1008
+ void g2(int = ([i]{ return 0; })()); // error
1009
+ void g3(int = ([=]{ return i; })()); // error
1010
  void g4(int = ([=]{ return 0; })()); // OK
1011
  void g5(int = ([]{ return sizeof i; })()); // OK
1012
+ void g6(int = ([x=1]{ return x; })()); // OK
1013
+ void g7(int = ([x=i]{ return x; })()); // error
1014
  }
1015
  ```
1016
 
1017
  — *end example*]
1018
 
 
1030
  referenced function type if the entity is a reference to a function, or
1031
  the type of the corresponding captured entity otherwise. A member of an
1032
  anonymous union shall not be captured by copy.
1033
 
1034
  Every *id-expression* within the *compound-statement* of a
1035
+ *lambda-expression* that is an odr-use [[basic.def.odr]] of an entity
1036
  captured by copy is transformed into an access to the corresponding
1037
  unnamed data member of the closure type.
1038
 
1039
+ [*Note 7*: An *id-expression* that is not an odr-use refers to the
1040
+ original entity, never to a member of the closure type. However, such an
1041
+ *id-expression* can still cause the implicit capture of the
1042
  entity. — *end note*]
1043
 
1044
+ If `*this` is captured by copy, each expression that odr-uses `*this` is
1045
+ transformed to instead refer to the corresponding unnamed data member of
1046
+ the closure type.
1047
 
1048
+ [*Example 8*:
 
 
 
 
 
 
 
 
 
 
 
 
1049
 
1050
  ``` cpp
1051
  void f(const int*);
1052
  void g() {
1053
  const int N = 10;
 
1055
  int arr[N]; // OK: not an odr-use, refers to automatic variable
1056
  f(&N); // OK: causes N to be captured; &N points to
1057
  // the corresponding member of the closure type
1058
  };
1059
  }
 
 
 
 
 
 
1060
  ```
1061
 
1062
  — *end example*]
1063
 
1064
  An entity is *captured by reference* if it is implicitly or explicitly
1065
  captured but not captured by copy. It is unspecified whether additional
1066
  unnamed non-static data members are declared in the closure type for
1067
  entities captured by reference. If declared, such non-static data
1068
  members shall be of literal type.
1069
 
1070
+ [*Example 9*:
1071
 
1072
  ``` cpp
1073
  // The inner closure type must be a literal type regardless of how reference captures are represented.
1074
  static_assert([](int n) { return [&n] { return ++n; }(); }(3) == 4);
1075
  ```
 
1077
  — *end example*]
1078
 
1079
  A bit-field or a member of an anonymous union shall not be captured by
1080
  reference.
1081
 
1082
+ An *id-expression* within the *compound-statement* of a
1083
+ *lambda-expression* that is an odr-use of a reference captured by
1084
+ reference refers to the entity to which the captured reference is bound
1085
+ and not to the captured reference.
1086
+
1087
+ [*Note 8*: The validity of such captures is determined by the lifetime
1088
+ of the object to which the reference refers, not by the lifetime of the
1089
+ reference itself. — *end note*]
1090
+
1091
+ [*Example 10*:
1092
+
1093
+ ``` cpp
1094
+ auto h(int &r) {
1095
+ return [&] {
1096
+ ++r; // Valid after h returns if the lifetime of the
1097
+ // object to which r is bound has not ended
1098
+ };
1099
+ }
1100
+ ```
1101
+
1102
+ — *end example*]
1103
+
1104
  If a *lambda-expression* `m2` captures an entity and that entity is
1105
  captured by an immediately enclosing *lambda-expression* `m1`, then
1106
  `m2`’s capture is transformed as follows:
1107
 
1108
  - if `m1` captures the entity by copy, `m2` captures the corresponding
1109
  non-static data member of `m1`’s closure type;
1110
  - if `m1` captures the entity by reference, `m2` captures the same
1111
  entity captured by `m1`.
1112
 
1113
+ [*Example 11*:
1114
 
1115
+ The nested *lambda-expression*s and invocations below will output
1116
  `123234`.
1117
 
1118
  ``` cpp
1119
  int a = 1, b = 1, c = 1;
1120
  auto m1 = [a, &b, &c]() mutable {
 
1130
  std::cout << a << b << c;
1131
  ```
1132
 
1133
  — *end example*]
1134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1135
  When the *lambda-expression* is evaluated, the entities that are
1136
  captured by copy are used to direct-initialize each corresponding
1137
  non-static data member of the resulting closure object, and the
1138
  non-static data members corresponding to the *init-capture*s are
1139
  initialized as indicated by the corresponding *initializer* (which may
1140
  be copy- or direct-initialization). (For array members, the array
1141
  elements are direct-initialized in increasing subscript order.) These
1142
  initializations are performed in the (unspecified) order in which the
1143
  non-static data members are declared.
1144
 
1145
+ [*Note 9*: This ensures that the destructions will occur in the reverse
1146
  order of the constructions. — *end note*]
1147
 
1148
+ [*Note 10*: If a non-reference entity is implicitly or explicitly
1149
  captured by reference, invoking the function call operator of the
1150
  corresponding *lambda-expression* after the lifetime of the entity has
1151
  ended is likely to result in undefined behavior. — *end note*]
1152
 
1153
+ A *simple-capture* containing an ellipsis is a pack expansion
1154
+ [[temp.variadic]]. An *init-capture* containing an ellipsis is a pack
1155
+ expansion that introduces an *init-capture* pack [[temp.variadic]] whose
1156
+ declarative region is the *lambda-expression*’s *compound-statement*.
1157
 
1158
+ [*Example 12*:
1159
 
1160
  ``` cpp
1161
  template<class... Args>
1162
  void f(Args... args) {
1163
  auto lm = [&, args...] { return g(args...); };
1164
  lm();
1165
+
1166
+ auto lm2 = [...xs=std::move(args)] { return g(xs...); };
1167
+ lm2();
1168
  }
1169
  ```
1170
 
1171
  — *end example*]
1172
 
1173
  ### Fold expressions <a id="expr.prim.fold">[[expr.prim.fold]]</a>
1174
 
1175
+ A fold expression performs a fold of a pack [[temp.variadic]] over a
1176
+ binary operator.
1177
 
1178
  ``` bnf
1179
  fold-expression:
1180
  '(' cast-expression fold-operator '...' ')'
1181
  '(' '...' fold-operator cast-expression ')'
 
1194
  An expression of the form `(...` *op* `e)` where *op* is a
1195
  *fold-operator* is called a *unary left fold*. An expression of the form
1196
  `(e` *op* `...)` where *op* is a *fold-operator* is called a *unary
1197
  right fold*. Unary left folds and unary right folds are collectively
1198
  called *unary folds*. In a unary fold, the *cast-expression* shall
1199
+ contain an unexpanded pack [[temp.variadic]].
1200
 
1201
  An expression of the form `(e1` *op1* `...` *op2* `e2)` where *op1* and
1202
  *op2* are *fold-operator*s is called a *binary fold*. In a binary fold,
1203
  *op1* and *op2* shall be the same *fold-operator*, and either `e1` shall
1204
+ contain an unexpanded pack or `e2` shall contain an unexpanded pack, but
1205
+ not both. If `e2` contains an unexpanded pack, the expression is called
1206
+ a *binary left fold*. If `e1` contains an unexpanded pack, the
1207
+ expression is called a *binary right fold*.
 
1208
 
1209
  [*Example 1*:
1210
 
1211
  ``` cpp
1212
  template<typename ...Args>
 
1214
  return (true && ... && args); // OK
1215
  }
1216
 
1217
  template<typename ...Args>
1218
  bool f(Args ...args) {
1219
+ return (args + ... + args); // error: both operands contain unexpanded packs
1220
  }
1221
  ```
1222
 
1223
  — *end example*]
1224
 
1225
+ ### Requires expressions <a id="expr.prim.req">[[expr.prim.req]]</a>
1226
+
1227
+ A *requires-expression* provides a concise way to express requirements
1228
+ on template arguments that can be checked by name lookup
1229
+ [[basic.lookup]] or by checking properties of types and expressions.
1230
+
1231
+ ``` bnf
1232
+ requires-expression:
1233
+ requires requirement-parameter-listₒₚₜ requirement-body
1234
+ ```
1235
+
1236
+ ``` bnf
1237
+ requirement-parameter-list:
1238
+ '(' parameter-declaration-clauseₒₚₜ ')'
1239
+ ```
1240
+
1241
+ ``` bnf
1242
+ requirement-body:
1243
+ '{' requirement-seq '}'
1244
+ ```
1245
+
1246
+ ``` bnf
1247
+ requirement-seq:
1248
+ requirement
1249
+ requirement-seq requirement
1250
+ ```
1251
+
1252
+ ``` bnf
1253
+ requirement:
1254
+ simple-requirement
1255
+ type-requirement
1256
+ compound-requirement
1257
+ nested-requirement
1258
+ ```
1259
+
1260
+ A *requires-expression* is a prvalue of type `bool` whose value is
1261
+ described below. Expressions appearing within a *requirement-body* are
1262
+ unevaluated operands [[expr.prop]].
1263
+
1264
+ [*Example 1*:
1265
+
1266
+ A common use of *requires-expression*s is to define requirements in
1267
+ concepts such as the one below:
1268
+
1269
+ ``` cpp
1270
+ template<typename T>
1271
+ concept R = requires (T i) {
1272
+ typename T::type;
1273
+ {*i} -> std::convertible_to<const typename T::type&>;
1274
+ };
1275
+ ```
1276
+
1277
+ A *requires-expression* can also be used in a *requires-clause*
1278
+ [[temp.pre]] as a way of writing ad hoc constraints on template
1279
+ arguments such as the one below:
1280
+
1281
+ ``` cpp
1282
+ template<typename T>
1283
+ requires requires (T x) { x + x; }
1284
+ T add(T a, T b) { return a + b; }
1285
+ ```
1286
+
1287
+ The first `requires` introduces the *requires-clause*, and the second
1288
+ introduces the *requires-expression*.
1289
+
1290
+ — *end example*]
1291
+
1292
+ A *requires-expression* may introduce local parameters using a
1293
+ *parameter-declaration-clause* [[dcl.fct]]. A local parameter of a
1294
+ *requires-expression* shall not have a default argument. Each name
1295
+ introduced by a local parameter is in scope from the point of its
1296
+ declaration until the closing brace of the *requirement-body*. These
1297
+ parameters have no linkage, storage, or lifetime; they are only used as
1298
+ notation for the purpose of defining *requirement*s. The
1299
+ *parameter-declaration-clause* of a *requirement-parameter-list* shall
1300
+ not terminate with an ellipsis.
1301
+
1302
+ [*Example 2*:
1303
+
1304
+ ``` cpp
1305
+ template<typename T>
1306
+ concept C = requires(T t, ...) { // error: terminates with an ellipsis
1307
+ t;
1308
+ };
1309
+ ```
1310
+
1311
+ — *end example*]
1312
+
1313
+ The *requirement-body* contains a sequence of *requirement*s. These
1314
+ *requirement*s may refer to local parameters, template parameters, and
1315
+ any other declarations visible from the enclosing context.
1316
+
1317
+ The substitution of template arguments into a *requires-expression* may
1318
+ result in the formation of invalid types or expressions in its
1319
+ *requirement*s or the violation of the semantic constraints of those
1320
+ *requirement*s. In such cases, the *requires-expression* evaluates to
1321
+ `false`; it does not cause the program to be ill-formed. The
1322
+ substitution and semantic constraint checking proceeds in lexical order
1323
+ and stops when a condition that determines the result of the
1324
+ *requires-expression* is encountered. If substitution (if any) and
1325
+ semantic constraint checking succeed, the *requires-expression*
1326
+ evaluates to `true`.
1327
+
1328
+ [*Note 1*: If a *requires-expression* contains invalid types or
1329
+ expressions in its *requirement*s, and it does not appear within the
1330
+ declaration of a templated entity, then the program is
1331
+ ill-formed. — *end note*]
1332
+
1333
+ If the substitution of template arguments into a *requirement* would
1334
+ always result in a substitution failure, the program is ill-formed; no
1335
+ diagnostic required.
1336
+
1337
+ [*Example 3*:
1338
+
1339
+ ``` cpp
1340
+ template<typename T> concept C =
1341
+ requires {
1342
+ new int[-(int)sizeof(T)]; // ill-formed, no diagnostic required
1343
+ };
1344
+ ```
1345
+
1346
+ — *end example*]
1347
+
1348
+ #### Simple requirements <a id="expr.prim.req.simple">[[expr.prim.req.simple]]</a>
1349
+
1350
+ ``` bnf
1351
+ simple-requirement:
1352
+ expression ';'
1353
+ ```
1354
+
1355
+ A *simple-requirement* asserts the validity of an *expression*.
1356
+
1357
+ [*Note 1*: The enclosing *requires-expression* will evaluate to `false`
1358
+ if substitution of template arguments into the *expression* fails. The
1359
+ *expression* is an unevaluated operand [[expr.prop]]. — *end note*]
1360
+
1361
+ [*Example 1*:
1362
+
1363
+ ``` cpp
1364
+ template<typename T> concept C =
1365
+ requires (T a, T b) {
1366
+ a + b; // C<T> is true if a + b is a valid expression
1367
+ };
1368
+ ```
1369
+
1370
+ — *end example*]
1371
+
1372
+ A *requirement* that starts with a `requires` token is never interpreted
1373
+ as a *simple-requirement*.
1374
+
1375
+ [*Note 2*: This simplifies distinguishing between a
1376
+ *simple-requirement* and a *nested-requirement*. — *end note*]
1377
+
1378
+ #### Type requirements <a id="expr.prim.req.type">[[expr.prim.req.type]]</a>
1379
+
1380
+ ``` bnf
1381
+ type-requirement:
1382
+ typename nested-name-specifierₒₚₜ type-name ';'
1383
+ ```
1384
+
1385
+ A *type-requirement* asserts the validity of a type.
1386
+
1387
+ [*Note 1*: The enclosing *requires-expression* will evaluate to `false`
1388
+ if substitution of template arguments fails. — *end note*]
1389
+
1390
+ [*Example 1*:
1391
+
1392
+ ``` cpp
1393
+ template<typename T, typename T::type = 0> struct S;
1394
+ template<typename T> using Ref = T&;
1395
+
1396
+ template<typename T> concept C = requires {
1397
+ typename T::inner; // required nested member name
1398
+ typename S<T>; // required class template specialization
1399
+ typename Ref<T>; // required alias template substitution, fails if T is void
1400
+ };
1401
+ ```
1402
+
1403
+ — *end example*]
1404
+
1405
+ A *type-requirement* that names a class template specialization does not
1406
+ require that type to be complete [[basic.types]].
1407
+
1408
+ #### Compound requirements <a id="expr.prim.req.compound">[[expr.prim.req.compound]]</a>
1409
+
1410
+ ``` bnf
1411
+ compound-requirement:
1412
+ '{' expression '}' noexceptₒₚₜ return-type-requirementₒₚₜ ';'
1413
+ ```
1414
+
1415
+ ``` bnf
1416
+ return-type-requirement:
1417
+ '->' type-constraint
1418
+ ```
1419
+
1420
+ A *compound-requirement* asserts properties of the *expression* E.
1421
+ Substitution of template arguments (if any) and verification of semantic
1422
+ properties proceed in the following order:
1423
+
1424
+ - Substitution of template arguments (if any) into the *expression* is
1425
+ performed.
1426
+ - If the `noexcept` specifier is present, E shall not be a
1427
+ potentially-throwing expression [[except.spec]].
1428
+ - If the *return-type-requirement* is present, then:
1429
+ - Substitution of template arguments (if any) into the
1430
+ *return-type-requirement* is performed.
1431
+ - The immediately-declared constraint [[temp.param]] of the
1432
+ *type-constraint* for `decltype((E))` shall be satisfied.
1433
+ \[*Example 1*:
1434
+ Given concepts `C` and `D`,
1435
+ ``` cpp
1436
+ requires {
1437
+ { E1 } -> C;
1438
+ { E2 } -> D<A₁, ⋯, Aₙ>;
1439
+ };
1440
+ ```
1441
+
1442
+ is equivalent to
1443
+ ``` cpp
1444
+ requires {
1445
+ E1; requires C<decltype((E1))>;
1446
+ E2; requires D<decltype((E2)), A₁, ⋯, Aₙ>;
1447
+ };
1448
+ ```
1449
+
1450
+ (including in the case where n is zero).
1451
+ — *end example*]
1452
+
1453
+ [*Example 2*:
1454
+
1455
+ ``` cpp
1456
+ template<typename T> concept C1 = requires(T x) {
1457
+ {x++};
1458
+ };
1459
+ ```
1460
+
1461
+ The *compound-requirement* in `C1` requires that `x++` is a valid
1462
+ expression. It is equivalent to the *simple-requirement* `x++;`.
1463
+
1464
+ ``` cpp
1465
+ template<typename T> concept C2 = requires(T x) {
1466
+ {*x} -> std::same_as<typename T::inner>;
1467
+ };
1468
+ ```
1469
+
1470
+ The *compound-requirement* in `C2` requires that `*x` is a valid
1471
+ expression, that `typename T::inner` is a valid type, and that
1472
+ `std::same_as<decltype((*x)), typename T::inner>` is satisfied.
1473
+
1474
+ ``` cpp
1475
+ template<typename T> concept C3 =
1476
+ requires(T x) {
1477
+ {g(x)} noexcept;
1478
+ };
1479
+ ```
1480
+
1481
+ The *compound-requirement* in `C3` requires that `g(x)` is a valid
1482
+ expression and that `g(x)` is non-throwing.
1483
+
1484
+ — *end example*]
1485
+
1486
+ #### Nested requirements <a id="expr.prim.req.nested">[[expr.prim.req.nested]]</a>
1487
+
1488
+ ``` bnf
1489
+ nested-requirement:
1490
+ requires constraint-expression ';'
1491
+ ```
1492
+
1493
+ A *nested-requirement* can be used to specify additional constraints in
1494
+ terms of local parameters. The *constraint-expression* shall be
1495
+ satisfied [[temp.constr.decl]] by the substituted template arguments, if
1496
+ any. Substitution of template arguments into a *nested-requirement* does
1497
+ not result in substitution into the *constraint-expression* other than
1498
+ as specified in [[temp.constr.constr]].
1499
+
1500
+ [*Example 1*:
1501
+
1502
+ ``` cpp
1503
+ template<typename U> concept C = sizeof(U) == 1;
1504
+
1505
+ template<typename T> concept D = requires (T t) {
1506
+ requires C<decltype (+t)>;
1507
+ };
1508
+ ```
1509
+
1510
+ `D<T>` is satisfied if `sizeof(decltype (+t)) == 1`
1511
+ [[temp.constr.atomic]].
1512
+
1513
+ — *end example*]
1514
+
1515
+ A local parameter shall only appear as an unevaluated operand
1516
+ [[expr.prop]] within the *constraint-expression*.
1517
+
1518
+ [*Example 2*:
1519
+
1520
+ ``` cpp
1521
+ template<typename T> concept C = requires (T a) {
1522
+ requires sizeof(a) == 4; // OK
1523
+ requires a == 0; // error: evaluation of a constraint variable
1524
+ };
1525
+ ```
1526
+
1527
+ — *end example*]
1528
+