- tmp/tmp78ub49ga/{from.md → to.md} +530 -166
tmp/tmp78ub49ga/{from.md → to.md}
RENAMED
|
@@ -3,31 +3,130 @@
|
|
| 3 |
Certain contexts require expressions that satisfy additional
|
| 4 |
requirements as detailed in this subclause; other contexts have
|
| 5 |
different semantics depending on whether or not an expression satisfies
|
| 6 |
these requirements. Expressions that satisfy these requirements,
|
| 7 |
assuming that copy elision [[class.copy.elision]] is not performed, are
|
| 8 |
-
called
|
| 9 |
|
| 10 |
[*Note 1*: Constant expressions can be evaluated during
|
| 11 |
translation. — *end note*]
|
| 12 |
|
| 13 |
``` bnf
|
| 14 |
constant-expression:
|
| 15 |
conditional-expression
|
| 16 |
```
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
- either it has an initializer or its default-initialization results in
|
| 21 |
-
some initialization being performed, and
|
| 22 |
- the full-expression of its initialization is a constant expression
|
| 23 |
-
when interpreted as a *constant-expression*
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
@@ -36,54 +135,80 @@ 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*
|
|
|
|
| 42 |
|
| 43 |
-
- a variable that is usable in
|
| 44 |
-
|
| 45 |
-
- a string literal object [[lex.string]], or
|
| 46 |
- a temporary object of non-volatile const-qualified literal type whose
|
| 47 |
lifetime is extended [[class.temporary]] to that of a variable that is
|
| 48 |
-
usable in constant expressions
|
| 49 |
-
- a
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 61 |
-
|
| 62 |
-
duration, unless that variable is usable
|
| 63 |
-
|
|
|
|
| 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;[^
|
| 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
|
| 83 |
-
[[intro]] through [[
|
| 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
|
|
@@ -96,11 +221,11 @@ would evaluate one of the following:
|
|
| 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
|
| 102 |
``` cpp
|
| 103 |
void g() {
|
| 104 |
const int n = 0;
|
| 105 |
[=] {
|
| 106 |
constexpr int i = n; // OK, n is not odr-used here
|
|
@@ -108,17 +233,17 @@ would evaluate one of the following:
|
|
| 108 |
};
|
| 109 |
}
|
| 110 |
```
|
| 111 |
|
| 112 |
— *end example*]
|
| 113 |
-
\[*Note
|
| 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
|
| 117 |
[[expr.prim.lambda.capture]] of the *id-expression* into an access of
|
| 118 |
the corresponding data member.
|
| 119 |
-
\[*Example
|
| 120 |
``` cpp
|
| 121 |
auto monad = [](auto v) { return [=] { return v; }; };
|
| 122 |
auto bind = [](auto m) {
|
| 123 |
return [=](auto fvm) { return fvm(m()); };
|
| 124 |
};
|
|
@@ -127,61 +252,82 @@ would evaluate one of the following:
|
|
| 127 |
static_assert(bind(monad(2))(monad)() == monad(2)());
|
| 128 |
```
|
| 129 |
|
| 130 |
— *end example*]
|
| 131 |
— *end note*]
|
| 132 |
-
- a conversion from type cv `void
|
|
|
|
|
|
|
|
|
|
| 133 |
- a `reinterpret_cast` [[expr.reinterpret.cast]];
|
| 134 |
- a modification of an object
|
| 135 |
-
[[expr.
|
| 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
|
| 143 |
-
function is a replaceable global allocation
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 150 |
within the evaluation of E;
|
| 151 |
- a call to an instance of `std::allocator<T>::deallocate`
|
| 152 |
[[allocator.members]], unless it deallocates a region of storage
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 [[
|
| 173 |
-
- an invocation of the `va_start` macro [[cstdarg.syn]]
|
| 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
|
| 183 |
|
| 184 |
``` cpp
|
| 185 |
int x; // not constant
|
| 186 |
struct A {
|
| 187 |
constexpr A(bool b) : m(b?42:x) { }
|
|
@@ -222,38 +368,70 @@ constexpr int y = h(1); // OK, initializes y with the value 2
|
|
| 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.
|
| 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
|
| 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
|
| 243 |
-
reference whose lifetime did not begin with the
|
| 244 |
-
treated as referring to a specific instance of that
|
| 245 |
-
whose lifetime and that of all subobjects (including
|
| 246 |
-
includes the entire constant evaluation. For such an
|
| 247 |
-
usable in constant expressions, the dynamic type of
|
| 248 |
-
*constexpr-unknown*. For such a reference that is not
|
| 249 |
-
expressions, the reference is treated as binding to
|
| 250 |
-
object of the referenced type whose lifetime and that of
|
| 251 |
-
includes the entire constant evaluation and whose dynamic
|
| 252 |
-
constexpr-unknown.
|
| 253 |
|
| 254 |
-
[*Example
|
| 255 |
|
| 256 |
``` cpp
|
| 257 |
template <typename T, size_t N>
|
| 258 |
constexpr size_t array_size(T (&)[N]) {
|
| 259 |
return N;
|
|
@@ -305,36 +483,36 @@ 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
|
| 316 |
-
expression E whose only effect is to destroy `a`, E would
|
| 317 |
-
constant expression if the lifetime of `a` and its
|
| 318 |
-
subobjects (but not its mutable subobjects) were
|
| 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
|
| 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
|
| 336 |
|
| 337 |
``` cpp
|
| 338 |
struct A {
|
| 339 |
constexpr A(int i) : val(i) { }
|
| 340 |
constexpr operator int() const { return val; }
|
|
@@ -359,82 +537,110 @@ constant expression and the implicit conversion sequence contains only
|
|
| 359 |
- function-to-pointer conversions [[conv.func]],
|
| 360 |
- qualification conversions [[conv.qual]],
|
| 361 |
- integral promotions [[conv.prom]],
|
| 362 |
- integral conversions [[conv.integral]] other than narrowing
|
| 363 |
conversions [[dcl.init.list]],
|
|
|
|
|
|
|
|
|
|
| 364 |
- null pointer conversions [[conv.ptr]] from `std::nullptr_t`,
|
| 365 |
- null member pointer conversions [[conv.mem]] from `std::nullptr_t`,
|
| 366 |
and
|
| 367 |
- function pointer conversions [[conv.fctptr]],
|
| 368 |
|
| 369 |
and where the reference binding (if any) binds directly.
|
| 370 |
|
| 371 |
-
[*Note
|
| 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]],
|
| 375 |
-
|
|
|
|
| 376 |
|
| 377 |
A *contextually converted constant expression of type `bool`* is an
|
| 378 |
expression, contextually converted to `bool` [[conv]], where the
|
| 379 |
converted expression is a constant expression and the conversion
|
| 380 |
sequence contains only the conversions above.
|
| 381 |
|
| 382 |
-
A *constant expression* is either
|
| 383 |
-
that refers to an entity that is a permitted result of a constant
|
| 384 |
-
expression (as defined below), or a prvalue core constant expression
|
| 385 |
-
whose value satisfies the following constraints:
|
| 386 |
|
| 387 |
-
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
null pointer value,
|
| 396 |
-
- if the value is of pointer-to-member-function type, it does not
|
| 397 |
-
designate an immediate function, and
|
| 398 |
-
- if the value is an object of class or array type, each subobject
|
| 399 |
-
satisfies these constraints for the value.
|
| 400 |
|
| 401 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
points to an unspecified object is not a constant
|
| 408 |
expression. — *end note*]
|
| 409 |
|
| 410 |
-
[*Example
|
| 411 |
|
| 412 |
``` cpp
|
| 413 |
consteval int f() { return 42; }
|
| 414 |
consteval auto g() { return f; }
|
| 415 |
consteval int h(int (*p)() = g()) { return p(); }
|
| 416 |
constexpr int r = h(); // OK
|
| 417 |
constexpr auto e = g(); // error: a pointer to an immediate function is
|
| 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
|
| 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
|
| 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
|
|
@@ -463,17 +669,18 @@ 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 |
-
|
| 469 |
-
|
|
|
|
| 470 |
|
| 471 |
-
-
|
| 472 |
-
|
| 473 |
-
- an immediate invocation that is not a constant expression
|
| 474 |
-
|
| 475 |
|
| 476 |
An *immediate-escalating* function is
|
| 477 |
|
| 478 |
- the call operator of a lambda that is not declared with the
|
| 479 |
`consteval` specifier,
|
|
@@ -483,18 +690,22 @@ An *immediate-escalating* function is
|
|
| 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
|
| 489 |
|
| 490 |
- declared with the `consteval` specifier, or
|
| 491 |
-
- an immediate-escalating function `F` whose function body contains
|
| 492 |
-
|
| 493 |
-
|
|
|
|
| 494 |
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
| 496 |
|
| 497 |
``` cpp
|
| 498 |
consteval int id(int i) { return i; }
|
| 499 |
constexpr char id(char c) { return c; }
|
| 500 |
|
|
@@ -524,16 +735,17 @@ 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 |
|
|
@@ -544,10 +756,19 @@ struct A {
|
|
| 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:
|
|
@@ -556,12 +777,12 @@ An expression or conversion is *manifestly constant-evaluated* if it is:
|
|
| 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]].[^
|
| 562 |
-
\[*Example
|
| 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
|
|
@@ -580,22 +801,140 @@ An expression or conversion is *manifestly constant-evaluated* if it is:
|
|
| 580 |
int q = p + f(); // m is 17 for this call; initialized to 56
|
| 581 |
```
|
| 582 |
|
| 583 |
— *end example*]
|
| 584 |
|
| 585 |
-
[*Note
|
| 586 |
-
even in an unevaluated
|
| 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*,[^
|
| 595 |
- an expression of the form `&` *cast-expression* that occurs within a
|
| 596 |
-
templated entity,[^
|
| 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 |
|
|
@@ -603,42 +942,51 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 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
|
|
|
|
|
|
|
|
|
|
| 614 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 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
|
| 641 |
[class.bit]: class.md#class.bit
|
| 642 |
[class.cdtor]: class.md#class.cdtor
|
| 643 |
[class.conv]: class.md#class.conv
|
| 644 |
[class.conv.fct]: class.md#class.conv.fct
|
|
@@ -649,14 +997,15 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 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
|
|
@@ -679,62 +1028,67 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 679 |
[conv.prom]: #conv.prom
|
| 680 |
[conv.ptr]: #conv.ptr
|
| 681 |
[conv.qual]: #conv.qual
|
| 682 |
[conv.rank]: basic.md#conv.rank
|
| 683 |
[conv.rval]: #conv.rval
|
| 684 |
-
[cpp]: cpp.md#cpp
|
| 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.
|
| 691 |
[dcl.constexpr]: dcl.md#dcl.constexpr
|
| 692 |
-
[dcl.
|
|
|
|
| 693 |
[dcl.decl]: dcl.md#dcl.decl
|
| 694 |
[dcl.enum]: dcl.md#dcl.enum
|
| 695 |
[dcl.fct]: dcl.md#dcl.fct
|
| 696 |
[dcl.fct.def]: dcl.md#dcl.fct.def
|
| 697 |
[dcl.fct.def.coroutine]: dcl.md#dcl.fct.def.coroutine
|
| 698 |
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 699 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 700 |
[dcl.init]: dcl.md#dcl.init
|
| 701 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
|
|
|
| 702 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 703 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
| 704 |
[dcl.init.string]: dcl.md#dcl.init.string
|
| 705 |
[dcl.link]: dcl.md#dcl.link
|
| 706 |
[dcl.mptr]: dcl.md#dcl.mptr
|
| 707 |
[dcl.name]: dcl.md#dcl.name
|
|
|
|
| 708 |
[dcl.ptr]: dcl.md#dcl.ptr
|
| 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
|
| 729 |
[except.terminate]: except.md#except.terminate
|
| 730 |
[except.throw]: except.md#except.throw
|
|
|
|
| 731 |
[expr]: #expr
|
| 732 |
[expr.add]: #expr.add
|
| 733 |
[expr.alignof]: #expr.alignof
|
| 734 |
[expr.arith.conv]: #expr.arith.conv
|
| 735 |
-
[expr.
|
| 736 |
[expr.await]: #expr.await
|
| 737 |
[expr.bit.and]: #expr.bit.and
|
| 738 |
[expr.call]: #expr.call
|
| 739 |
[expr.cast]: #expr.cast
|
| 740 |
[expr.comma]: #expr.comma
|
|
@@ -757,30 +1111,34 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 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
|
| 781 |
[expr.ref]: #expr.ref
|
|
|
|
| 782 |
[expr.reinterpret.cast]: #expr.reinterpret.cast
|
| 783 |
[expr.rel]: #expr.rel
|
| 784 |
[expr.shift]: #expr.shift
|
| 785 |
[expr.sizeof]: #expr.sizeof
|
| 786 |
[expr.spaceship]: #expr.spaceship
|
|
@@ -803,67 +1161,81 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 803 |
[intro.memory]: basic.md#intro.memory
|
| 804 |
[intro.object]: basic.md#intro.object
|
| 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.
|
| 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
|
| 846 |
[temp.constr.atomic]: temp.md#temp.constr.atomic
|
| 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.
|
|
@@ -871,23 +1243,22 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 871 |
[^2]: Overloaded operators are never assumed to be associative or
|
| 872 |
commutative.
|
| 873 |
|
| 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.
|
| 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]].
|
| 885 |
|
| 886 |
[^6]: In C++ class and array prvalues can have cv-qualified types. This
|
| 887 |
-
differs from
|
| 888 |
-
types.
|
| 889 |
|
| 890 |
[^7]: This conversion never applies to non-static member functions
|
| 891 |
because an lvalue that refers to a non-static member function cannot
|
| 892 |
be obtained.
|
| 893 |
|
|
@@ -903,99 +1274,92 @@ A function or variable is *needed for constant evaluation* if it is:
|
|
| 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
|
| 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
|
| 915 |
subexpression evaluation happens even if the result is unnecessary
|
| 916 |
to determine the value of the entire postfix expression, for example
|
| 917 |
if the *id-expression* denotes a static member.
|
| 918 |
|
| 919 |
-
[^13]:
|
| 920 |
-
|
| 921 |
-
[^14]: The most derived object [[intro.object]] pointed or referred to
|
| 922 |
by `v` can contain other `B` objects as base classes, but these are
|
| 923 |
ignored.
|
| 924 |
|
| 925 |
-
[^
|
| 926 |
|
| 927 |
-
[^
|
| 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 |
-
[^
|
| 935 |
overall restriction that a `reinterpret_cast` cannot cast away
|
| 936 |
constness.
|
| 937 |
|
| 938 |
-
[^
|
| 939 |
refers to the same object as the source glvalue.
|
| 940 |
|
| 941 |
-
[^
|
| 942 |
const-qualifier.
|
| 943 |
|
| 944 |
-
[^
|
| 945 |
|
| 946 |
-
[^
|
| 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 |
-
[^
|
| 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 |
-
[^
|
| 957 |
constructor.
|
| 958 |
|
| 959 |
-
[^
|
| 960 |
empty square brackets can follow the `delete` keyword if the
|
| 961 |
*lambda-expression* is enclosed in parentheses.
|
| 962 |
|
| 963 |
-
[^
|
| 964 |
-
type `void*` because `void` is not an object type.
|
| 965 |
-
|
| 966 |
-
[^27]: For nonzero-length arrays, this is the same as a pointer to the
|
| 967 |
first element of the array created by that *new-expression*.
|
| 968 |
Zero-length arrays do not have a first element.
|
| 969 |
|
| 970 |
-
[^
|
| 971 |
|
| 972 |
-
[^
|
| 973 |
array element is considered to belong to a single-element array for
|
| 974 |
this purpose and a pointer past the last element of an array of n
|
| 975 |
elements is considered to be equivalent to a pointer to a
|
| 976 |
hypothetical array element n for this purpose.
|
| 977 |
|
| 978 |
-
[^
|
| 979 |
array element is considered to belong to a single-element array for
|
| 980 |
this purpose and a pointer past the last element of an array of n
|
| 981 |
elements is considered to be equivalent to a pointer to a
|
| 982 |
hypothetical array element n for this purpose.
|
| 983 |
|
| 984 |
-
[^
|
| 985 |
array element is considered to belong to a single-element array for
|
| 986 |
this purpose.
|
| 987 |
|
| 988 |
-
[^
|
| 989 |
|
| 990 |
-
[^
|
| 991 |
certain pointer arithmetic [[expr.add]], division by zero
|
| 992 |
[[expr.mul]], or certain shift operations [[expr.shift]].
|
| 993 |
|
| 994 |
-
[^
|
| 995 |
-
initializer
|
|
|
|
|
|
|
| 996 |
|
| 997 |
-
[^
|
| 998 |
a narrowing conversion is performed [[dcl.init.list]].
|
| 999 |
|
| 1000 |
-
[^
|
| 1001 |
such an expression is value-dependent [[temp.dep.constexpr]].
|
|
|
|
| 3 |
Certain contexts require expressions that satisfy additional
|
| 4 |
requirements as detailed in this subclause; other contexts have
|
| 5 |
different semantics depending on whether or not an expression satisfies
|
| 6 |
these requirements. Expressions that satisfy these requirements,
|
| 7 |
assuming that copy elision [[class.copy.elision]] is not performed, are
|
| 8 |
+
called constant expressions.
|
| 9 |
|
| 10 |
[*Note 1*: Constant expressions can be evaluated during
|
| 11 |
translation. — *end note*]
|
| 12 |
|
| 13 |
``` bnf
|
| 14 |
constant-expression:
|
| 15 |
conditional-expression
|
| 16 |
```
|
| 17 |
|
| 18 |
+
The *constituent values* of an object o are
|
| 19 |
+
|
| 20 |
+
- if o has scalar type, the value of o;
|
| 21 |
+
- otherwise, the constituent values of any direct subobjects of o other
|
| 22 |
+
than inactive union members.
|
| 23 |
+
|
| 24 |
+
The *constituent references* of an object o are
|
| 25 |
+
|
| 26 |
+
- any direct members of o that have reference type, and
|
| 27 |
+
- the constituent references of any direct subobjects of o other than
|
| 28 |
+
inactive union members.
|
| 29 |
+
|
| 30 |
+
The constituent values and constituent references of a variable `x` are
|
| 31 |
+
defined as follows:
|
| 32 |
+
|
| 33 |
+
- If `x` declares an object, the constituent values and references of
|
| 34 |
+
that object are constituent values and references of `x`.
|
| 35 |
+
- If `x` declares a reference, that reference is a constituent reference
|
| 36 |
+
of `x`.
|
| 37 |
+
|
| 38 |
+
For any constituent reference `r` of a variable `x`, if `r` is bound to
|
| 39 |
+
a temporary object or subobject thereof whose lifetime is extended to
|
| 40 |
+
that of `r`, the constituent values and references of that temporary
|
| 41 |
+
object are also constituent values and references of `x`, recursively.
|
| 42 |
+
|
| 43 |
+
An object o is *constexpr-referenceable* from a point P if
|
| 44 |
+
|
| 45 |
+
- o has static storage duration, or
|
| 46 |
+
- o has automatic storage duration, and, letting `v` denote
|
| 47 |
+
- the variable corresponding to o’s complete object or
|
| 48 |
+
- the variable to whose lifetime that of o is extended,
|
| 49 |
+
|
| 50 |
+
the smallest scope enclosing `v` and the smallest scope enclosing P
|
| 51 |
+
that are neither
|
| 52 |
+
- block scopes nor
|
| 53 |
+
- function parameter scopes associated with a
|
| 54 |
+
*requirement-parameter-list*
|
| 55 |
+
|
| 56 |
+
are the same function parameter scope.
|
| 57 |
+
|
| 58 |
+
[*Example 1*:
|
| 59 |
+
|
| 60 |
+
``` cpp
|
| 61 |
+
struct A {
|
| 62 |
+
int m;
|
| 63 |
+
const int& r;
|
| 64 |
+
};
|
| 65 |
+
void f() {
|
| 66 |
+
static int sx;
|
| 67 |
+
thread_local int tx; // tx is never constexpr-referenceable
|
| 68 |
+
int ax;
|
| 69 |
+
A aa = {1, 2};
|
| 70 |
+
static A sa = {3, 4};
|
| 71 |
+
// The objects sx, ax, and aa.m, sa.m, and the temporaries to which aa.r and sa.r are bound, are constexpr-referenceable.
|
| 72 |
+
auto lambda = [] {
|
| 73 |
+
int ay;
|
| 74 |
+
// The objects sx, sa.m, and ay (but not ax or aa), and the
|
| 75 |
+
// temporary to which sa.r is bound, are constexpr-referenceable.
|
| 76 |
+
};
|
| 77 |
+
}
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
— *end example*]
|
| 81 |
+
|
| 82 |
+
An object or reference `x` is *constexpr-representable* at a point P if,
|
| 83 |
+
for each constituent value of `x` that points to or past an object o,
|
| 84 |
+
and for each constituent reference of `x` that refers to an object o, o
|
| 85 |
+
is constexpr-referenceable from P.
|
| 86 |
+
|
| 87 |
+
A variable `v` is *constant-initializable* if
|
| 88 |
|
|
|
|
|
|
|
| 89 |
- the full-expression of its initialization is a constant expression
|
| 90 |
+
when interpreted as a *constant-expression* with all contract
|
| 91 |
+
assertions using the ignore evaluation semantic
|
| 92 |
+
[[basic.contract.eval]], \[*Note 2*: Within this evaluation,
|
| 93 |
+
`std::is_constant_evaluated()` [[meta.const.eval]] returns
|
| 94 |
+
`true`. — *end note*] \[*Note 3*: The initialization, when evaluated,
|
| 95 |
+
can still evaluate contract assertions with other evaluation
|
| 96 |
+
semantics, resulting in a diagnostic or ill-formed program if a
|
| 97 |
+
contract violation occurs. — *end note*]
|
| 98 |
+
- immediately after the initializing declaration of `v`, the object or
|
| 99 |
+
reference `x` declared by `v` is constexpr-representable, and
|
| 100 |
+
- if `x` has static or thread storage duration, `x` is
|
| 101 |
+
constexpr-representable at the nearest point whose immediate scope is
|
| 102 |
+
a namespace scope that follows the initializing declaration of `v`.
|
| 103 |
+
|
| 104 |
+
A constant-initializable variable is *constant-initialized* if either it
|
| 105 |
+
has an initializer or its type is const-default-constructible
|
| 106 |
+
[[dcl.init.general]].
|
| 107 |
+
|
| 108 |
+
[*Example 2*:
|
| 109 |
+
|
| 110 |
+
``` cpp
|
| 111 |
+
void f() {
|
| 112 |
+
int ax = 0; // ax is constant-initialized
|
| 113 |
+
thread_local int tx = 0; // tx is constant-initialized
|
| 114 |
+
static int sx; // sx is not constant-initialized
|
| 115 |
+
static int& rss = sx; // rss is constant-initialized
|
| 116 |
+
static int& rst = tx; // rst is not constant-initialized
|
| 117 |
+
static int& rsa = ax; // rsa is not constant-initialized
|
| 118 |
+
thread_local int& rts = sx; // rts is constant-initialized
|
| 119 |
+
thread_local int& rtt = tx; // rtt is not constant-initialized
|
| 120 |
+
thread_local int& rta = ax; // rta is not constant-initialized
|
| 121 |
+
int& ras = sx; // ras is constant-initialized
|
| 122 |
+
int& rat = tx; // rat is not constant-initialized
|
| 123 |
+
int& raa = ax; // raa is constant-initialized
|
| 124 |
+
}
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
— *end example*]
|
| 128 |
|
| 129 |
A variable is *potentially-constant* if it is constexpr or it has
|
| 130 |
reference or non-volatile const-qualified integral or enumeration type.
|
| 131 |
|
| 132 |
A constant-initialized potentially-constant variable V is *usable in
|
|
|
|
| 135 |
|
| 136 |
- V is constexpr,
|
| 137 |
- V is not initialized to a TU-local value, or
|
| 138 |
- P is in the same translation unit as D.
|
| 139 |
|
| 140 |
+
An object or reference is *potentially usable in constant expressions*
|
| 141 |
+
at point P if it is
|
| 142 |
|
| 143 |
+
- the object or reference declared by a variable that is usable in
|
| 144 |
+
constant expressions at P,
|
|
|
|
| 145 |
- a temporary object of non-volatile const-qualified literal type whose
|
| 146 |
lifetime is extended [[class.temporary]] to that of a variable that is
|
| 147 |
+
usable in constant expressions at P,
|
| 148 |
+
- a template parameter object [[temp.param]],
|
| 149 |
+
- a string literal object [[lex.string]],
|
| 150 |
+
- a non-mutable subobject of any of the above, or
|
| 151 |
+
- a reference member of any of the above.
|
| 152 |
+
|
| 153 |
+
An object or reference is *usable in constant expressions* at point P if
|
| 154 |
+
it is an object or reference that is potentially usable in constant
|
| 155 |
+
expressions at P and is constexpr-representable at P.
|
| 156 |
+
|
| 157 |
+
[*Example 3*:
|
| 158 |
+
|
| 159 |
+
``` cpp
|
| 160 |
+
struct A {
|
| 161 |
+
int* const & r;
|
| 162 |
+
};
|
| 163 |
+
void f(int x) {
|
| 164 |
+
constexpr A a = {&x};
|
| 165 |
+
static_assert(a.r == &x); // OK
|
| 166 |
+
[&] {
|
| 167 |
+
static_assert(a.r != nullptr); // error: a.r is not usable in constant expressions at this point
|
| 168 |
+
}();
|
| 169 |
+
}
|
| 170 |
+
```
|
| 171 |
+
|
| 172 |
+
— *end example*]
|
| 173 |
|
| 174 |
An expression E is a *core constant expression* unless the evaluation of
|
| 175 |
E, following the rules of the abstract machine [[intro.execution]],
|
| 176 |
would evaluate one of the following:
|
| 177 |
|
| 178 |
- `this` [[expr.prim.this]], except
|
| 179 |
- in a constexpr function [[dcl.constexpr]] that is being evaluated as
|
| 180 |
part of E or
|
| 181 |
- when appearing as the *postfix-expression* of an implicit or
|
| 182 |
explicit class member access expression [[expr.ref]];
|
| 183 |
+
- a control flow that passes through a declaration of a block variable
|
| 184 |
+
[[basic.scope.block]] with static [[basic.stc.static]] or thread
|
| 185 |
+
[[basic.stc.thread]] storage duration, unless that variable is usable
|
| 186 |
+
in constant expressions;
|
| 187 |
+
\[*Example 4*:
|
| 188 |
``` cpp
|
| 189 |
constexpr char test() {
|
| 190 |
static const int x = 5;
|
| 191 |
static constexpr char c[] = "Hello World";
|
| 192 |
return *(c + x);
|
| 193 |
}
|
| 194 |
static_assert(' ' == test());
|
| 195 |
```
|
| 196 |
|
| 197 |
— *end example*]
|
| 198 |
+
- an invocation of a non-constexpr function;[^29]
|
| 199 |
- an invocation of an undefined constexpr function;
|
| 200 |
- an invocation of an instantiated constexpr function that is not
|
| 201 |
constexpr-suitable;
|
| 202 |
- an invocation of a virtual function [[class.virtual]] for an object
|
| 203 |
whose dynamic type is constexpr-unknown;
|
| 204 |
- an expression that would exceed the implementation-defined limits (see
|
| 205 |
[[implimits]]);
|
| 206 |
+
- an operation that would have undefined or erroneous behavior as
|
| 207 |
+
specified in [[intro]] through [[\lastcorechapter]];[^30]
|
| 208 |
- an lvalue-to-rvalue conversion [[conv.lval]] unless it is applied to
|
| 209 |
+
- a glvalue of type cv `std::nullptr_t`,
|
| 210 |
- a non-volatile glvalue that refers to an object that is usable in
|
| 211 |
constant expressions, or
|
| 212 |
- a non-volatile glvalue of literal type that refers to a non-volatile
|
| 213 |
object whose lifetime began within the evaluation of E;
|
| 214 |
- an lvalue-to-rvalue conversion that is applied to a glvalue that
|
|
|
|
| 221 |
evaluation of E;
|
| 222 |
- in a *lambda-expression*, a reference to `this` or to a variable with
|
| 223 |
automatic storage duration defined outside that *lambda-expression*,
|
| 224 |
where the reference would be an odr-use
|
| 225 |
[[term.odr.use]], [[expr.prim.lambda]];
|
| 226 |
+
\[*Example 5*:
|
| 227 |
``` cpp
|
| 228 |
void g() {
|
| 229 |
const int n = 0;
|
| 230 |
[=] {
|
| 231 |
constexpr int i = n; // OK, n is not odr-used here
|
|
|
|
| 233 |
};
|
| 234 |
}
|
| 235 |
```
|
| 236 |
|
| 237 |
— *end example*]
|
| 238 |
+
\[*Note 4*:
|
| 239 |
If the odr-use occurs in an invocation of a function call operator of
|
| 240 |
a closure type, it no longer refers to `this` or to an enclosing
|
| 241 |
+
variable with automatic storage duration due to the transformation
|
| 242 |
[[expr.prim.lambda.capture]] of the *id-expression* into an access of
|
| 243 |
the corresponding data member.
|
| 244 |
+
\[*Example 6*:
|
| 245 |
``` cpp
|
| 246 |
auto monad = [](auto v) { return [=] { return v; }; };
|
| 247 |
auto bind = [](auto m) {
|
| 248 |
return [=](auto fvm) { return fvm(m()); };
|
| 249 |
};
|
|
|
|
| 252 |
static_assert(bind(monad(2))(monad)() == monad(2)());
|
| 253 |
```
|
| 254 |
|
| 255 |
— *end example*]
|
| 256 |
— *end note*]
|
| 257 |
+
- a conversion from a prvalue `P` of type “pointer to cv `void`” to a
|
| 258 |
+
type “*cv1* pointer to `T`”, where `T` is not *cv2* `void`, unless `P`
|
| 259 |
+
is a null pointer value or points to an object whose type is similar
|
| 260 |
+
to `T`;
|
| 261 |
- a `reinterpret_cast` [[expr.reinterpret.cast]];
|
| 262 |
- a modification of an object
|
| 263 |
+
[[expr.assign]], [[expr.post.incr]], [[expr.pre.incr]] unless it is
|
| 264 |
applied to a non-volatile lvalue of literal type that refers to a
|
| 265 |
non-volatile object whose lifetime began within the evaluation of E;
|
| 266 |
- an invocation of a destructor [[class.dtor]] or a function call whose
|
| 267 |
*postfix-expression* names a pseudo-destructor [[expr.call]], in
|
| 268 |
either case for an object whose lifetime did not begin within the
|
| 269 |
evaluation of E;
|
| 270 |
+
- a *new-expression* [[expr.new]], unless either
|
| 271 |
+
- the selected allocation function is a replaceable global allocation
|
| 272 |
+
function [[new.delete.single]], [[new.delete.array]] and the
|
| 273 |
+
allocated storage is deallocated within the evaluation of E, or
|
| 274 |
+
- the selected allocation function is a non-allocating form
|
| 275 |
+
[[new.delete.placement]] with an allocated type `T`, where
|
| 276 |
+
- the placement argument to the *new-expression* points to an object
|
| 277 |
+
whose type is similar to `T` [[conv.qual]] or, if `T` is an array
|
| 278 |
+
type, to the first element of an object of a type similar to `T`,
|
| 279 |
+
and
|
| 280 |
+
- the placement argument points to storage whose duration began
|
| 281 |
+
within the evaluation of E;
|
| 282 |
- a *delete-expression* [[expr.delete]], unless it deallocates a region
|
| 283 |
of storage allocated within the evaluation of E;
|
| 284 |
- a call to an instance of `std::allocator<T>::allocate`
|
| 285 |
[[allocator.members]], unless the allocated storage is deallocated
|
| 286 |
within the evaluation of E;
|
| 287 |
- a call to an instance of `std::allocator<T>::deallocate`
|
| 288 |
[[allocator.members]], unless it deallocates a region of storage
|
| 289 |
allocated within the evaluation of E;
|
| 290 |
+
- a construction of an exception object, unless the exception object and
|
| 291 |
+
all of its implicit copies created by invocations of
|
| 292 |
+
`std::current_exception` or `std::rethrow_exception` [[propagation]]
|
| 293 |
+
are destroyed within the evaluation of E;
|
| 294 |
- an *await-expression* [[expr.await]];
|
| 295 |
- a *yield-expression* [[expr.yield]];
|
| 296 |
- a three-way comparison [[expr.spaceship]], relational [[expr.rel]], or
|
| 297 |
equality [[expr.eq]] operator where the result is unspecified;
|
|
|
|
| 298 |
- a `dynamic_cast` [[expr.dynamic.cast]] or `typeid` [[expr.typeid]]
|
| 299 |
expression on a glvalue that refers to an object whose dynamic type is
|
| 300 |
+
constexpr-unknown;
|
| 301 |
+
- a `dynamic_cast` [[expr.dynamic.cast]] expression, `typeid`
|
| 302 |
+
[[expr.typeid]] expression, or `new-expression` [[expr.new]] that
|
| 303 |
+
would throw an exception where no definition of the exception type is
|
| 304 |
+
reachable;
|
| 305 |
+
- an expression that would produce an injected declaration (see below),
|
| 306 |
+
unless E is the corresponding expression of a
|
| 307 |
+
*consteval-block-declaration* [[dcl.pre]];
|
| 308 |
- an *asm-declaration* [[dcl.asm]];
|
| 309 |
- an invocation of the `va_arg` macro [[cstdarg.syn]];
|
| 310 |
- a non-constant library call [[defns.nonconst.libcall]]; or
|
| 311 |
+
- a `goto` statement [[stmt.goto]]. \[*Note 5*: A `goto` statement
|
| 312 |
+
introduced by equivalence [[stmt]] is not in scope. For example, a
|
| 313 |
+
`while` statement [[stmt.while]] can be executed during constant
|
| 314 |
+
evaluation. — *end note*]
|
| 315 |
+
|
| 316 |
+
It is *implementation-defined* whether E is a core constant expression
|
| 317 |
+
if E satisfies the constraints of a core constant expression, but
|
| 318 |
+
evaluation of E has runtime-undefined behavior.
|
| 319 |
|
| 320 |
It is unspecified whether E is a core constant expression if E satisfies
|
| 321 |
the constraints of a core constant expression, but evaluation of E would
|
| 322 |
evaluate
|
| 323 |
|
| 324 |
- an operation that has undefined behavior as specified in [[library]]
|
| 325 |
+
through [[exec]] or
|
| 326 |
+
- an invocation of the `va_start` macro [[cstdarg.syn]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
+
[*Example 7*:
|
| 329 |
|
| 330 |
``` cpp
|
| 331 |
int x; // not constant
|
| 332 |
struct A {
|
| 333 |
constexpr A(bool b) : m(b?42:x) { }
|
|
|
|
| 368 |
— *end example*]
|
| 369 |
|
| 370 |
For the purposes of determining whether an expression E is a core
|
| 371 |
constant expression, the evaluation of the body of a member function of
|
| 372 |
`std::allocator<T>` as defined in [[allocator.members]], where `T` is a
|
| 373 |
+
literal type, is ignored.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
|
| 375 |
For the purposes of determining whether E is a core constant expression,
|
| 376 |
the evaluation of a call to a trivial copy/move constructor or copy/move
|
| 377 |
assignment operator of a union is considered to copy/move the active
|
| 378 |
member of the union, if any.
|
| 379 |
|
| 380 |
+
[*Note 6*: The copy/move of the active member is
|
| 381 |
trivial. — *end note*]
|
| 382 |
|
| 383 |
+
For the purposes of determining whether E is a core constant expression,
|
| 384 |
+
the evaluation of an *id-expression* that names a structured binding `v`
|
| 385 |
+
[[dcl.struct.bind]] has the following semantics:
|
| 386 |
+
|
| 387 |
+
- If `v` is an lvalue referring to the object bound to an invented
|
| 388 |
+
reference `r`, the behavior is as if `r` were nominated.
|
| 389 |
+
- Otherwise, if `v` names an array element or class member, the behavior
|
| 390 |
+
is that of evaluating `e[i]` or `e.m`, respectively, where e is the
|
| 391 |
+
name of the variable initialized from the initializer of the
|
| 392 |
+
structured binding declaration, and i is the index of the element
|
| 393 |
+
referred to by `v` or m is the name of the member referred to by `v`,
|
| 394 |
+
respectively.
|
| 395 |
+
|
| 396 |
+
[*Example 8*:
|
| 397 |
+
|
| 398 |
+
``` cpp
|
| 399 |
+
#include <tuple>
|
| 400 |
+
struct S {
|
| 401 |
+
mutable int m;
|
| 402 |
+
constexpr S(int m): m(m) {}
|
| 403 |
+
virtual int g() const;
|
| 404 |
+
};
|
| 405 |
+
void f(std::tuple<S&> t) {
|
| 406 |
+
auto [r] = t;
|
| 407 |
+
static_assert(r.g() >= 0); // error: dynamic type is constexpr-unknown
|
| 408 |
+
constexpr auto [m] = S(1);
|
| 409 |
+
static_assert(m == 1); // error: lvalue-to-rvalue conversion on mutable
|
| 410 |
+
// subobject e.m, where e is a constexpr object of type S
|
| 411 |
+
using A = int[2];
|
| 412 |
+
constexpr auto [v0, v1] = A{2, 3};
|
| 413 |
+
static_assert(v0 + v1 == 5); // OK, equivalent to e[0] + e[1] where e is a constexpr array
|
| 414 |
+
}
|
| 415 |
+
```
|
| 416 |
+
|
| 417 |
+
— *end example*]
|
| 418 |
+
|
| 419 |
During the evaluation of an expression E as a core constant expression,
|
| 420 |
+
all *id-expression*s, *splice-expression*s, and uses of `*this` that
|
| 421 |
+
refer to an object or reference whose lifetime did not begin with the
|
| 422 |
+
evaluation of E are treated as referring to a specific instance of that
|
| 423 |
+
object or reference whose lifetime and that of all subobjects (including
|
| 424 |
+
all union members) includes the entire constant evaluation. For such an
|
| 425 |
+
object that is not usable in constant expressions, the dynamic type of
|
| 426 |
+
the object is *constexpr-unknown*. For such a reference that is not
|
| 427 |
+
usable in constant expressions, the reference is treated as binding to
|
| 428 |
+
an unspecified object of the referenced type whose lifetime and that of
|
| 429 |
+
all subobjects includes the entire constant evaluation and whose dynamic
|
| 430 |
+
type is constexpr-unknown.
|
| 431 |
|
| 432 |
+
[*Example 9*:
|
| 433 |
|
| 434 |
``` cpp
|
| 435 |
template <typename T, size_t N>
|
| 436 |
constexpr size_t array_size(T (&)[N]) {
|
| 437 |
return N;
|
|
|
|
| 483 |
constexpr auto& gallagher = typeid(trident); // error: constexpr-unknown dynamic type
|
| 484 |
```
|
| 485 |
|
| 486 |
— *end example*]
|
| 487 |
|
| 488 |
+
An object `a` is said to have *constant destruction* if
|
| 489 |
|
| 490 |
- it is not of class type nor (possibly multidimensional) array thereof,
|
| 491 |
or
|
| 492 |
- it is of class type or (possibly multidimensional) array thereof, that
|
| 493 |
+
class type has a constexpr destructor [[dcl.constexpr]], and for a
|
| 494 |
+
hypothetical expression E whose only effect is to destroy `a`, E would
|
| 495 |
+
be a core constant expression if the lifetime of `a` and its
|
| 496 |
+
non-mutable subobjects (but not its mutable subobjects) were
|
| 497 |
+
considered to start within E.
|
| 498 |
|
| 499 |
An *integral constant expression* is an expression of integral or
|
| 500 |
unscoped enumeration type, implicitly converted to a prvalue, where the
|
| 501 |
converted expression is a core constant expression.
|
| 502 |
|
| 503 |
+
[*Note 7*: Such expressions can be used as bit-field lengths
|
| 504 |
[[class.bit]], as enumerator initializers if the underlying type is not
|
| 505 |
fixed [[dcl.enum]], and as alignments [[dcl.align]]. — *end note*]
|
| 506 |
|
| 507 |
If an expression of literal class type is used in a context where an
|
| 508 |
integral constant expression is required, then that expression is
|
| 509 |
contextually implicitly converted [[conv]] to an integral or unscoped
|
| 510 |
enumeration type and the selected conversion function shall be
|
| 511 |
`constexpr`.
|
| 512 |
|
| 513 |
+
[*Example 10*:
|
| 514 |
|
| 515 |
``` cpp
|
| 516 |
struct A {
|
| 517 |
constexpr A(int i) : val(i) { }
|
| 518 |
constexpr operator int() const { return val; }
|
|
|
|
| 537 |
- function-to-pointer conversions [[conv.func]],
|
| 538 |
- qualification conversions [[conv.qual]],
|
| 539 |
- integral promotions [[conv.prom]],
|
| 540 |
- integral conversions [[conv.integral]] other than narrowing
|
| 541 |
conversions [[dcl.init.list]],
|
| 542 |
+
- floating-point promotions [[conv.fpprom]],
|
| 543 |
+
- floating-point conversions [[conv.double]] where the source value can
|
| 544 |
+
be represented exactly in the destination type,
|
| 545 |
- null pointer conversions [[conv.ptr]] from `std::nullptr_t`,
|
| 546 |
- null member pointer conversions [[conv.mem]] from `std::nullptr_t`,
|
| 547 |
and
|
| 548 |
- function pointer conversions [[conv.fctptr]],
|
| 549 |
|
| 550 |
and where the reference binding (if any) binds directly.
|
| 551 |
|
| 552 |
+
[*Note 8*: Such expressions can be used in `new` expressions
|
| 553 |
[[expr.new]], as case expressions [[stmt.switch]], as enumerator
|
| 554 |
initializers if the underlying type is fixed [[dcl.enum]], as array
|
| 555 |
+
bounds [[dcl.array]], as constant template arguments [[temp.arg]], and
|
| 556 |
+
as the constant expression of a *splice-specifier*
|
| 557 |
+
[[basic.splice]]. — *end note*]
|
| 558 |
|
| 559 |
A *contextually converted constant expression of type `bool`* is an
|
| 560 |
expression, contextually converted to `bool` [[conv]], where the
|
| 561 |
converted expression is a constant expression and the conversion
|
| 562 |
sequence contains only the conversions above.
|
| 563 |
|
| 564 |
+
A *constant expression* is either
|
|
|
|
|
|
|
|
|
|
| 565 |
|
| 566 |
+
- a glvalue core constant expression E for which
|
| 567 |
+
- E refers to a non-immediate function,
|
| 568 |
+
- E designates an object `o`, and if the complete object of `o` is of
|
| 569 |
+
consteval-only type then so is E,
|
| 570 |
+
\[*Example 11*:
|
| 571 |
+
``` cpp
|
| 572 |
+
struct Base { };
|
| 573 |
+
struct Derived : Base { std::meta::info r; };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 574 |
|
| 575 |
+
consteval const Base& fn(const Derived& derived) { return derived; }
|
|
|
|
|
|
|
|
|
|
| 576 |
|
| 577 |
+
constexpr Derived obj{.r=^^::}; // OK
|
| 578 |
+
constexpr const Derived& d = obj; // OK
|
| 579 |
+
constexpr const Base& b = fn(obj); // error: not a constant expression because Derived
|
| 580 |
+
// is a consteval-only type but Base is not.
|
| 581 |
+
```
|
| 582 |
+
|
| 583 |
+
— *end example*]
|
| 584 |
+
|
| 585 |
+
or
|
| 586 |
+
- a prvalue core constant expression whose result object [[basic.lval]]
|
| 587 |
+
satisfies the following constraints:
|
| 588 |
+
- each constituent reference refers to an object or a non-immediate
|
| 589 |
+
function,
|
| 590 |
+
- no constituent value of scalar type is an indeterminate or erroneous
|
| 591 |
+
value [[basic.indet]],
|
| 592 |
+
- no constituent value of pointer type is a pointer to an immediate
|
| 593 |
+
function or an invalid pointer value [[basic.compound]],
|
| 594 |
+
- no constituent value of pointer-to-member type designates an
|
| 595 |
+
immediate function, and
|
| 596 |
+
- unless the value is of consteval-only type,
|
| 597 |
+
- no constituent value of pointer-to-member type points to a direct
|
| 598 |
+
member of a consteval-only class type,
|
| 599 |
+
- no constituent value of pointer type points to or past an object
|
| 600 |
+
whose complete object is of consteval-only type, and
|
| 601 |
+
- no constituent reference refers to an object whose complete object
|
| 602 |
+
is of consteval-only type.
|
| 603 |
+
|
| 604 |
+
[*Note 9*: A glvalue core constant expression that either refers to or
|
| 605 |
points to an unspecified object is not a constant
|
| 606 |
expression. — *end note*]
|
| 607 |
|
| 608 |
+
[*Example 12*:
|
| 609 |
|
| 610 |
``` cpp
|
| 611 |
consteval int f() { return 42; }
|
| 612 |
consteval auto g() { return f; }
|
| 613 |
consteval int h(int (*p)() = g()) { return p(); }
|
| 614 |
constexpr int r = h(); // OK
|
| 615 |
constexpr auto e = g(); // error: a pointer to an immediate function is
|
| 616 |
// not a permitted result of a constant expression
|
| 617 |
+
|
| 618 |
+
struct S {
|
| 619 |
+
int x;
|
| 620 |
+
constexpr S() {}
|
| 621 |
+
};
|
| 622 |
+
int i() {
|
| 623 |
+
constexpr S s; // error: s.x has erroneous value
|
| 624 |
+
}
|
| 625 |
```
|
| 626 |
|
| 627 |
— *end example*]
|
| 628 |
|
| 629 |
*Recommended practice:* Implementations should provide consistent
|
| 630 |
results of floating-point evaluations, irrespective of whether the
|
| 631 |
evaluation is performed during translation or during program execution.
|
| 632 |
|
| 633 |
+
[*Note 10*:
|
| 634 |
|
| 635 |
Since this document imposes no restrictions on the accuracy of
|
| 636 |
floating-point operations, it is unspecified whether the evaluation of a
|
| 637 |
floating-point expression during translation yields the same result as
|
| 638 |
the evaluation of the same expression (or the same operations on the
|
| 639 |
same values) during program execution.
|
| 640 |
|
| 641 |
+
[*Example 13*:
|
| 642 |
|
| 643 |
``` cpp
|
| 644 |
bool f() {
|
| 645 |
char array[1 + int(1 + 0.2 - 0.1 - 0.1)]; // Must be evaluated during translation
|
| 646 |
int size = 1 + int(1 + 0.2 - 0.1 - 0.1); // May be evaluated at runtime
|
|
|
|
| 669 |
function and is not in an immediate function context. An aggregate
|
| 670 |
initialization is an immediate invocation if it evaluates a default
|
| 671 |
member initializer that has a subexpression that is an
|
| 672 |
immediate-escalating expression.
|
| 673 |
|
| 674 |
+
A potentially-evaluated expression or conversion is
|
| 675 |
+
*immediate-escalating* if it is neither initially in an immediate
|
| 676 |
+
function context nor a subexpression of an immediate invocation, and
|
| 677 |
|
| 678 |
+
- it is an *id-expression* or *splice-expression* that designates an
|
| 679 |
+
immediate function,
|
| 680 |
+
- it is an immediate invocation that is not a constant expression, or
|
| 681 |
+
- it is of consteval-only type [[basic.types.general]].
|
| 682 |
|
| 683 |
An *immediate-escalating* function is
|
| 684 |
|
| 685 |
- the call operator of a lambda that is not declared with the
|
| 686 |
`consteval` specifier,
|
|
|
|
| 690 |
defined with the `constexpr` specifier.
|
| 691 |
|
| 692 |
An immediate-escalating expression shall appear only in an
|
| 693 |
immediate-escalating function.
|
| 694 |
|
| 695 |
+
An *immediate function* is a function that is either
|
| 696 |
|
| 697 |
- declared with the `consteval` specifier, or
|
| 698 |
+
- an immediate-escalating function `F` whose function body contains
|
| 699 |
+
either
|
| 700 |
+
- an immediate-escalating expression or
|
| 701 |
+
- a definition of a non-constexpr variable with consteval-only type
|
| 702 |
|
| 703 |
+
whose innermost enclosing non-block scope is `F`’s function parameter
|
| 704 |
+
scope.
|
| 705 |
+
|
| 706 |
+
[*Example 14*:
|
| 707 |
|
| 708 |
``` cpp
|
| 709 |
consteval int id(int i) { return i; }
|
| 710 |
constexpr char id(char c) { return c; }
|
| 711 |
|
|
|
|
| 735 |
|
| 736 |
int x = 0;
|
| 737 |
|
| 738 |
template<class T>
|
| 739 |
constexpr T h(T t = id(x)) { // h<int> is not an immediate function
|
| 740 |
+
// id(x) is not evaluated when parsing the default argument[dcl.fct.default,temp.inst]
|
| 741 |
return t;
|
| 742 |
}
|
| 743 |
|
| 744 |
template<class T>
|
| 745 |
+
constexpr T hh() { // hh<int> is an immediate function because of the invocation
|
| 746 |
+
return h<T>(); // of the immediate function id in the default argument of h<int>
|
| 747 |
}
|
| 748 |
|
| 749 |
int i = hh<int>(); // error: hh<int>() is an immediate-escalating expression
|
| 750 |
// outside of an immediate-escalating function
|
| 751 |
|
|
|
|
| 756 |
|
| 757 |
template<class T>
|
| 758 |
constexpr int k(int) { // k<int> is not an immediate function because A(42) is a
|
| 759 |
return A(42).y; // constant expression and thus not immediate-escalating
|
| 760 |
}
|
| 761 |
+
|
| 762 |
+
constexpr int l(int c) pre(c >= 2) {
|
| 763 |
+
return (c % 2 == 0) ? c / 0 : c;
|
| 764 |
+
}
|
| 765 |
+
|
| 766 |
+
const int i0 = l(0); // dynamic initialization; contract violation or undefined behavior
|
| 767 |
+
const int i1 = l(1); // static initialization; value of 1 or contract violation at compile time
|
| 768 |
+
const int i2 = l(2); // dynamic initialization; undefined behavior
|
| 769 |
+
const int i3 = l(3); // static initialization; value of 3
|
| 770 |
```
|
| 771 |
|
| 772 |
— *end example*]
|
| 773 |
|
| 774 |
An expression or conversion is *manifestly constant-evaluated* if it is:
|
|
|
|
| 777 |
- the condition of a constexpr if statement [[stmt.if]], or
|
| 778 |
- an immediate invocation, or
|
| 779 |
- the result of substitution into an atomic constraint expression to
|
| 780 |
determine whether it is satisfied [[temp.constr.atomic]], or
|
| 781 |
- the initializer of a variable that is usable in constant expressions
|
| 782 |
+
or has constant initialization [[basic.start.static]].[^31]
|
| 783 |
+
\[*Example 15*:
|
| 784 |
``` cpp
|
| 785 |
template<bool> struct X {};
|
| 786 |
X<std::is_constant_evaluated()> x; // type X<true>
|
| 787 |
int y;
|
| 788 |
const int a = std::is_constant_evaluated() ? y : 1; // dynamic initialization to 1
|
|
|
|
| 801 |
int q = p + f(); // m is 17 for this call; initialized to 56
|
| 802 |
```
|
| 803 |
|
| 804 |
— *end example*]
|
| 805 |
|
| 806 |
+
[*Note 11*: Except for a *static_assert-message*, a manifestly
|
| 807 |
+
constant-evaluated expression is evaluated even in an unevaluated
|
| 808 |
+
operand [[term.unevaluated.operand]]. — *end note*]
|
| 809 |
+
|
| 810 |
+
The evaluation of an expression can introduce one or more *injected
|
| 811 |
+
declarations*. The evaluation is said to *produce* the declarations.
|
| 812 |
+
|
| 813 |
+
[*Note 12*: An invocation of the library function template
|
| 814 |
+
`std::meta::define_aggregate` produces an injected declaration
|
| 815 |
+
[[meta.reflection.define.aggregate]]. — *end note*]
|
| 816 |
+
|
| 817 |
+
Each such declaration has
|
| 818 |
+
|
| 819 |
+
- an associated *synthesized point*, which follows the last
|
| 820 |
+
non-synthesized program point in the translation unit containing that
|
| 821 |
+
declaration, and
|
| 822 |
+
- an associated *characteristic sequence* of values.
|
| 823 |
+
|
| 824 |
+
[*Note 13*: Special rules concerning reachability apply to synthesized
|
| 825 |
+
points [[module.reach]]. — *end note*]
|
| 826 |
+
|
| 827 |
+
[*Note 14*: The program is ill-formed if injected declarations with
|
| 828 |
+
different characteristic sequences define the same entity in different
|
| 829 |
+
translation units [[basic.def.odr]]. — *end note*]
|
| 830 |
+
|
| 831 |
+
A member of an entity defined by an injected declaration shall not have
|
| 832 |
+
a name reserved to the implementation [[lex.name]]; no diagnostic is
|
| 833 |
+
required.
|
| 834 |
+
|
| 835 |
+
Let C be a *consteval-block-declaration*, the evaluation of whose
|
| 836 |
+
corresponding expression produces an injected declaration for an entity
|
| 837 |
+
E. The program is ill-formed if either
|
| 838 |
+
|
| 839 |
+
- C is enclosed by a scope associated with E or
|
| 840 |
+
- letting P be a point whose immediate scope is that to which E belongs,
|
| 841 |
+
there is a function parameter scope or class scope that encloses
|
| 842 |
+
exactly one of C or P.
|
| 843 |
+
|
| 844 |
+
[*Example 16*:
|
| 845 |
+
|
| 846 |
+
``` cpp
|
| 847 |
+
struct S0 {
|
| 848 |
+
consteval {
|
| 849 |
+
std::meta::define_aggregate(^^S0, {}); // error: scope associated with S0 encloses the consteval block
|
| 850 |
+
}
|
| 851 |
+
};
|
| 852 |
+
|
| 853 |
+
struct S1;
|
| 854 |
+
consteval { std::meta::define_aggregate(^^S1, {}); } // OK
|
| 855 |
+
|
| 856 |
+
template <std::meta::info R> consteval void tfn1() {
|
| 857 |
+
std::meta::define_aggregate(R, {});
|
| 858 |
+
}
|
| 859 |
+
|
| 860 |
+
struct S2;
|
| 861 |
+
consteval { tfn1<^^S2>(); } // OK
|
| 862 |
+
|
| 863 |
+
template <std::meta::info R> consteval void tfn2() {
|
| 864 |
+
consteval { std::meta::define_aggregate(R, {}); }
|
| 865 |
+
}
|
| 866 |
+
|
| 867 |
+
struct S3;
|
| 868 |
+
consteval { tfn2<^^S3>(); }
|
| 869 |
+
// error: function parameter scope of tfn2<^^ S3> intervenes between the declaration of S3
|
| 870 |
+
// and the consteval block that produces the injected declaration
|
| 871 |
+
|
| 872 |
+
template <typename> struct TCls {
|
| 873 |
+
struct S4;
|
| 874 |
+
static void sfn() requires ([] {
|
| 875 |
+
consteval { std::meta::define_aggregate(^^S4, {}); }
|
| 876 |
+
return true;
|
| 877 |
+
}()) { }
|
| 878 |
+
};
|
| 879 |
+
|
| 880 |
+
consteval { TCls<void>::sfn(); } // error: TCls<void>::S4 is not enclosed by requires-clause lambda
|
| 881 |
+
|
| 882 |
+
struct S5;
|
| 883 |
+
struct Cls {
|
| 884 |
+
consteval { std::meta::define_aggregate(^^S5, {}); } // error: S5 is not enclosed by class Cls
|
| 885 |
+
};
|
| 886 |
+
|
| 887 |
+
struct S6;
|
| 888 |
+
consteval { // #1
|
| 889 |
+
struct S7; // local class
|
| 890 |
+
|
| 891 |
+
std::meta::define_aggregate(^^S7, {}); // error: consteval block #1 does not enclose itself,
|
| 892 |
+
// but encloses S7
|
| 893 |
+
|
| 894 |
+
struct S8; // local class
|
| 895 |
+
consteval { // #2
|
| 896 |
+
std::meta::define_aggregate(^^S6, {}); // error: consteval block #1 encloses
|
| 897 |
+
// consteval block #2 but not S6
|
| 898 |
+
|
| 899 |
+
std::meta::define_aggregate(^^S8, {}); // OK, consteval block #1 encloses both #2 and S8
|
| 900 |
+
}
|
| 901 |
+
}
|
| 902 |
+
```
|
| 903 |
+
|
| 904 |
+
— *end example*]
|
| 905 |
+
|
| 906 |
+
The *evaluation context* is a set of program points that determines the
|
| 907 |
+
behavior of certain functions used for reflection [[meta.reflection]].
|
| 908 |
+
During the evaluation V of an expression E as a core constant
|
| 909 |
+
expression, the evaluation context of an evaluation X
|
| 910 |
+
[[intro.execution]] consists of the following points:
|
| 911 |
+
|
| 912 |
+
- The program point EVAL-PT(L), where L is the point at which E appears,
|
| 913 |
+
and where EVAL-PT(P), for a point P, is a point R determined as
|
| 914 |
+
follows:
|
| 915 |
+
- If a potentially-evaluated subexpression [[intro.execution]] of a
|
| 916 |
+
default member initializer I appears at P, and a (possibly
|
| 917 |
+
aggregate) initialization during V is using I, then R is EVAL-PT(Q)
|
| 918 |
+
where Q is the point at which that initialization appears.
|
| 919 |
+
- Otherwise, if a potentially-evaluated subexpression of a default
|
| 920 |
+
argument [[dcl.fct.default]] appears at P, and an invocation of a
|
| 921 |
+
function [[expr.call]] during V is using that default argument, then
|
| 922 |
+
R is EVAL-PT(Q) where Q is the point at which that invocation
|
| 923 |
+
appears.
|
| 924 |
+
- Otherwise, R is P.
|
| 925 |
+
- Each synthesized point corresponding to an injected declaration
|
| 926 |
+
produced by any evaluation sequenced before X [[intro.execution]].
|
| 927 |
|
| 928 |
An expression or conversion is *potentially constant evaluated* if it
|
| 929 |
is:
|
| 930 |
|
| 931 |
- a manifestly constant-evaluated expression,
|
| 932 |
- a potentially-evaluated expression [[basic.def.odr]],
|
| 933 |
+
- an immediate subexpression of a *braced-init-list*,[^32]
|
| 934 |
- an expression of the form `&` *cast-expression* that occurs within a
|
| 935 |
+
templated entity,[^33] or
|
| 936 |
- a potentially-evaluated subexpression [[intro.execution]] of one of
|
| 937 |
the above.
|
| 938 |
|
| 939 |
A function or variable is *needed for constant evaluation* if it is:
|
| 940 |
|
|
|
|
| 942 |
that is potentially constant evaluated, or
|
| 943 |
- a potentially-constant variable named by a potentially constant
|
| 944 |
evaluated expression.
|
| 945 |
|
| 946 |
<!-- Link reference definitions -->
|
| 947 |
+
[\lastcorechapter]: #\lastcorechapter
|
| 948 |
[allocator.members]: mem.md#allocator.members
|
| 949 |
[bad.alloc]: support.md#bad.alloc
|
| 950 |
[bad.cast]: support.md#bad.cast
|
| 951 |
[bad.typeid]: support.md#bad.typeid
|
| 952 |
[basic.align]: basic.md#basic.align
|
| 953 |
[basic.compound]: basic.md#basic.compound
|
| 954 |
+
[basic.contract]: basic.md#basic.contract
|
| 955 |
+
[basic.contract.eval]: basic.md#basic.contract.eval
|
| 956 |
+
[basic.contract.general]: basic.md#basic.contract.general
|
| 957 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 958 |
[basic.fundamental]: basic.md#basic.fundamental
|
| 959 |
[basic.indet]: basic.md#basic.indet
|
| 960 |
[basic.life]: basic.md#basic.life
|
| 961 |
[basic.lookup]: basic.md#basic.lookup
|
| 962 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 963 |
[basic.lookup.general]: basic.md#basic.lookup.general
|
| 964 |
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 965 |
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 966 |
[basic.lval]: #basic.lval
|
| 967 |
+
[basic.namespace]: dcl.md#basic.namespace
|
| 968 |
[basic.pre]: basic.md#basic.pre
|
| 969 |
[basic.scope.block]: basic.md#basic.scope.block
|
| 970 |
[basic.scope.class]: basic.md#basic.scope.class
|
| 971 |
+
[basic.scope.contract]: basic.md#basic.scope.contract
|
| 972 |
[basic.scope.lambda]: basic.md#basic.scope.lambda
|
| 973 |
+
[basic.splice]: basic.md#basic.splice
|
| 974 |
[basic.start.main]: basic.md#basic.start.main
|
| 975 |
[basic.start.static]: basic.md#basic.start.static
|
| 976 |
[basic.stc.dynamic]: basic.md#basic.stc.dynamic
|
| 977 |
[basic.stc.dynamic.allocation]: basic.md#basic.stc.dynamic.allocation
|
| 978 |
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 979 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 980 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 981 |
[basic.type.qualifier]: basic.md#basic.type.qualifier
|
| 982 |
+
[basic.types.general]: basic.md#basic.types.general
|
| 983 |
[class]: class.md#class
|
| 984 |
[class.abstract]: class.md#class.abstract
|
| 985 |
[class.access]: class.md#class.access
|
| 986 |
[class.access.base]: class.md#class.access.base
|
| 987 |
+
[class.access.general]: class.md#class.access.general
|
| 988 |
[class.base.init]: class.md#class.base.init
|
| 989 |
[class.bit]: class.md#class.bit
|
| 990 |
[class.cdtor]: class.md#class.cdtor
|
| 991 |
[class.conv]: class.md#class.conv
|
| 992 |
[class.conv.fct]: class.md#class.conv.fct
|
|
|
|
| 997 |
[class.derived]: class.md#class.derived
|
| 998 |
[class.dtor]: class.md#class.dtor
|
| 999 |
[class.free]: class.md#class.free
|
| 1000 |
[class.friend]: class.md#class.friend
|
| 1001 |
[class.mem]: class.md#class.mem
|
| 1002 |
+
[class.mem.general]: class.md#class.mem.general
|
| 1003 |
[class.member.lookup]: basic.md#class.member.lookup
|
|
|
|
| 1004 |
[class.mfct.non.static]: class.md#class.mfct.non.static
|
| 1005 |
[class.mi]: class.md#class.mi
|
| 1006 |
+
[class.pre]: class.md#class.pre
|
| 1007 |
[class.prop]: class.md#class.prop
|
| 1008 |
[class.spaceship]: class.md#class.spaceship
|
| 1009 |
[class.static.mfct]: class.md#class.static.mfct
|
| 1010 |
[class.temporary]: basic.md#class.temporary
|
| 1011 |
[class.union]: class.md#class.union
|
|
|
|
| 1028 |
[conv.prom]: #conv.prom
|
| 1029 |
[conv.ptr]: #conv.ptr
|
| 1030 |
[conv.qual]: #conv.qual
|
| 1031 |
[conv.rank]: basic.md#conv.rank
|
| 1032 |
[conv.rval]: #conv.rval
|
|
|
|
| 1033 |
[cstdarg.syn]: support.md#cstdarg.syn
|
| 1034 |
[cstddef.syn]: support.md#cstddef.syn
|
| 1035 |
+
[dcl]: dcl.md#dcl
|
| 1036 |
[dcl.align]: dcl.md#dcl.align
|
| 1037 |
[dcl.array]: dcl.md#dcl.array
|
| 1038 |
[dcl.asm]: dcl.md#dcl.asm
|
| 1039 |
+
[dcl.attr.annotation]: dcl.md#dcl.attr.annotation
|
| 1040 |
[dcl.constexpr]: dcl.md#dcl.constexpr
|
| 1041 |
+
[dcl.contract.func]: dcl.md#dcl.contract.func
|
| 1042 |
+
[dcl.contract.res]: dcl.md#dcl.contract.res
|
| 1043 |
[dcl.decl]: dcl.md#dcl.decl
|
| 1044 |
[dcl.enum]: dcl.md#dcl.enum
|
| 1045 |
[dcl.fct]: dcl.md#dcl.fct
|
| 1046 |
[dcl.fct.def]: dcl.md#dcl.fct.def
|
| 1047 |
[dcl.fct.def.coroutine]: dcl.md#dcl.fct.def.coroutine
|
| 1048 |
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 1049 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 1050 |
[dcl.init]: dcl.md#dcl.init
|
| 1051 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
| 1052 |
+
[dcl.init.general]: dcl.md#dcl.init.general
|
| 1053 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 1054 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
| 1055 |
[dcl.init.string]: dcl.md#dcl.init.string
|
| 1056 |
[dcl.link]: dcl.md#dcl.link
|
| 1057 |
[dcl.mptr]: dcl.md#dcl.mptr
|
| 1058 |
[dcl.name]: dcl.md#dcl.name
|
| 1059 |
+
[dcl.pre]: dcl.md#dcl.pre
|
| 1060 |
[dcl.ptr]: dcl.md#dcl.ptr
|
| 1061 |
[dcl.ref]: dcl.md#dcl.ref
|
| 1062 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 1063 |
+
[dcl.spec.auto.general]: dcl.md#dcl.spec.auto.general
|
| 1064 |
[dcl.stc]: dcl.md#dcl.stc
|
| 1065 |
[dcl.struct.bind]: dcl.md#dcl.struct.bind
|
| 1066 |
[dcl.type]: dcl.md#dcl.type
|
| 1067 |
[dcl.type.auto.deduct]: dcl.md#dcl.type.auto.deduct
|
| 1068 |
+
[dcl.type.class.deduct]: dcl.md#dcl.type.class.deduct
|
| 1069 |
[dcl.type.cv]: dcl.md#dcl.type.cv
|
| 1070 |
[dcl.type.decltype]: dcl.md#dcl.type.decltype
|
| 1071 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 1072 |
[dcl.type.simple]: dcl.md#dcl.type.simple
|
| 1073 |
[defns.access]: intro.md#defns.access
|
| 1074 |
[defns.nonconst.libcall]: intro.md#defns.nonconst.libcall
|
|
|
|
|
|
|
| 1075 |
[depr.capture.this]: future.md#depr.capture.this
|
| 1076 |
[depr.volatile.type]: future.md#depr.volatile.type
|
| 1077 |
[except]: except.md#except
|
| 1078 |
+
[except.ctor]: except.md#except.ctor
|
| 1079 |
[except.handle]: except.md#except.handle
|
| 1080 |
[except.pre]: except.md#except.pre
|
| 1081 |
[except.spec]: except.md#except.spec
|
| 1082 |
[except.terminate]: except.md#except.terminate
|
| 1083 |
[except.throw]: except.md#except.throw
|
| 1084 |
+
[exec]: exec.md#exec
|
| 1085 |
[expr]: #expr
|
| 1086 |
[expr.add]: #expr.add
|
| 1087 |
[expr.alignof]: #expr.alignof
|
| 1088 |
[expr.arith.conv]: #expr.arith.conv
|
| 1089 |
+
[expr.assign]: #expr.assign
|
| 1090 |
[expr.await]: #expr.await
|
| 1091 |
[expr.bit.and]: #expr.bit.and
|
| 1092 |
[expr.call]: #expr.call
|
| 1093 |
[expr.cast]: #expr.cast
|
| 1094 |
[expr.comma]: #expr.comma
|
|
|
|
| 1111 |
[expr.post.incr]: #expr.post.incr
|
| 1112 |
[expr.pre]: #expr.pre
|
| 1113 |
[expr.pre.incr]: #expr.pre.incr
|
| 1114 |
[expr.prim]: #expr.prim
|
| 1115 |
[expr.prim.fold]: #expr.prim.fold
|
| 1116 |
+
[expr.prim.grammar]: #expr.prim.grammar
|
| 1117 |
[expr.prim.id]: #expr.prim.id
|
| 1118 |
[expr.prim.id.dtor]: #expr.prim.id.dtor
|
| 1119 |
[expr.prim.id.general]: #expr.prim.id.general
|
| 1120 |
[expr.prim.id.qual]: #expr.prim.id.qual
|
| 1121 |
[expr.prim.id.unqual]: #expr.prim.id.unqual
|
| 1122 |
[expr.prim.lambda]: #expr.prim.lambda
|
| 1123 |
[expr.prim.lambda.capture]: #expr.prim.lambda.capture
|
| 1124 |
[expr.prim.lambda.closure]: #expr.prim.lambda.closure
|
| 1125 |
[expr.prim.lambda.general]: #expr.prim.lambda.general
|
| 1126 |
[expr.prim.literal]: #expr.prim.literal
|
| 1127 |
+
[expr.prim.pack.index]: #expr.prim.pack.index
|
| 1128 |
[expr.prim.paren]: #expr.prim.paren
|
| 1129 |
[expr.prim.req]: #expr.prim.req
|
| 1130 |
[expr.prim.req.compound]: #expr.prim.req.compound
|
| 1131 |
[expr.prim.req.general]: #expr.prim.req.general
|
| 1132 |
[expr.prim.req.nested]: #expr.prim.req.nested
|
| 1133 |
[expr.prim.req.simple]: #expr.prim.req.simple
|
| 1134 |
[expr.prim.req.type]: #expr.prim.req.type
|
| 1135 |
+
[expr.prim.splice]: #expr.prim.splice
|
| 1136 |
[expr.prim.this]: #expr.prim.this
|
| 1137 |
[expr.prop]: #expr.prop
|
| 1138 |
[expr.ref]: #expr.ref
|
| 1139 |
+
[expr.reflect]: #expr.reflect
|
| 1140 |
[expr.reinterpret.cast]: #expr.reinterpret.cast
|
| 1141 |
[expr.rel]: #expr.rel
|
| 1142 |
[expr.shift]: #expr.shift
|
| 1143 |
[expr.sizeof]: #expr.sizeof
|
| 1144 |
[expr.spaceship]: #expr.spaceship
|
|
|
|
| 1161 |
[intro.memory]: basic.md#intro.memory
|
| 1162 |
[intro.object]: basic.md#intro.object
|
| 1163 |
[lex.ext]: lex.md#lex.ext
|
| 1164 |
[lex.icon]: lex.md#lex.icon
|
| 1165 |
[lex.literal]: lex.md#lex.literal
|
| 1166 |
+
[lex.name]: lex.md#lex.name
|
| 1167 |
[lex.string]: lex.md#lex.string
|
| 1168 |
[library]: library.md#library
|
| 1169 |
[meta.const.eval]: meta.md#meta.const.eval
|
| 1170 |
+
[meta.reflection]: meta.md#meta.reflection
|
| 1171 |
+
[meta.reflection.define.aggregate]: meta.md#meta.reflection.define.aggregate
|
| 1172 |
+
[module.reach]: module.md#module.reach
|
| 1173 |
+
[namespace.alias]: dcl.md#namespace.alias
|
| 1174 |
[namespace.udecl]: dcl.md#namespace.udecl
|
| 1175 |
[new.badlength]: support.md#new.badlength
|
| 1176 |
[new.delete.array]: support.md#new.delete.array
|
| 1177 |
[new.delete.placement]: support.md#new.delete.placement
|
| 1178 |
[new.delete.single]: support.md#new.delete.single
|
| 1179 |
[over]: over.md#over
|
| 1180 |
+
[over.assign]: over.md#over.assign
|
| 1181 |
[over.best.ics]: over.md#over.best.ics
|
| 1182 |
[over.built]: over.md#over.built
|
| 1183 |
[over.call]: over.md#over.call
|
| 1184 |
[over.call.func]: over.md#over.call.func
|
| 1185 |
[over.ics.user]: over.md#over.ics.user
|
| 1186 |
[over.literal]: over.md#over.literal
|
| 1187 |
[over.match]: over.md#over.match
|
| 1188 |
[over.match.class.deduct]: over.md#over.match.class.deduct
|
| 1189 |
+
[over.match.funcs]: over.md#over.match.funcs
|
| 1190 |
[over.match.oper]: over.md#over.match.oper
|
| 1191 |
[over.match.viable]: over.md#over.match.viable
|
| 1192 |
[over.oper]: over.md#over.oper
|
| 1193 |
[over.over]: over.md#over.over
|
| 1194 |
[over.sub]: over.md#over.sub
|
| 1195 |
+
[propagation]: support.md#propagation
|
| 1196 |
[replacement.functions]: library.md#replacement.functions
|
| 1197 |
[special]: class.md#special
|
| 1198 |
[std.modules]: library.md#std.modules
|
| 1199 |
+
[stmt]: stmt.md#stmt
|
| 1200 |
+
[stmt.contract.assert]: stmt.md#stmt.contract.assert
|
| 1201 |
[stmt.goto]: stmt.md#stmt.goto
|
| 1202 |
[stmt.if]: stmt.md#stmt.if
|
| 1203 |
[stmt.iter]: stmt.md#stmt.iter
|
| 1204 |
[stmt.jump]: stmt.md#stmt.jump
|
| 1205 |
[stmt.pre]: stmt.md#stmt.pre
|
| 1206 |
[stmt.return]: stmt.md#stmt.return
|
| 1207 |
[stmt.return.coroutine]: stmt.md#stmt.return.coroutine
|
| 1208 |
[stmt.switch]: stmt.md#stmt.switch
|
| 1209 |
+
[stmt.while]: stmt.md#stmt.while
|
| 1210 |
[support.runtime]: support.md#support.runtime
|
| 1211 |
[support.types.layout]: support.md#support.types.layout
|
| 1212 |
+
[temp.alias]: temp.md#temp.alias
|
| 1213 |
[temp.arg]: temp.md#temp.arg
|
| 1214 |
[temp.concept]: temp.md#temp.concept
|
| 1215 |
[temp.constr.atomic]: temp.md#temp.constr.atomic
|
| 1216 |
[temp.constr.constr]: temp.md#temp.constr.constr
|
| 1217 |
[temp.constr.decl]: temp.md#temp.constr.decl
|
| 1218 |
+
[temp.deduct.general]: temp.md#temp.deduct.general
|
| 1219 |
[temp.dep.constexpr]: temp.md#temp.dep.constexpr
|
| 1220 |
+
[temp.dep.type]: temp.md#temp.dep.type
|
| 1221 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 1222 |
[temp.explicit]: temp.md#temp.explicit
|
| 1223 |
[temp.mem]: temp.md#temp.mem
|
| 1224 |
[temp.names]: temp.md#temp.names
|
| 1225 |
[temp.over.link]: temp.md#temp.over.link
|
| 1226 |
[temp.param]: temp.md#temp.param
|
| 1227 |
[temp.pre]: temp.md#temp.pre
|
| 1228 |
[temp.res]: temp.md#temp.res
|
| 1229 |
[temp.spec.partial]: temp.md#temp.spec.partial
|
| 1230 |
+
[temp.type]: temp.md#temp.type
|
| 1231 |
[temp.variadic]: temp.md#temp.variadic
|
| 1232 |
[term.incomplete.type]: basic.md#term.incomplete.type
|
| 1233 |
[term.object.representation]: basic.md#term.object.representation
|
| 1234 |
[term.odr.use]: basic.md#term.odr.use
|
| 1235 |
+
[term.structural.type]: temp.md#term.structural.type
|
| 1236 |
[term.unevaluated.operand]: #term.unevaluated.operand
|
|
|
|
| 1237 |
[type.info]: support.md#type.info
|
| 1238 |
[typeinfo.syn]: support.md#typeinfo.syn
|
| 1239 |
|
| 1240 |
[^1]: The precedence of operators is not directly specified, but it can
|
| 1241 |
be derived from the syntax.
|
|
|
|
| 1243 |
[^2]: Overloaded operators are never assumed to be associative or
|
| 1244 |
commutative.
|
| 1245 |
|
| 1246 |
[^3]: The cast and assignment operators must still perform their
|
| 1247 |
specific conversions as described in [[expr.type.conv]],
|
| 1248 |
+
[[expr.cast]], [[expr.static.cast]] and [[expr.assign]].
|
| 1249 |
|
| 1250 |
[^4]: The intent of this list is to specify those circumstances in which
|
| 1251 |
an object can or cannot be aliased.
|
| 1252 |
|
| 1253 |
[^5]: For historical reasons, this conversion is called the
|
| 1254 |
“lvalue-to-rvalue” conversion, even though that name does not
|
| 1255 |
accurately reflect the taxonomy of expressions described in
|
| 1256 |
[[basic.lval]].
|
| 1257 |
|
| 1258 |
[^6]: In C++ class and array prvalues can have cv-qualified types. This
|
| 1259 |
+
differs from C, in which non-lvalues never have cv-qualified types.
|
|
|
|
| 1260 |
|
| 1261 |
[^7]: This conversion never applies to non-static member functions
|
| 1262 |
because an lvalue that refers to a non-static member function cannot
|
| 1263 |
be obtained.
|
| 1264 |
|
|
|
|
| 1274 |
|
| 1275 |
[^9]: As a consequence, operands of type `bool`, `char8_t`, `char16_t`,
|
| 1276 |
`char32_t`, `wchar_t`, or of enumeration type are converted to some
|
| 1277 |
integral type.
|
| 1278 |
|
| 1279 |
+
[^10]: This is true even if the subscript operator is used in the
|
|
|
|
|
|
|
|
|
|
| 1280 |
following common idiom: `&x[0]`.
|
| 1281 |
|
| 1282 |
+
[^11]: Note that `(*(E1))` is an lvalue.
|
| 1283 |
+
|
| 1284 |
[^12]: If the class member access expression is evaluated, the
|
| 1285 |
subexpression evaluation happens even if the result is unnecessary
|
| 1286 |
to determine the value of the entire postfix expression, for example
|
| 1287 |
if the *id-expression* denotes a static member.
|
| 1288 |
|
| 1289 |
+
[^13]: The most derived object [[intro.object]] pointed or referred to
|
|
|
|
|
|
|
| 1290 |
by `v` can contain other `B` objects as base classes, but these are
|
| 1291 |
ignored.
|
| 1292 |
|
| 1293 |
+
[^14]: The recommended name for such a class is `extended_type_info`.
|
| 1294 |
|
| 1295 |
+
[^15]: The types can have different cv-qualifiers, subject to the
|
|
|
|
|
|
|
|
|
|
| 1296 |
overall restriction that a `reinterpret_cast` cannot cast away
|
| 1297 |
constness.
|
| 1298 |
|
| 1299 |
+
[^16]: `T1` and `T2` can have different cv-qualifiers, subject to the
|
| 1300 |
overall restriction that a `reinterpret_cast` cannot cast away
|
| 1301 |
constness.
|
| 1302 |
|
| 1303 |
+
[^17]: This is sometimes referred to as a type pun when the result
|
| 1304 |
refers to the same object as the source glvalue.
|
| 1305 |
|
| 1306 |
+
[^18]: `const_cast` is not limited to conversions that cast away a
|
| 1307 |
const-qualifier.
|
| 1308 |
|
| 1309 |
+
[^19]: `sizeof``(``bool``)` is not required to be `1`.
|
| 1310 |
|
| 1311 |
+
[^20]: The actual size of a potentially-overlapping subobject can be
|
| 1312 |
less than the result of applying `sizeof` to the subobject, due to
|
| 1313 |
virtual base classes and less strict padding requirements on
|
| 1314 |
potentially-overlapping subobjects.
|
| 1315 |
|
| 1316 |
+
[^21]: If the conversion function returns a signed integer type, the
|
| 1317 |
second standard conversion converts to the unsigned type
|
| 1318 |
`std::size_t` and thus thwarts any attempt to detect a negative
|
| 1319 |
value afterwards.
|
| 1320 |
|
| 1321 |
+
[^22]: This can include evaluating a *new-initializer* and/or calling a
|
| 1322 |
constructor.
|
| 1323 |
|
| 1324 |
+
[^23]: A *lambda-expression* with a *lambda-introducer* that consists of
|
| 1325 |
empty square brackets can follow the `delete` keyword if the
|
| 1326 |
*lambda-expression* is enclosed in parentheses.
|
| 1327 |
|
| 1328 |
+
[^24]: For nonzero-length arrays, this is the same as a pointer to the
|
|
|
|
|
|
|
|
|
|
| 1329 |
first element of the array created by that *new-expression*.
|
| 1330 |
Zero-length arrays do not have a first element.
|
| 1331 |
|
| 1332 |
+
[^25]: This is often called truncation towards zero.
|
| 1333 |
|
| 1334 |
+
[^26]: As specified in [[basic.compound]], an object that is not an
|
| 1335 |
array element is considered to belong to a single-element array for
|
| 1336 |
this purpose and a pointer past the last element of an array of n
|
| 1337 |
elements is considered to be equivalent to a pointer to a
|
| 1338 |
hypothetical array element n for this purpose.
|
| 1339 |
|
| 1340 |
+
[^27]: As specified in [[basic.compound]], an object that is not an
|
| 1341 |
array element is considered to belong to a single-element array for
|
| 1342 |
this purpose and a pointer past the last element of an array of n
|
| 1343 |
elements is considered to be equivalent to a pointer to a
|
| 1344 |
hypothetical array element n for this purpose.
|
| 1345 |
|
| 1346 |
+
[^28]: As specified in [[basic.compound]], an object that is not an
|
| 1347 |
array element is considered to belong to a single-element array for
|
| 1348 |
this purpose.
|
| 1349 |
|
| 1350 |
+
[^29]: Overload resolution [[over.match]] is applied as usual.
|
| 1351 |
|
| 1352 |
+
[^30]: This includes, for example, signed integer overflow [[expr.pre]],
|
| 1353 |
certain pointer arithmetic [[expr.add]], division by zero
|
| 1354 |
[[expr.mul]], or certain shift operations [[expr.shift]].
|
| 1355 |
|
| 1356 |
+
[^31]: Testing this condition can involve a trial evaluation of its
|
| 1357 |
+
initializer, with evaluations of contract assertions using the
|
| 1358 |
+
ignore evaluation semantic [[basic.contract.eval]], as described
|
| 1359 |
+
above.
|
| 1360 |
|
| 1361 |
+
[^32]: In some cases, constant evaluation is needed to determine whether
|
| 1362 |
a narrowing conversion is performed [[dcl.init.list]].
|
| 1363 |
|
| 1364 |
+
[^33]: In some cases, constant evaluation is needed to determine whether
|
| 1365 |
such an expression is value-dependent [[temp.dep.constexpr]].
|