From Jason Turner

[expr.const]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpweavc5q2/{from.md → to.md} +352 -134
tmp/tmpweavc5q2/{from.md → to.md} RENAMED
@@ -21,23 +21,23 @@ A variable or temporary object `o` is *constant-initialized* if
21
  some initialization being performed, and
22
  - the full-expression of its initialization is a constant expression
23
  when interpreted as a *constant-expression*, except that if `o` is an
24
  object, that full-expression may also invoke constexpr constructors
25
  for `o` and its subobjects even if those objects are of non-literal
26
- class types. \[*Note 2*: Such a class may have a non-trivial
27
  destructor. Within this evaluation, `std::is_constant_evaluated()`
28
  [[meta.const.eval]] returns `true`. — *end note*]
29
 
30
  A variable is *potentially-constant* if it is constexpr or it has
31
- reference or const-qualified integral or enumeration type.
32
 
33
- A constant-initialized potentially-constant variable is *usable in
34
- constant expressions* at a point P if its initializing declaration D is
35
  reachable from P and
36
 
37
- - it is constexpr,
38
- - it is not initialized to a TU-local value, or
39
  - P is in the same translation unit as D.
40
 
41
  An object or reference is *usable in constant expressions* if it is
42
 
43
  - a variable that is usable in constant expressions, or
@@ -50,52 +50,57 @@ An object or reference is *usable in constant expressions* if it is
50
 
51
  An expression E is a *core constant expression* unless the evaluation of
52
  E, following the rules of the abstract machine [[intro.execution]],
53
  would evaluate one of the following:
54
 
55
- - `this` [[expr.prim.this]], except in a constexpr function
56
- [[dcl.constexpr]] that is being evaluated as part of E;
57
- - an invocation of a non-constexpr function \[*Note 3*: Overload
58
- resolution [[over.match]] is applied as usual. *end note*] ;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  - an invocation of an undefined constexpr function;
60
- - an invocation of an instantiated constexpr function that fails to
61
- satisfy the requirements for a constexpr function;
62
  - an invocation of a virtual function [[class.virtual]] for an object
63
- unless
64
- - the object is usable in constant expressions or
65
- - its lifetime began within the evaluation of E;
66
  - an expression that would exceed the implementation-defined limits (see
67
  [[implimits]]);
68
  - an operation that would have undefined behavior as specified in
69
- [[intro]] through [[cpp]] of this document \[*Note 4*: including, for
70
- example, signed integer overflow [[expr.prop]], certain pointer
71
- arithmetic [[expr.add]], division by zero [[expr.mul]], or certain
72
- shift operations [[expr.shift]] — *end note*] ;
73
  - an lvalue-to-rvalue conversion [[conv.lval]] unless it is applied to
74
  - a non-volatile glvalue that refers to an object that is usable in
75
  constant expressions, or
76
  - a non-volatile glvalue of literal type that refers to a non-volatile
77
  object whose lifetime began within the evaluation of E;
78
- - an lvalue-to-rvalue conversion [[conv.lval]] that is applied to a
79
- glvalue that refers to a non-active member of a union or a subobject
80
- thereof;
81
  - an lvalue-to-rvalue conversion that is applied to an object with an
82
  indeterminate value [[basic.indet]];
83
  - an invocation of an implicitly-defined copy/move constructor or
84
  copy/move assignment operator for a union whose active member (if any)
85
  is mutable, unless the lifetime of the union object began within the
86
  evaluation of E;
87
- - an *id-expression* that refers to a variable or data member of
88
- reference type unless the reference has a preceding initialization and
89
- either
90
- - it is usable in constant expressions or
91
- - its lifetime began within the evaluation of E;
92
  - in a *lambda-expression*, a reference to `this` or to a variable with
93
  automatic storage duration defined outside that *lambda-expression*,
94
- where the reference would be an odr-use ([[basic.def.odr]],
95
- [[expr.prim.lambda]]);
96
- \[*Example 1*:
97
  ``` cpp
98
  void g() {
99
  const int n = 0;
100
  [=] {
101
  constexpr int i = n; // OK, n is not odr-used here
@@ -103,17 +108,17 @@ would evaluate one of the following:
103
  };
104
  }
105
  ```
106
 
107
  — *end example*]
108
- \[*Note 5*:
109
  If the odr-use occurs in an invocation of a function call operator of
110
  a closure type, it no longer refers to `this` or to an enclosing
111
  automatic variable due to the transformation
112
  [[expr.prim.lambda.capture]] of the *id-expression* into an access of
113
  the corresponding data member.
114
- \[*Example 2*:
115
  ``` cpp
116
  auto monad = [](auto v) { return [=] { return v; }; };
117
  auto bind = [](auto m) {
118
  return [=](auto fvm) { return fvm(m()); };
119
  };
@@ -124,17 +129,21 @@ would evaluate one of the following:
124
 
125
  — *end example*]
126
  — *end note*]
127
  - a conversion from type cv `void*` to a pointer-to-object type;
128
  - a `reinterpret_cast` [[expr.reinterpret.cast]];
129
- - a modification of an object ([[expr.ass]], [[expr.post.incr]],
130
- [[expr.pre.incr]]) unless it is applied to a non-volatile lvalue of
131
- literal type that refers to a non-volatile object whose lifetime began
132
- within the evaluation of E;
 
 
 
 
133
  - a *new-expression* [[expr.new]], unless the selected allocation
134
- function is a replaceable global allocation function (
135
- [[new.delete.single]], [[new.delete.array]]) and the allocated storage
136
  is deallocated within the evaluation of E;
137
  - a *delete-expression* [[expr.delete]], unless it deallocates a region
138
  of storage allocated within the evaluation of E;
139
  - a call to an instance of `std::allocator<T>::allocate`
140
  [[allocator.members]], unless the allocated storage is deallocated
@@ -144,41 +153,53 @@ would evaluate one of the following:
144
  allocated within the evaluation of E;
145
  - an *await-expression* [[expr.await]];
146
  - a *yield-expression* [[expr.yield]];
147
  - a three-way comparison [[expr.spaceship]], relational [[expr.rel]], or
148
  equality [[expr.eq]] operator where the result is unspecified;
149
- - a *throw-expression* [[expr.throw]] or a dynamic cast
150
- [[expr.dynamic.cast]] or `typeid` [[expr.typeid]] expression that
151
- would throw an exception;
152
- - an *asm-declaration* [[dcl.asm]]; or
153
- - an invocation of the `va_arg` macro [[cstdarg.syn]].
 
 
 
154
 
155
- If E satisfies the constraints of a core constant expression, but
156
- evaluation of E would evaluate an operation that has undefined behavior
157
- as specified in [[library]] through [[thread]] of this document, or an
158
- invocation of the `va_start` macro [[cstdarg.syn]], it is unspecified
159
- whether E is a core constant expression.
160
 
161
- [*Example 3*:
 
 
 
 
 
 
 
 
 
 
 
162
 
163
  ``` cpp
164
  int x; // not constant
165
  struct A {
166
  constexpr A(bool b) : m(b?42:x) { }
167
  int m;
168
  };
169
- constexpr int v = A(true).m; // OK: constructor call initializes m with the value 42
170
 
171
  constexpr int w = A(false).m; // error: initializer for m is x, which is non-constant
172
 
173
  constexpr int f1(int k) {
174
  constexpr int x = k; // error: x is not initialized by a constant expression
175
  // because lifetime of k began outside the initializer of x
176
  return x;
177
  }
178
  constexpr int f2(int k) {
179
- int x = k; // OK: not required to be a constant expression
180
  // because x is not constexpr
181
  return x;
182
  }
183
 
184
  constexpr int incr(int &n) {
@@ -188,67 +209,132 @@ constexpr int g(int k) {
188
  constexpr int x = incr(k); // error: incr(k) is not a core constant expression
189
  // because lifetime of k began outside the expression incr(k)
190
  return x;
191
  }
192
  constexpr int h(int k) {
193
- int x = incr(k); // OK: incr(k) is not required to be a core constant expression
194
  return x;
195
  }
196
- constexpr int y = h(1); // OK: initializes y with the value 2
197
  // h(1) is a core constant expression because
198
  // the lifetime of k begins inside h(1)
199
  ```
200
 
201
  — *end example*]
202
 
203
  For the purposes of determining whether an expression E is a core
204
- constant expression, the evaluation of a call to a member function of
205
  `std::allocator<T>` as defined in [[allocator.members]], where `T` is a
206
- literal type, does not disqualify E from being a core constant
207
- expression, even if the actual evaluation of such a call would otherwise
208
- fail the requirements for a core constant expression. Similarly, the
209
- evaluation of a call to `std::destroy_at`, `std::ranges::destroy_at`,
210
- `std::construct_at`, or `std::ranges::construct_at` does not disqualify
211
- E from being a core constant expression unless:
212
-
213
- - for a call to `std::construct_at` or `std::ranges::construct_at`, the
214
- first argument, of type `T*`, does not point to storage allocated with
215
- `std::allocator<T>` or to an object whose lifetime began within the
216
- evaluation of E, or the evaluation of the underlying constructor call
217
- disqualifies E from being a core constant expression, or
218
- - for a call to `std::destroy_at` or `std::ranges::destroy_at`, the
219
- first argument, of type `T*`, does not point to storage allocated with
220
- `std::allocator<T>` or to an object whose lifetime began within the
221
- evaluation of E, or the evaluation of the underlying destructor call
222
- disqualifies E from being a core constant expression.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
 
224
  An object `a` is said to have *constant destruction* if:
225
 
226
- - it is not of class type nor (possibly multi-dimensional) array
227
- thereof, or
228
- - it is of class type or (possibly multi-dimensional) array thereof,
229
- that class type has a constexpr destructor, and for a hypothetical
230
  expression E whose only effect is to destroy `a`, E would be a core
231
  constant expression if the lifetime of `a` and its non-mutable
232
  subobjects (but not its mutable subobjects) were considered to start
233
  within E.
234
 
235
  An *integral constant expression* is an expression of integral or
236
  unscoped enumeration type, implicitly converted to a prvalue, where the
237
  converted expression is a core constant expression.
238
 
239
- [*Note 6*: Such expressions may be used as bit-field lengths
240
  [[class.bit]], as enumerator initializers if the underlying type is not
241
  fixed [[dcl.enum]], and as alignments [[dcl.align]]. — *end note*]
242
 
243
  If an expression of literal class type is used in a context where an
244
  integral constant expression is required, then that expression is
245
  contextually implicitly converted [[conv]] to an integral or unscoped
246
  enumeration type and the selected conversion function shall be
247
  `constexpr`.
248
 
249
- [*Example 4*:
250
 
251
  ``` cpp
252
  struct A {
253
  constexpr A(int i) : val(i) { }
254
  constexpr operator int() const { return val; }
@@ -280,11 +366,11 @@ constant expression and the implicit conversion sequence contains only
280
  and
281
  - function pointer conversions [[conv.fctptr]],
282
 
283
  and where the reference binding (if any) binds directly.
284
 
285
- [*Note 7*: Such expressions may be used in `new` expressions
286
  [[expr.new]], as case expressions [[stmt.switch]], as enumerator
287
  initializers if the underlying type is fixed [[dcl.enum]], as array
288
  bounds [[dcl.array]], and as non-type template arguments
289
  [[temp.arg]]. — *end note*]
290
 
@@ -299,10 +385,12 @@ expression (as defined below), or a prvalue core constant expression
299
  whose value satisfies the following constraints:
300
 
301
  - if the value is an object of class type, each non-static data member
302
  of reference type refers to an entity that is a permitted result of a
303
  constant expression,
 
 
304
  - if the value is of pointer type, it contains the address of an object
305
  with static storage duration, the address past the end of such an
306
  object [[expr.add]], the address of a non-immediate function, or a
307
  null pointer value,
308
  - if the value is of pointer-to-member-function type, it does not
@@ -313,11 +401,15 @@ whose value satisfies the following constraints:
313
  An entity is a *permitted result of a constant expression* if it is an
314
  object with static storage duration that either is not a temporary
315
  object or is a temporary object whose value satisfies the above
316
  constraints, or if it is a non-immediate function.
317
 
318
- [*Example 5*:
 
 
 
 
319
 
320
  ``` cpp
321
  consteval int f() { return 42; }
322
  consteval auto g() { return f; }
323
  consteval int h(int (*p)() = g()) { return p(); }
@@ -326,19 +418,23 @@ constexpr auto e = g(); // error: a pointer to an immedi
326
  // not a permitted result of a constant expression
327
  ```
328
 
329
  — *end example*]
330
 
331
- [*Note 8*:
 
 
 
 
332
 
333
  Since this document imposes no restrictions on the accuracy of
334
  floating-point operations, it is unspecified whether the evaluation of a
335
  floating-point expression during translation yields the same result as
336
  the evaluation of the same expression (or the same operations on the
337
- same values) during program execution.[^32]
338
 
339
- [*Example 6*:
340
 
341
  ``` cpp
342
  bool f() {
343
  char array[1 + int(1 + 0.2 - 0.1 - 0.1)]; // Must be evaluated during translation
344
  int size = 1 + int(1 + 0.2 - 0.1 - 0.1); // May be evaluated at runtime
@@ -351,27 +447,121 @@ It is unspecified whether the value of `f()` will be `true` or `false`.
351
  — *end example*]
352
 
353
  — *end note*]
354
 
355
  An expression or conversion is in an *immediate function context* if it
356
- is potentially evaluated and its innermost non-block scope is a function
357
- parameter scope of an immediate function. An expression or conversion is
358
- an *immediate invocation* if it is a potentially-evaluated explicit or
359
- implicit invocation of an immediate function and is not in an immediate
360
- function context. An immediate invocation shall be a constant
361
- expression.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
 
363
  An expression or conversion is *manifestly constant-evaluated* if it is:
364
 
365
  - a *constant-expression*, or
366
  - the condition of a constexpr if statement [[stmt.if]], or
367
  - an immediate invocation, or
368
  - the result of substitution into an atomic constraint expression to
369
  determine whether it is satisfied [[temp.constr.atomic]], or
370
  - the initializer of a variable that is usable in constant expressions
371
- or has constant initialization.[^33]
372
- \[*Example 7*:
373
  ``` cpp
374
  template<bool> struct X {};
375
  X<std::is_constant_evaluated()> x; // type X<true>
376
  int y;
377
  const int a = std::is_constant_evaluated() ? y : 1; // dynamic initialization to 1
@@ -380,44 +570,44 @@ An expression or conversion is *manifestly constant-evaluated* if it is:
380
  const int b = std::is_constant_evaluated() ? 2 : y; // static initialization to 2
381
  int c = y + (std::is_constant_evaluated() ? 2 : y); // dynamic initialization to y+y
382
 
383
  constexpr int f() {
384
  const int n = std::is_constant_evaluated() ? 13 : 17; // n is 13
385
- int m = std::is_constant_evaluated() ? 13 : 17; // m might be 13 or 17 (see below)
386
  char arr[n] = {}; // char[13]
387
  return m + sizeof(arr);
388
  }
389
  int p = f(); // m is 13; initialized to 26
390
  int q = p + f(); // m is 17 for this call; initialized to 56
391
  ```
392
 
393
  — *end example*]
394
 
395
- [*Note 9*: A manifestly constant-evaluated expression is evaluated even
396
- in an unevaluated operand. — *end note*]
 
397
 
398
  An expression or conversion is *potentially constant evaluated* if it
399
  is:
400
 
401
  - a manifestly constant-evaluated expression,
402
  - a potentially-evaluated expression [[basic.def.odr]],
403
- - an immediate subexpression of a *braced-init-list*, [^34]
404
  - an expression of the form `&` *cast-expression* that occurs within a
405
- templated entity, [^35] or
406
- - a subexpression of one of the above that is not a subexpression of a
407
- nested unevaluated operand.
408
 
409
  A function or variable is *needed for constant evaluation* if it is:
410
 
411
  - a constexpr function that is named by an expression [[basic.def.odr]]
412
  that is potentially constant evaluated, or
413
- - a variable whose name appears as a potentially constant evaluated
414
- expression that is either a constexpr variable or is of non-volatile
415
- const-qualified integral type or of reference type.
416
 
417
  <!-- Link reference definitions -->
418
- [allocator.members]: utilities.md#allocator.members
419
  [bad.alloc]: support.md#bad.alloc
420
  [bad.cast]: support.md#bad.cast
421
  [bad.typeid]: support.md#bad.typeid
422
  [basic.align]: basic.md#basic.align
423
  [basic.compound]: basic.md#basic.compound
@@ -425,23 +615,26 @@ A function or variable is *needed for constant evaluation* if it is:
425
  [basic.fundamental]: basic.md#basic.fundamental
426
  [basic.indet]: basic.md#basic.indet
427
  [basic.life]: basic.md#basic.life
428
  [basic.lookup]: basic.md#basic.lookup
429
  [basic.lookup.argdep]: basic.md#basic.lookup.argdep
430
- [basic.lookup.classref]: basic.md#basic.lookup.classref
 
431
  [basic.lookup.unqual]: basic.md#basic.lookup.unqual
432
  [basic.lval]: #basic.lval
433
- [basic.namespace]: dcl.md#basic.namespace
434
  [basic.scope.block]: basic.md#basic.scope.block
435
  [basic.scope.class]: basic.md#basic.scope.class
 
436
  [basic.start.main]: basic.md#basic.start.main
 
437
  [basic.stc.dynamic]: basic.md#basic.stc.dynamic
438
  [basic.stc.dynamic.allocation]: basic.md#basic.stc.dynamic.allocation
439
  [basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
440
- [basic.stc.dynamic.safety]: basic.md#basic.stc.dynamic.safety
 
441
  [basic.type.qualifier]: basic.md#basic.type.qualifier
442
- [basic.types]: basic.md#basic.types
443
  [class]: class.md#class
444
  [class.abstract]: class.md#class.abstract
445
  [class.access]: class.md#class.access
446
  [class.access.base]: class.md#class.access.base
447
  [class.base.init]: class.md#class.base.init
@@ -456,31 +649,32 @@ A function or variable is *needed for constant evaluation* if it is:
456
  [class.derived]: class.md#class.derived
457
  [class.dtor]: class.md#class.dtor
458
  [class.free]: class.md#class.free
459
  [class.friend]: class.md#class.friend
460
  [class.mem]: class.md#class.mem
461
- [class.member.lookup]: class.md#class.member.lookup
462
  [class.mfct]: class.md#class.mfct
463
- [class.mfct.non-static]: class.md#class.mfct.non-static
464
  [class.mi]: class.md#class.mi
465
  [class.prop]: class.md#class.prop
466
- [class.qual]: basic.md#class.qual
467
  [class.spaceship]: class.md#class.spaceship
468
- [class.static]: class.md#class.static
469
  [class.temporary]: basic.md#class.temporary
470
- [class.this]: class.md#class.this
471
  [class.union]: class.md#class.union
 
472
  [class.virtual]: class.md#class.virtual
473
  [cmp.categories]: support.md#cmp.categories
 
474
  [conv]: #conv
475
  [conv.array]: #conv.array
476
  [conv.bool]: #conv.bool
477
  [conv.double]: #conv.double
478
  [conv.fctptr]: #conv.fctptr
479
  [conv.fpint]: #conv.fpint
480
  [conv.fpprom]: #conv.fpprom
481
  [conv.func]: #conv.func
 
482
  [conv.integral]: #conv.integral
483
  [conv.lval]: #conv.lval
484
  [conv.mem]: #conv.mem
485
  [conv.prom]: #conv.prom
486
  [conv.ptr]: #conv.ptr
@@ -491,10 +685,11 @@ A function or variable is *needed for constant evaluation* if it is:
491
  [cstdarg.syn]: support.md#cstdarg.syn
492
  [cstddef.syn]: support.md#cstddef.syn
493
  [dcl.align]: dcl.md#dcl.align
494
  [dcl.array]: dcl.md#dcl.array
495
  [dcl.asm]: dcl.md#dcl.asm
 
496
  [dcl.constexpr]: dcl.md#dcl.constexpr
497
  [dcl.dcl]: dcl.md#dcl.dcl
498
  [dcl.decl]: dcl.md#dcl.decl
499
  [dcl.enum]: dcl.md#dcl.enum
500
  [dcl.fct]: dcl.md#dcl.fct
@@ -514,17 +709,20 @@ A function or variable is *needed for constant evaluation* if it is:
514
  [dcl.ref]: dcl.md#dcl.ref
515
  [dcl.spec.auto]: dcl.md#dcl.spec.auto
516
  [dcl.stc]: dcl.md#dcl.stc
517
  [dcl.struct.bind]: dcl.md#dcl.struct.bind
518
  [dcl.type]: dcl.md#dcl.type
 
519
  [dcl.type.cv]: dcl.md#dcl.type.cv
 
 
520
  [dcl.type.simple]: dcl.md#dcl.type.simple
521
  [defns.access]: intro.md#defns.access
 
522
  [depr.arith.conv.enum]: future.md#depr.arith.conv.enum
523
  [depr.array.comp]: future.md#depr.array.comp
524
  [depr.capture.this]: future.md#depr.capture.this
525
- [depr.comma.subscript]: future.md#depr.comma.subscript
526
  [depr.volatile.type]: future.md#depr.volatile.type
527
  [except]: except.md#except
528
  [except.handle]: except.md#except.handle
529
  [except.pre]: except.md#except.pre
530
  [except.spec]: except.md#except.spec
@@ -553,26 +751,30 @@ A function or variable is *needed for constant evaluation* if it is:
553
  [expr.mptr.oper]: #expr.mptr.oper
554
  [expr.mul]: #expr.mul
555
  [expr.new]: #expr.new
556
  [expr.or]: #expr.or
557
  [expr.post]: #expr.post
 
558
  [expr.post.incr]: #expr.post.incr
559
  [expr.pre]: #expr.pre
560
  [expr.pre.incr]: #expr.pre.incr
561
  [expr.prim]: #expr.prim
562
  [expr.prim.fold]: #expr.prim.fold
563
  [expr.prim.id]: #expr.prim.id
564
  [expr.prim.id.dtor]: #expr.prim.id.dtor
 
565
  [expr.prim.id.qual]: #expr.prim.id.qual
566
  [expr.prim.id.unqual]: #expr.prim.id.unqual
567
  [expr.prim.lambda]: #expr.prim.lambda
568
  [expr.prim.lambda.capture]: #expr.prim.lambda.capture
569
  [expr.prim.lambda.closure]: #expr.prim.lambda.closure
 
570
  [expr.prim.literal]: #expr.prim.literal
571
  [expr.prim.paren]: #expr.prim.paren
572
  [expr.prim.req]: #expr.prim.req
573
  [expr.prim.req.compound]: #expr.prim.req.compound
 
574
  [expr.prim.req.nested]: #expr.prim.req.nested
575
  [expr.prim.req.simple]: #expr.prim.req.simple
576
  [expr.prim.req.type]: #expr.prim.req.type
577
  [expr.prim.this]: #expr.prim.this
578
  [expr.prop]: #expr.prop
@@ -587,10 +789,11 @@ A function or variable is *needed for constant evaluation* if it is:
587
  [expr.throw]: #expr.throw
588
  [expr.type]: #expr.type
589
  [expr.type.conv]: #expr.type.conv
590
  [expr.typeid]: #expr.typeid
591
  [expr.unary]: #expr.unary
 
592
  [expr.unary.noexcept]: #expr.unary.noexcept
593
  [expr.unary.op]: #expr.unary.op
594
  [expr.xor]: #expr.xor
595
  [expr.yield]: #expr.yield
596
  [function.objects]: utilities.md#function.objects
@@ -602,35 +805,41 @@ A function or variable is *needed for constant evaluation* if it is:
602
  [lex.ext]: lex.md#lex.ext
603
  [lex.icon]: lex.md#lex.icon
604
  [lex.literal]: lex.md#lex.literal
605
  [lex.string]: lex.md#lex.string
606
  [library]: library.md#library
607
- [meta.const.eval]: utilities.md#meta.const.eval
608
- [namespace.qual]: basic.md#namespace.qual
609
  [new.badlength]: support.md#new.badlength
610
  [new.delete.array]: support.md#new.delete.array
611
  [new.delete.placement]: support.md#new.delete.placement
612
  [new.delete.single]: support.md#new.delete.single
613
  [over]: over.md#over
614
  [over.ass]: over.md#over.ass
615
  [over.best.ics]: over.md#over.best.ics
616
  [over.built]: over.md#over.built
617
  [over.call]: over.md#over.call
 
618
  [over.ics.user]: over.md#over.ics.user
619
  [over.literal]: over.md#over.literal
620
  [over.match]: over.md#over.match
621
  [over.match.class.deduct]: over.md#over.match.class.deduct
622
  [over.match.oper]: over.md#over.match.oper
623
  [over.match.viable]: over.md#over.match.viable
624
  [over.oper]: over.md#over.oper
625
  [over.over]: over.md#over.over
 
626
  [replacement.functions]: library.md#replacement.functions
627
  [special]: class.md#special
 
 
628
  [stmt.if]: stmt.md#stmt.if
629
  [stmt.iter]: stmt.md#stmt.iter
630
  [stmt.jump]: stmt.md#stmt.jump
 
631
  [stmt.return]: stmt.md#stmt.return
 
632
  [stmt.switch]: stmt.md#stmt.switch
633
  [support.runtime]: support.md#support.runtime
634
  [support.types.layout]: support.md#support.types.layout
635
  [temp.arg]: temp.md#temp.arg
636
  [temp.concept]: temp.md#temp.concept
@@ -638,17 +847,25 @@ A function or variable is *needed for constant evaluation* if it is:
638
  [temp.constr.constr]: temp.md#temp.constr.constr
639
  [temp.constr.decl]: temp.md#temp.constr.decl
640
  [temp.dep.constexpr]: temp.md#temp.dep.constexpr
641
  [temp.expl.spec]: temp.md#temp.expl.spec
642
  [temp.explicit]: temp.md#temp.explicit
 
643
  [temp.names]: temp.md#temp.names
 
644
  [temp.param]: temp.md#temp.param
645
  [temp.pre]: temp.md#temp.pre
646
  [temp.res]: temp.md#temp.res
 
647
  [temp.variadic]: temp.md#temp.variadic
 
 
 
 
648
  [thread]: thread.md#thread
649
  [type.info]: support.md#type.info
 
650
 
651
  [^1]: The precedence of operators is not directly specified, but it can
652
  be derived from the syntax.
653
 
654
  [^2]: Overloaded operators are never assumed to be associative or
@@ -657,11 +874,11 @@ A function or variable is *needed for constant evaluation* if it is:
657
  [^3]: The cast and assignment operators must still perform their
658
  specific conversions as described in  [[expr.type.conv]],
659
  [[expr.cast]], [[expr.static.cast]] and  [[expr.ass]].
660
 
661
  [^4]: The intent of this list is to specify those circumstances in which
662
- an object may or may not be aliased.
663
 
664
  [^5]: For historical reasons, this conversion is called the
665
  “lvalue-to-rvalue” conversion, even though that name does not
666
  accurately reflect the taxonomy of expressions described in 
667
  [[basic.lval]].
@@ -675,23 +892,23 @@ A function or variable is *needed for constant evaluation* if it is:
675
  be obtained.
676
 
677
  [^8]: The rule for conversion of pointers to members (from pointer to
678
  member of base to pointer to member of derived) appears inverted
679
  compared to the rule for pointers to objects (from pointer to
680
- derived to pointer to base) ([[conv.ptr]], [[class.derived]]). This
681
  inversion is necessary to ensure type safety. Note that a pointer to
682
  member is not an object pointer or a function pointer and the rules
683
  for conversions of such pointers do not apply to pointers to
684
  members. In particular, a pointer to member cannot be converted to a
685
  `void*`.
686
 
687
  [^9]: As a consequence, operands of type `bool`, `char8_t`, `char16_t`,
688
- `char32_t`, `wchar_t`, or an enumerated type are converted to some
689
  integral type.
690
 
691
  [^10]: This also applies when the object expression is an implicit
692
- `(*this)` ([[class.mfct.non-static]]).
693
 
694
  [^11]: This is true even if the subscript operator is used in the
695
  following common idiom: `&x[0]`.
696
 
697
  [^12]: If the class member access expression is evaluated, the
@@ -708,38 +925,37 @@ A function or variable is *needed for constant evaluation* if it is:
708
  [^15]: The recommended name for such a class is `extended_type_info`.
709
 
710
  [^16]: If `p` is an expression of pointer type, then `*p`, `(*p)`,
711
  `*(p)`, `((*p))`, `*((p))`, and so on all meet this requirement.
712
 
713
- [^17]: The types may have different cv-qualifiers, subject to the
714
  overall restriction that a `reinterpret_cast` cannot cast away
715
  constness.
716
 
717
- [^18]: `T1` and `T2` may have different cv-qualifiers, subject to the
718
  overall restriction that a `reinterpret_cast` cannot cast away
719
  constness.
720
 
721
- [^19]: This is sometimes referred to as a *type pun* when the result
722
  refers to the same object as the source glvalue.
723
 
724
- [^20]: `const_cast`
 
725
 
726
- is not limited to conversions that cast away a const-qualifier.
727
 
728
- [^21]: `sizeof(bool)` is not required to be `1`.
729
-
730
- [^22]: The actual size of a potentially-overlapping subobject may be
731
  less than the result of applying `sizeof` to the subobject, due to
732
  virtual base classes and less strict padding requirements on
733
  potentially-overlapping subobjects.
734
 
735
  [^23]: If the conversion function returns a signed integer type, the
736
  second standard conversion converts to the unsigned type
737
  `std::size_t` and thus thwarts any attempt to detect a negative
738
  value afterwards.
739
 
740
- [^24]: This may include evaluating a *new-initializer* and/or calling a
741
  constructor.
742
 
743
  [^25]: A *lambda-expression* with a *lambda-introducer* that consists of
744
  empty square brackets can follow the `delete` keyword if the
745
  *lambda-expression* is enclosed in parentheses.
@@ -767,17 +983,19 @@ A function or variable is *needed for constant evaluation* if it is:
767
 
768
  [^31]: As specified in [[basic.compound]], an object that is not an
769
  array element is considered to belong to a single-element array for
770
  this purpose.
771
 
772
- [^32]: Nonetheless, implementations should provide consistent results,
773
- irrespective of whether the evaluation was performed during
774
- translation and/or during program execution.
775
 
776
- [^33]: Testing this condition may involve a trial evaluation of its
 
 
 
 
777
  initializer as described above.
778
 
779
- [^34]: Constant evaluation may be necessary to determine whether a
780
- narrowing conversion is performed [[dcl.init.list]].
781
 
782
- [^35]: Constant evaluation may be necessary to determine whether such an
783
- expression is value-dependent [[temp.dep.constexpr]].
 
21
  some initialization being performed, and
22
  - the full-expression of its initialization is a constant expression
23
  when interpreted as a *constant-expression*, except that if `o` is an
24
  object, that full-expression may also invoke constexpr constructors
25
  for `o` and its subobjects even if those objects are of non-literal
26
+ class types. \[*Note 2*: Such a class can have a non-trivial
27
  destructor. Within this evaluation, `std::is_constant_evaluated()`
28
  [[meta.const.eval]] returns `true`. — *end note*]
29
 
30
  A variable is *potentially-constant* if it is constexpr or it has
31
+ reference or non-volatile const-qualified integral or enumeration type.
32
 
33
+ A constant-initialized potentially-constant variable V is *usable in
34
+ constant expressions* at a point P if V’s initializing declaration D is
35
  reachable from P and
36
 
37
+ - V is constexpr,
38
+ - V is not initialized to a TU-local value, or
39
  - P is in the same translation unit as D.
40
 
41
  An object or reference is *usable in constant expressions* if it is
42
 
43
  - a variable that is usable in constant expressions, or
 
50
 
51
  An expression E is a *core constant expression* unless the evaluation of
52
  E, following the rules of the abstract machine [[intro.execution]],
53
  would evaluate one of the following:
54
 
55
+ - `this` [[expr.prim.this]], except
56
+ - in a constexpr function [[dcl.constexpr]] that is being evaluated as
57
+ part of E or
58
+ - when appearing as the *postfix-expression* of an implicit or
59
+ explicit class member access expression [[expr.ref]];
60
+ - a control flow that passes through a declaration of a variable with
61
+ static [[basic.stc.static]] or thread [[basic.stc.thread]] storage
62
+ duration, unless that variable is usable in constant expressions;
63
+ \[*Example 1*:
64
+ ``` cpp
65
+ constexpr char test() {
66
+ static const int x = 5;
67
+ static constexpr char c[] = "Hello World";
68
+ return *(c + x);
69
+ }
70
+ static_assert(' ' == test());
71
+ ```
72
+
73
+ — *end example*]
74
+ - an invocation of a non-constexpr function;[^32]
75
  - an invocation of an undefined constexpr function;
76
+ - an invocation of an instantiated constexpr function that is not
77
+ constexpr-suitable;
78
  - an invocation of a virtual function [[class.virtual]] for an object
79
+ whose dynamic type is constexpr-unknown;
 
 
80
  - an expression that would exceed the implementation-defined limits (see
81
  [[implimits]]);
82
  - an operation that would have undefined behavior as specified in
83
+ [[intro]] through [[cpp]], excluding [[dcl.attr.assume]];[^33]
 
 
 
84
  - an lvalue-to-rvalue conversion [[conv.lval]] unless it is applied to
85
  - a non-volatile glvalue that refers to an object that is usable in
86
  constant expressions, or
87
  - a non-volatile glvalue of literal type that refers to a non-volatile
88
  object whose lifetime began within the evaluation of E;
89
+ - an lvalue-to-rvalue conversion that is applied to a glvalue that
90
+ refers to a non-active member of a union or a subobject thereof;
 
91
  - an lvalue-to-rvalue conversion that is applied to an object with an
92
  indeterminate value [[basic.indet]];
93
  - an invocation of an implicitly-defined copy/move constructor or
94
  copy/move assignment operator for a union whose active member (if any)
95
  is mutable, unless the lifetime of the union object began within the
96
  evaluation of E;
 
 
 
 
 
97
  - in a *lambda-expression*, a reference to `this` or to a variable with
98
  automatic storage duration defined outside that *lambda-expression*,
99
+ where the reference would be an odr-use
100
+ [[term.odr.use]], [[expr.prim.lambda]];
101
+ \[*Example 2*:
102
  ``` cpp
103
  void g() {
104
  const int n = 0;
105
  [=] {
106
  constexpr int i = n; // OK, n is not odr-used here
 
108
  };
109
  }
110
  ```
111
 
112
  — *end example*]
113
+ \[*Note 3*:
114
  If the odr-use occurs in an invocation of a function call operator of
115
  a closure type, it no longer refers to `this` or to an enclosing
116
  automatic variable due to the transformation
117
  [[expr.prim.lambda.capture]] of the *id-expression* into an access of
118
  the corresponding data member.
119
+ \[*Example 3*:
120
  ``` cpp
121
  auto monad = [](auto v) { return [=] { return v; }; };
122
  auto bind = [](auto m) {
123
  return [=](auto fvm) { return fvm(m()); };
124
  };
 
129
 
130
  — *end example*]
131
  — *end note*]
132
  - a conversion from type cv `void*` to a pointer-to-object type;
133
  - a `reinterpret_cast` [[expr.reinterpret.cast]];
134
+ - a modification of an object
135
+ [[expr.ass]], [[expr.post.incr]], [[expr.pre.incr]] unless it is
136
+ applied to a non-volatile lvalue of literal type that refers to a
137
+ non-volatile object whose lifetime began within the evaluation of E;
138
+ - an invocation of a destructor [[class.dtor]] or a function call whose
139
+ *postfix-expression* names a pseudo-destructor [[expr.call]], in
140
+ either case for an object whose lifetime did not begin within the
141
+ evaluation of E;
142
  - a *new-expression* [[expr.new]], unless the selected allocation
143
+ function is a replaceable global allocation function
144
+ [[new.delete.single]], [[new.delete.array]] and the allocated storage
145
  is deallocated within the evaluation of E;
146
  - a *delete-expression* [[expr.delete]], unless it deallocates a region
147
  of storage allocated within the evaluation of E;
148
  - a call to an instance of `std::allocator<T>::allocate`
149
  [[allocator.members]], unless the allocated storage is deallocated
 
153
  allocated within the evaluation of E;
154
  - an *await-expression* [[expr.await]];
155
  - a *yield-expression* [[expr.yield]];
156
  - a three-way comparison [[expr.spaceship]], relational [[expr.rel]], or
157
  equality [[expr.eq]] operator where the result is unspecified;
158
+ - a *throw-expression* [[expr.throw]];
159
+ - a `dynamic_cast` [[expr.dynamic.cast]] or `typeid` [[expr.typeid]]
160
+ expression on a glvalue that refers to an object whose dynamic type is
161
+ constexpr-unknown or that would throw an exception;
162
+ - an *asm-declaration* [[dcl.asm]];
163
+ - an invocation of the `va_arg` macro [[cstdarg.syn]];
164
+ - a non-constant library call [[defns.nonconst.libcall]]; or
165
+ - a `goto` statement [[stmt.goto]].
166
 
167
+ It is unspecified whether E is a core constant expression if E satisfies
168
+ the constraints of a core constant expression, but evaluation of E would
169
+ evaluate
 
 
170
 
171
+ - an operation that has undefined behavior as specified in [[library]]
172
+ through [[thread]],
173
+ - an invocation of the `va_start` macro [[cstdarg.syn]], or
174
+ - a statement with an assumption [[dcl.attr.assume]] whose converted
175
+ *conditional-expression*, if evaluated where the assumption appears,
176
+ would not disqualify E from being a core constant expression and would
177
+ not evaluate to `true`. \[*Note 4*: E is not disqualified from being a
178
+ core constant expression if the hypothetical evaluation of the
179
+ converted *conditional-expression* would disqualify E from being a
180
+ core constant expression. — *end note*]
181
+
182
+ [*Example 4*:
183
 
184
  ``` cpp
185
  int x; // not constant
186
  struct A {
187
  constexpr A(bool b) : m(b?42:x) { }
188
  int m;
189
  };
190
+ constexpr int v = A(true).m; // OK, constructor call initializes m with the value 42
191
 
192
  constexpr int w = A(false).m; // error: initializer for m is x, which is non-constant
193
 
194
  constexpr int f1(int k) {
195
  constexpr int x = k; // error: x is not initialized by a constant expression
196
  // because lifetime of k began outside the initializer of x
197
  return x;
198
  }
199
  constexpr int f2(int k) {
200
+ int x = k; // OK, not required to be a constant expression
201
  // because x is not constexpr
202
  return x;
203
  }
204
 
205
  constexpr int incr(int &n) {
 
209
  constexpr int x = incr(k); // error: incr(k) is not a core constant expression
210
  // because lifetime of k began outside the expression incr(k)
211
  return x;
212
  }
213
  constexpr int h(int k) {
214
+ int x = incr(k); // OK, incr(k) is not required to be a core constant expression
215
  return x;
216
  }
217
+ constexpr int y = h(1); // OK, initializes y with the value 2
218
  // h(1) is a core constant expression because
219
  // the lifetime of k begins inside h(1)
220
  ```
221
 
222
  — *end example*]
223
 
224
  For the purposes of determining whether an expression E is a core
225
+ constant expression, the evaluation of the body of a member function of
226
  `std::allocator<T>` as defined in [[allocator.members]], where `T` is a
227
+ literal type, is ignored. Similarly, the evaluation of the body of
228
+ `std::construct_at` or `std::ranges::construct_at` is considered to
229
+ include only the underlying constructor call if the first argument (of
230
+ type `T*`) points to storage allocated with `std::allocator<T>` or to an
231
+ object whose lifetime began within the evaluation of E.
232
+
233
+ For the purposes of determining whether E is a core constant expression,
234
+ the evaluation of a call to a trivial copy/move constructor or copy/move
235
+ assignment operator of a union is considered to copy/move the active
236
+ member of the union, if any.
237
+
238
+ [*Note 5*: The copy/move of the active member is
239
+ trivial. *end note*]
240
+
241
+ During the evaluation of an expression E as a core constant expression,
242
+ all *id-expression*s and uses of `*this` that refer to an object or
243
+ reference whose lifetime did not begin with the evaluation of E are
244
+ treated as referring to a specific instance of that object or reference
245
+ whose lifetime and that of all subobjects (including all union members)
246
+ includes the entire constant evaluation. For such an object that is not
247
+ usable in constant expressions, the dynamic type of the object is
248
+ *constexpr-unknown*. For such a reference that is not usable in constant
249
+ expressions, the reference is treated as binding to an unspecified
250
+ object of the referenced type whose lifetime and that of all subobjects
251
+ includes the entire constant evaluation and whose dynamic type is
252
+ constexpr-unknown.
253
+
254
+ [*Example 5*:
255
+
256
+ ``` cpp
257
+ template <typename T, size_t N>
258
+ constexpr size_t array_size(T (&)[N]) {
259
+ return N;
260
+ }
261
+
262
+ void use_array(int const (&gold_medal_mel)[2]) {
263
+ constexpr auto gold = array_size(gold_medal_mel); // OK
264
+ }
265
+
266
+ constexpr auto olympic_mile() {
267
+ const int ledecky = 1500;
268
+ return []{ return ledecky; };
269
+ }
270
+ static_assert(olympic_mile()() == 1500); // OK
271
+
272
+ struct Swim {
273
+ constexpr int phelps() { return 28; }
274
+ virtual constexpr int lochte() { return 12; }
275
+ int coughlin = 12;
276
+ };
277
+
278
+ constexpr int how_many(Swim& swam) {
279
+ Swim* p = &swam;
280
+ return (p + 1 - 1)->phelps();
281
+ }
282
+
283
+ void splash(Swim& swam) {
284
+ static_assert(swam.phelps() == 28); // OK
285
+ static_assert((&swam)->phelps() == 28); // OK
286
+
287
+ Swim* pswam = &swam;
288
+ static_assert(pswam->phelps() == 28); // error: lvalue-to-rvalue conversion on a pointer
289
+ // not usable in constant expressions
290
+
291
+ static_assert(how_many(swam) == 28); // OK
292
+ static_assert(Swim().lochte() == 12); // OK
293
+
294
+ static_assert(swam.lochte() == 12); // error: invoking virtual function on reference
295
+ // with constexpr-unknown dynamic type
296
+
297
+ static_assert(swam.coughlin == 12); // error: lvalue-to-rvalue conversion on an object
298
+ // not usable in constant expressions
299
+ }
300
+
301
+ extern Swim dc;
302
+ extern Swim& trident;
303
+
304
+ constexpr auto& sandeno = typeid(dc); // OK, can only be typeid(Swim)
305
+ constexpr auto& gallagher = typeid(trident); // error: constexpr-unknown dynamic type
306
+ ```
307
+
308
+ — *end example*]
309
 
310
  An object `a` is said to have *constant destruction* if:
311
 
312
+ - it is not of class type nor (possibly multidimensional) array thereof,
313
+ or
314
+ - it is of class type or (possibly multidimensional) array thereof, that
315
+ class type has a constexpr destructor, and for a hypothetical
316
  expression E whose only effect is to destroy `a`, E would be a core
317
  constant expression if the lifetime of `a` and its non-mutable
318
  subobjects (but not its mutable subobjects) were considered to start
319
  within E.
320
 
321
  An *integral constant expression* is an expression of integral or
322
  unscoped enumeration type, implicitly converted to a prvalue, where the
323
  converted expression is a core constant expression.
324
 
325
+ [*Note 6*: Such expressions can be used as bit-field lengths
326
  [[class.bit]], as enumerator initializers if the underlying type is not
327
  fixed [[dcl.enum]], and as alignments [[dcl.align]]. — *end note*]
328
 
329
  If an expression of literal class type is used in a context where an
330
  integral constant expression is required, then that expression is
331
  contextually implicitly converted [[conv]] to an integral or unscoped
332
  enumeration type and the selected conversion function shall be
333
  `constexpr`.
334
 
335
+ [*Example 6*:
336
 
337
  ``` cpp
338
  struct A {
339
  constexpr A(int i) : val(i) { }
340
  constexpr operator int() const { return val; }
 
366
  and
367
  - function pointer conversions [[conv.fctptr]],
368
 
369
  and where the reference binding (if any) binds directly.
370
 
371
+ [*Note 7*: Such expressions can be used in `new` expressions
372
  [[expr.new]], as case expressions [[stmt.switch]], as enumerator
373
  initializers if the underlying type is fixed [[dcl.enum]], as array
374
  bounds [[dcl.array]], and as non-type template arguments
375
  [[temp.arg]]. — *end note*]
376
 
 
385
  whose value satisfies the following constraints:
386
 
387
  - if the value is an object of class type, each non-static data member
388
  of reference type refers to an entity that is a permitted result of a
389
  constant expression,
390
+ - if the value is an object of scalar type, it does not have an
391
+ indeterminate value [[basic.indet]],
392
  - if the value is of pointer type, it contains the address of an object
393
  with static storage duration, the address past the end of such an
394
  object [[expr.add]], the address of a non-immediate function, or a
395
  null pointer value,
396
  - if the value is of pointer-to-member-function type, it does not
 
401
  An entity is a *permitted result of a constant expression* if it is an
402
  object with static storage duration that either is not a temporary
403
  object or is a temporary object whose value satisfies the above
404
  constraints, or if it is a non-immediate function.
405
 
406
+ [*Note 8*: A glvalue core constant expression that either refers to or
407
+ points to an unspecified object is not a constant
408
+ expression. — *end note*]
409
+
410
+ [*Example 7*:
411
 
412
  ``` cpp
413
  consteval int f() { return 42; }
414
  consteval auto g() { return f; }
415
  consteval int h(int (*p)() = g()) { return p(); }
 
418
  // not a permitted result of a constant expression
419
  ```
420
 
421
  — *end example*]
422
 
423
+ *Recommended practice:* Implementations should provide consistent
424
+ results of floating-point evaluations, irrespective of whether the
425
+ evaluation is performed during translation or during program execution.
426
+
427
+ [*Note 9*:
428
 
429
  Since this document imposes no restrictions on the accuracy of
430
  floating-point operations, it is unspecified whether the evaluation of a
431
  floating-point expression during translation yields the same result as
432
  the evaluation of the same expression (or the same operations on the
433
+ same values) during program execution.
434
 
435
+ [*Example 8*:
436
 
437
  ``` cpp
438
  bool f() {
439
  char array[1 + int(1 + 0.2 - 0.1 - 0.1)]; // Must be evaluated during translation
440
  int size = 1 + int(1 + 0.2 - 0.1 - 0.1); // May be evaluated at runtime
 
447
  — *end example*]
448
 
449
  — *end note*]
450
 
451
  An expression or conversion is in an *immediate function context* if it
452
+ is potentially evaluated and either:
453
+
454
+ - its innermost enclosing non-block scope is a function parameter scope
455
+ of an immediate function,
456
+ - it is a subexpression of a manifestly constant-evaluated expression or
457
+ conversion, or
458
+ - its enclosing statement is enclosed [[stmt.pre]] by the
459
+ *compound-statement* of a consteval if statement [[stmt.if]].
460
+
461
+ An invocation is an *immediate invocation* if it is a
462
+ potentially-evaluated explicit or implicit invocation of an immediate
463
+ function and is not in an immediate function context. An aggregate
464
+ initialization is an immediate invocation if it evaluates a default
465
+ member initializer that has a subexpression that is an
466
+ immediate-escalating expression.
467
+
468
+ An expression or conversion is *immediate-escalating* if it is not
469
+ initially in an immediate function context and it is either
470
+
471
+ - a potentially-evaluated *id-expression* that denotes an immediate
472
+ function that is not a subexpression of an immediate invocation, or
473
+ - an immediate invocation that is not a constant expression and is not a
474
+ subexpression of an immediate invocation.
475
+
476
+ An *immediate-escalating* function is
477
+
478
+ - the call operator of a lambda that is not declared with the
479
+ `consteval` specifier,
480
+ - a defaulted special member function that is not declared with the
481
+ `consteval` specifier, or
482
+ - a function that results from the instantiation of a templated entity
483
+ defined with the `constexpr` specifier.
484
+
485
+ An immediate-escalating expression shall appear only in an
486
+ immediate-escalating function.
487
+
488
+ An *immediate function* is a function or constructor that is
489
+
490
+ - declared with the `consteval` specifier, or
491
+ - an immediate-escalating function `F` whose function body contains an
492
+ immediate-escalating expression `E` such that `E`’s innermost
493
+ enclosing non-block scope is `F`’s function parameter scope.
494
+
495
+ [*Example 9*:
496
+
497
+ ``` cpp
498
+ consteval int id(int i) { return i; }
499
+ constexpr char id(char c) { return c; }
500
+
501
+ template<class T>
502
+ constexpr int f(T t) {
503
+ return t + id(t);
504
+ }
505
+
506
+ auto a = &f<char>; // OK, f<char> is not an immediate function
507
+ auto b = &f<int>; // error: f<int> is an immediate function
508
+
509
+ static_assert(f(3) == 6); // OK
510
+
511
+ template<class T>
512
+ constexpr int g(T t) { // g<int> is not an immediate function
513
+ return t + id(42); // because id(42) is already a constant
514
+ }
515
+
516
+ template<class T, class F>
517
+ constexpr bool is_not(T t, F f) {
518
+ return not f(t);
519
+ }
520
+
521
+ consteval bool is_even(int i) { return i % 2 == 0; }
522
+
523
+ static_assert(is_not(5, is_even)); // OK
524
+
525
+ int x = 0;
526
+
527
+ template<class T>
528
+ constexpr T h(T t = id(x)) { // h<int> is not an immediate function
529
+ return t;
530
+ }
531
+
532
+ template<class T>
533
+ constexpr T hh() { // hh<int> is an immediate function
534
+ return h<T>();
535
+ }
536
+
537
+ int i = hh<int>(); // error: hh<int>() is an immediate-escalating expression
538
+ // outside of an immediate-escalating function
539
+
540
+ struct A {
541
+ int x;
542
+ int y = id(x);
543
+ };
544
+
545
+ template<class T>
546
+ constexpr int k(int) { // k<int> is not an immediate function because A(42) is a
547
+ return A(42).y; // constant expression and thus not immediate-escalating
548
+ }
549
+ ```
550
+
551
+ — *end example*]
552
 
553
  An expression or conversion is *manifestly constant-evaluated* if it is:
554
 
555
  - a *constant-expression*, or
556
  - the condition of a constexpr if statement [[stmt.if]], or
557
  - an immediate invocation, or
558
  - the result of substitution into an atomic constraint expression to
559
  determine whether it is satisfied [[temp.constr.atomic]], or
560
  - the initializer of a variable that is usable in constant expressions
561
+ or has constant initialization [[basic.start.static]].[^34]
562
+ \[*Example 10*:
563
  ``` cpp
564
  template<bool> struct X {};
565
  X<std::is_constant_evaluated()> x; // type X<true>
566
  int y;
567
  const int a = std::is_constant_evaluated() ? y : 1; // dynamic initialization to 1
 
570
  const int b = std::is_constant_evaluated() ? 2 : y; // static initialization to 2
571
  int c = y + (std::is_constant_evaluated() ? 2 : y); // dynamic initialization to y+y
572
 
573
  constexpr int f() {
574
  const int n = std::is_constant_evaluated() ? 13 : 17; // n is 13
575
+ int m = std::is_constant_evaluated() ? 13 : 17; // m can be 13 or 17 (see below)
576
  char arr[n] = {}; // char[13]
577
  return m + sizeof(arr);
578
  }
579
  int p = f(); // m is 13; initialized to 26
580
  int q = p + f(); // m is 17 for this call; initialized to 56
581
  ```
582
 
583
  — *end example*]
584
 
585
+ [*Note 10*: A manifestly constant-evaluated expression is evaluated
586
+ even in an unevaluated operand
587
+ [[term.unevaluated.operand]]. — *end note*]
588
 
589
  An expression or conversion is *potentially constant evaluated* if it
590
  is:
591
 
592
  - a manifestly constant-evaluated expression,
593
  - a potentially-evaluated expression [[basic.def.odr]],
594
+ - an immediate subexpression of a *braced-init-list*,[^35]
595
  - an expression of the form `&` *cast-expression* that occurs within a
596
+ templated entity,[^36] or
597
+ - a potentially-evaluated subexpression [[intro.execution]] of one of
598
+ the above.
599
 
600
  A function or variable is *needed for constant evaluation* if it is:
601
 
602
  - a constexpr function that is named by an expression [[basic.def.odr]]
603
  that is potentially constant evaluated, or
604
+ - a potentially-constant variable named by a potentially constant
605
+ evaluated expression.
 
606
 
607
  <!-- Link reference definitions -->
608
+ [allocator.members]: mem.md#allocator.members
609
  [bad.alloc]: support.md#bad.alloc
610
  [bad.cast]: support.md#bad.cast
611
  [bad.typeid]: support.md#bad.typeid
612
  [basic.align]: basic.md#basic.align
613
  [basic.compound]: basic.md#basic.compound
 
615
  [basic.fundamental]: basic.md#basic.fundamental
616
  [basic.indet]: basic.md#basic.indet
617
  [basic.life]: basic.md#basic.life
618
  [basic.lookup]: basic.md#basic.lookup
619
  [basic.lookup.argdep]: basic.md#basic.lookup.argdep
620
+ [basic.lookup.general]: basic.md#basic.lookup.general
621
+ [basic.lookup.qual]: basic.md#basic.lookup.qual
622
  [basic.lookup.unqual]: basic.md#basic.lookup.unqual
623
  [basic.lval]: #basic.lval
624
+ [basic.pre]: basic.md#basic.pre
625
  [basic.scope.block]: basic.md#basic.scope.block
626
  [basic.scope.class]: basic.md#basic.scope.class
627
+ [basic.scope.lambda]: basic.md#basic.scope.lambda
628
  [basic.start.main]: basic.md#basic.start.main
629
+ [basic.start.static]: basic.md#basic.start.static
630
  [basic.stc.dynamic]: basic.md#basic.stc.dynamic
631
  [basic.stc.dynamic.allocation]: basic.md#basic.stc.dynamic.allocation
632
  [basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
633
+ [basic.stc.static]: basic.md#basic.stc.static
634
+ [basic.stc.thread]: basic.md#basic.stc.thread
635
  [basic.type.qualifier]: basic.md#basic.type.qualifier
 
636
  [class]: class.md#class
637
  [class.abstract]: class.md#class.abstract
638
  [class.access]: class.md#class.access
639
  [class.access.base]: class.md#class.access.base
640
  [class.base.init]: class.md#class.base.init
 
649
  [class.derived]: class.md#class.derived
650
  [class.dtor]: class.md#class.dtor
651
  [class.free]: class.md#class.free
652
  [class.friend]: class.md#class.friend
653
  [class.mem]: class.md#class.mem
654
+ [class.member.lookup]: basic.md#class.member.lookup
655
  [class.mfct]: class.md#class.mfct
656
+ [class.mfct.non.static]: class.md#class.mfct.non.static
657
  [class.mi]: class.md#class.mi
658
  [class.prop]: class.md#class.prop
 
659
  [class.spaceship]: class.md#class.spaceship
660
+ [class.static.mfct]: class.md#class.static.mfct
661
  [class.temporary]: basic.md#class.temporary
 
662
  [class.union]: class.md#class.union
663
+ [class.union.anon]: class.md#class.union.anon
664
  [class.virtual]: class.md#class.virtual
665
  [cmp.categories]: support.md#cmp.categories
666
+ [compare.syn]: support.md#compare.syn
667
  [conv]: #conv
668
  [conv.array]: #conv.array
669
  [conv.bool]: #conv.bool
670
  [conv.double]: #conv.double
671
  [conv.fctptr]: #conv.fctptr
672
  [conv.fpint]: #conv.fpint
673
  [conv.fpprom]: #conv.fpprom
674
  [conv.func]: #conv.func
675
+ [conv.general]: #conv.general
676
  [conv.integral]: #conv.integral
677
  [conv.lval]: #conv.lval
678
  [conv.mem]: #conv.mem
679
  [conv.prom]: #conv.prom
680
  [conv.ptr]: #conv.ptr
 
685
  [cstdarg.syn]: support.md#cstdarg.syn
686
  [cstddef.syn]: support.md#cstddef.syn
687
  [dcl.align]: dcl.md#dcl.align
688
  [dcl.array]: dcl.md#dcl.array
689
  [dcl.asm]: dcl.md#dcl.asm
690
+ [dcl.attr.assume]: dcl.md#dcl.attr.assume
691
  [dcl.constexpr]: dcl.md#dcl.constexpr
692
  [dcl.dcl]: dcl.md#dcl.dcl
693
  [dcl.decl]: dcl.md#dcl.decl
694
  [dcl.enum]: dcl.md#dcl.enum
695
  [dcl.fct]: dcl.md#dcl.fct
 
709
  [dcl.ref]: dcl.md#dcl.ref
710
  [dcl.spec.auto]: dcl.md#dcl.spec.auto
711
  [dcl.stc]: dcl.md#dcl.stc
712
  [dcl.struct.bind]: dcl.md#dcl.struct.bind
713
  [dcl.type]: dcl.md#dcl.type
714
+ [dcl.type.auto.deduct]: dcl.md#dcl.type.auto.deduct
715
  [dcl.type.cv]: dcl.md#dcl.type.cv
716
+ [dcl.type.decltype]: dcl.md#dcl.type.decltype
717
+ [dcl.type.elab]: dcl.md#dcl.type.elab
718
  [dcl.type.simple]: dcl.md#dcl.type.simple
719
  [defns.access]: intro.md#defns.access
720
+ [defns.nonconst.libcall]: intro.md#defns.nonconst.libcall
721
  [depr.arith.conv.enum]: future.md#depr.arith.conv.enum
722
  [depr.array.comp]: future.md#depr.array.comp
723
  [depr.capture.this]: future.md#depr.capture.this
 
724
  [depr.volatile.type]: future.md#depr.volatile.type
725
  [except]: except.md#except
726
  [except.handle]: except.md#except.handle
727
  [except.pre]: except.md#except.pre
728
  [except.spec]: except.md#except.spec
 
751
  [expr.mptr.oper]: #expr.mptr.oper
752
  [expr.mul]: #expr.mul
753
  [expr.new]: #expr.new
754
  [expr.or]: #expr.or
755
  [expr.post]: #expr.post
756
+ [expr.post.general]: #expr.post.general
757
  [expr.post.incr]: #expr.post.incr
758
  [expr.pre]: #expr.pre
759
  [expr.pre.incr]: #expr.pre.incr
760
  [expr.prim]: #expr.prim
761
  [expr.prim.fold]: #expr.prim.fold
762
  [expr.prim.id]: #expr.prim.id
763
  [expr.prim.id.dtor]: #expr.prim.id.dtor
764
+ [expr.prim.id.general]: #expr.prim.id.general
765
  [expr.prim.id.qual]: #expr.prim.id.qual
766
  [expr.prim.id.unqual]: #expr.prim.id.unqual
767
  [expr.prim.lambda]: #expr.prim.lambda
768
  [expr.prim.lambda.capture]: #expr.prim.lambda.capture
769
  [expr.prim.lambda.closure]: #expr.prim.lambda.closure
770
+ [expr.prim.lambda.general]: #expr.prim.lambda.general
771
  [expr.prim.literal]: #expr.prim.literal
772
  [expr.prim.paren]: #expr.prim.paren
773
  [expr.prim.req]: #expr.prim.req
774
  [expr.prim.req.compound]: #expr.prim.req.compound
775
+ [expr.prim.req.general]: #expr.prim.req.general
776
  [expr.prim.req.nested]: #expr.prim.req.nested
777
  [expr.prim.req.simple]: #expr.prim.req.simple
778
  [expr.prim.req.type]: #expr.prim.req.type
779
  [expr.prim.this]: #expr.prim.this
780
  [expr.prop]: #expr.prop
 
789
  [expr.throw]: #expr.throw
790
  [expr.type]: #expr.type
791
  [expr.type.conv]: #expr.type.conv
792
  [expr.typeid]: #expr.typeid
793
  [expr.unary]: #expr.unary
794
+ [expr.unary.general]: #expr.unary.general
795
  [expr.unary.noexcept]: #expr.unary.noexcept
796
  [expr.unary.op]: #expr.unary.op
797
  [expr.xor]: #expr.xor
798
  [expr.yield]: #expr.yield
799
  [function.objects]: utilities.md#function.objects
 
805
  [lex.ext]: lex.md#lex.ext
806
  [lex.icon]: lex.md#lex.icon
807
  [lex.literal]: lex.md#lex.literal
808
  [lex.string]: lex.md#lex.string
809
  [library]: library.md#library
810
+ [meta.const.eval]: meta.md#meta.const.eval
811
+ [namespace.udecl]: dcl.md#namespace.udecl
812
  [new.badlength]: support.md#new.badlength
813
  [new.delete.array]: support.md#new.delete.array
814
  [new.delete.placement]: support.md#new.delete.placement
815
  [new.delete.single]: support.md#new.delete.single
816
  [over]: over.md#over
817
  [over.ass]: over.md#over.ass
818
  [over.best.ics]: over.md#over.best.ics
819
  [over.built]: over.md#over.built
820
  [over.call]: over.md#over.call
821
+ [over.call.func]: over.md#over.call.func
822
  [over.ics.user]: over.md#over.ics.user
823
  [over.literal]: over.md#over.literal
824
  [over.match]: over.md#over.match
825
  [over.match.class.deduct]: over.md#over.match.class.deduct
826
  [over.match.oper]: over.md#over.match.oper
827
  [over.match.viable]: over.md#over.match.viable
828
  [over.oper]: over.md#over.oper
829
  [over.over]: over.md#over.over
830
+ [over.sub]: over.md#over.sub
831
  [replacement.functions]: library.md#replacement.functions
832
  [special]: class.md#special
833
+ [std.modules]: library.md#std.modules
834
+ [stmt.goto]: stmt.md#stmt.goto
835
  [stmt.if]: stmt.md#stmt.if
836
  [stmt.iter]: stmt.md#stmt.iter
837
  [stmt.jump]: stmt.md#stmt.jump
838
+ [stmt.pre]: stmt.md#stmt.pre
839
  [stmt.return]: stmt.md#stmt.return
840
+ [stmt.return.coroutine]: stmt.md#stmt.return.coroutine
841
  [stmt.switch]: stmt.md#stmt.switch
842
  [support.runtime]: support.md#support.runtime
843
  [support.types.layout]: support.md#support.types.layout
844
  [temp.arg]: temp.md#temp.arg
845
  [temp.concept]: temp.md#temp.concept
 
847
  [temp.constr.constr]: temp.md#temp.constr.constr
848
  [temp.constr.decl]: temp.md#temp.constr.decl
849
  [temp.dep.constexpr]: temp.md#temp.dep.constexpr
850
  [temp.expl.spec]: temp.md#temp.expl.spec
851
  [temp.explicit]: temp.md#temp.explicit
852
+ [temp.mem]: temp.md#temp.mem
853
  [temp.names]: temp.md#temp.names
854
+ [temp.over.link]: temp.md#temp.over.link
855
  [temp.param]: temp.md#temp.param
856
  [temp.pre]: temp.md#temp.pre
857
  [temp.res]: temp.md#temp.res
858
+ [temp.spec.partial]: temp.md#temp.spec.partial
859
  [temp.variadic]: temp.md#temp.variadic
860
+ [term.incomplete.type]: basic.md#term.incomplete.type
861
+ [term.object.representation]: basic.md#term.object.representation
862
+ [term.odr.use]: basic.md#term.odr.use
863
+ [term.unevaluated.operand]: #term.unevaluated.operand
864
  [thread]: thread.md#thread
865
  [type.info]: support.md#type.info
866
+ [typeinfo.syn]: support.md#typeinfo.syn
867
 
868
  [^1]: The precedence of operators is not directly specified, but it can
869
  be derived from the syntax.
870
 
871
  [^2]: Overloaded operators are never assumed to be associative or
 
874
  [^3]: The cast and assignment operators must still perform their
875
  specific conversions as described in  [[expr.type.conv]],
876
  [[expr.cast]], [[expr.static.cast]] and  [[expr.ass]].
877
 
878
  [^4]: The intent of this list is to specify those circumstances in which
879
+ an object can or cannot be aliased.
880
 
881
  [^5]: For historical reasons, this conversion is called the
882
  “lvalue-to-rvalue” conversion, even though that name does not
883
  accurately reflect the taxonomy of expressions described in 
884
  [[basic.lval]].
 
892
  be obtained.
893
 
894
  [^8]: The rule for conversion of pointers to members (from pointer to
895
  member of base to pointer to member of derived) appears inverted
896
  compared to the rule for pointers to objects (from pointer to
897
+ derived to pointer to base) [[conv.ptr]], [[class.derived]]. This
898
  inversion is necessary to ensure type safety. Note that a pointer to
899
  member is not an object pointer or a function pointer and the rules
900
  for conversions of such pointers do not apply to pointers to
901
  members. In particular, a pointer to member cannot be converted to a
902
  `void*`.
903
 
904
  [^9]: As a consequence, operands of type `bool`, `char8_t`, `char16_t`,
905
+ `char32_t`, `wchar_t`, or of enumeration type are converted to some
906
  integral type.
907
 
908
  [^10]: This also applies when the object expression is an implicit
909
+ `(*this)` [[class.mfct.non.static]].
910
 
911
  [^11]: This is true even if the subscript operator is used in the
912
  following common idiom: `&x[0]`.
913
 
914
  [^12]: If the class member access expression is evaluated, the
 
925
  [^15]: The recommended name for such a class is `extended_type_info`.
926
 
927
  [^16]: If `p` is an expression of pointer type, then `*p`, `(*p)`,
928
  `*(p)`, `((*p))`, `*((p))`, and so on all meet this requirement.
929
 
930
+ [^17]: The types can have different cv-qualifiers, subject to the
931
  overall restriction that a `reinterpret_cast` cannot cast away
932
  constness.
933
 
934
+ [^18]: `T1` and `T2` can have different cv-qualifiers, subject to the
935
  overall restriction that a `reinterpret_cast` cannot cast away
936
  constness.
937
 
938
+ [^19]: This is sometimes referred to as a type pun when the result
939
  refers to the same object as the source glvalue.
940
 
941
+ [^20]: `const_cast` is not limited to conversions that cast away a
942
+ const-qualifier.
943
 
944
+ [^21]: `sizeof``(``bool``)` is not required to be `1`.
945
 
946
+ [^22]: The actual size of a potentially-overlapping subobject can be
 
 
947
  less than the result of applying `sizeof` to the subobject, due to
948
  virtual base classes and less strict padding requirements on
949
  potentially-overlapping subobjects.
950
 
951
  [^23]: If the conversion function returns a signed integer type, the
952
  second standard conversion converts to the unsigned type
953
  `std::size_t` and thus thwarts any attempt to detect a negative
954
  value afterwards.
955
 
956
+ [^24]: This can include evaluating a *new-initializer* and/or calling a
957
  constructor.
958
 
959
  [^25]: A *lambda-expression* with a *lambda-introducer* that consists of
960
  empty square brackets can follow the `delete` keyword if the
961
  *lambda-expression* is enclosed in parentheses.
 
983
 
984
  [^31]: As specified in [[basic.compound]], an object that is not an
985
  array element is considered to belong to a single-element array for
986
  this purpose.
987
 
988
+ [^32]: Overload resolution [[over.match]] is applied as usual.
 
 
989
 
990
+ [^33]: This includes, for example, signed integer overflow [[expr.pre]],
991
+ certain pointer arithmetic [[expr.add]], division by zero
992
+ [[expr.mul]], or certain shift operations [[expr.shift]].
993
+
994
+ [^34]: Testing this condition can involve a trial evaluation of its
995
  initializer as described above.
996
 
997
+ [^35]: In some cases, constant evaluation is needed to determine whether
998
+ a narrowing conversion is performed [[dcl.init.list]].
999
 
1000
+ [^36]: In some cases, constant evaluation is needed to determine whether
1001
+ such an expression is value-dependent [[temp.dep.constexpr]].