From Jason Turner

[dcl.constexpr]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpt6k_s0bk/{from.md → to.md} +64 -61
tmp/tmpt6k_s0bk/{from.md → to.md} RENAMED
@@ -1,17 +1,20 @@
1
- ### The `constexpr` specifier <a id="dcl.constexpr">[[dcl.constexpr]]</a>
2
 
3
  The `constexpr` specifier shall be applied only to the definition of a
4
  variable or variable template or the declaration of a function or
5
- function template. A function or static data member declared with the
6
- `constexpr` specifier is implicitly an inline function or variable (
7
- [[dcl.inline]]). If any declaration of a function or function template
8
- has a `constexpr` specifier, then all its declarations shall contain the
9
- `constexpr` specifier.
 
 
10
 
11
  [*Note 1*: An explicit specialization can differ from the template
12
- declaration with respect to the `constexpr` specifier. — *end note*]
 
13
 
14
  [*Note 2*: Function parameters cannot be declared
15
  `constexpr`. — *end note*]
16
 
17
  [*Example 1*:
@@ -26,11 +29,11 @@ constexpr struct pixel { // error: pixel is a type
26
  };
27
  constexpr pixel::pixel(int a)
28
  : x(a), y(x) // OK: definition
29
  { square(x); }
30
  constexpr pixel small(2); // error: square not defined, so small(2)
31
- // not constant~([expr.const]) so constexpr not satisfied
32
 
33
  constexpr void square(int &x) { // OK: definition
34
  x *= x;
35
  }
36
  constexpr pixel large(4); // OK: square defined
@@ -40,29 +43,33 @@ int next(constexpr int x) { // error: not for parameters
40
  extern constexpr int memsz; // error: not a definition
41
  ```
42
 
43
  — *end example*]
44
 
45
- A `constexpr` specifier used in the declaration of a function that is
46
- not a constructor declares that function to be a *constexpr function*.
47
- Similarly, a `constexpr` specifier used in a constructor declaration
48
- declares that constructor to be a *constexpr constructor*.
 
 
49
 
50
  The definition of a constexpr function shall satisfy the following
51
  requirements:
52
 
53
- - it shall not be virtual ([[class.virtual]]);
54
- - its return type shall be a literal type;
55
  - each of its parameter types shall be a literal type;
56
- - its *function-body* shall be `= delete`, `= default`, or a
57
- *compound-statement* that does not contain
58
- - an *asm-definition*,
 
59
  - a `goto` statement,
60
- - an identifier label ([[stmt.label]]),
61
- - a *try-block*, or
62
  - a definition of a variable of non-literal type or of static or
63
- thread storage duration or for which no initialization is performed.
 
 
 
64
 
65
  [*Example 2*:
66
 
67
  ``` cpp
68
  constexpr int square(int x)
@@ -77,12 +84,12 @@ constexpr int abs(int x) {
77
  constexpr int first(int n) {
78
  static int value = n; // error: variable has static storage duration
79
  return value;
80
  }
81
  constexpr int uninit() {
82
- int a; // error: variable is uninitialized
83
- return a;
84
  }
85
  constexpr int prev(int x)
86
  { return --x; } // OK
87
  constexpr int g(int x, int n) { // OK
88
  int r = 1;
@@ -91,30 +98,13 @@ constexpr int g(int x, int n) { // OK
91
  }
92
  ```
93
 
94
  — *end example*]
95
 
96
- The definition of a constexpr constructor shall satisfy the following
97
- requirements:
98
 
99
- - the class shall not have any virtual base classes;
100
- - each of the parameter types shall be a literal type;
101
- - its *function-body* shall not be a *function-try-block*.
102
-
103
- In addition, either its *function-body* shall be `= delete`, or it shall
104
- satisfy the following requirements:
105
-
106
- - either its *function-body* shall be `= default`, or the
107
- *compound-statement* of its *function-body* shall satisfy the
108
- requirements for a *function-body* of a constexpr function;
109
- - every non-variant non-static data member and base class subobject
110
- shall be initialized ([[class.base.init]]);
111
- - if the class is a union having variant members ([[class.union]]),
112
- exactly one of them shall be initialized;
113
- - if the class is a union-like class, but is not a union, for each of
114
- its anonymous union members having variant members, exactly one of
115
- them shall be initialized;
116
  - for a non-delegating constructor, every constructor selected to
117
  initialize non-static data members and base class subobjects shall be
118
  a constexpr constructor;
119
  - for a delegating constructor, the target constructor shall be a
120
  constexpr constructor.
@@ -129,16 +119,23 @@ private:
129
  };
130
  ```
131
 
132
  — *end example*]
133
 
 
 
 
 
 
 
134
  For a constexpr function or constexpr constructor that is neither
135
  defaulted nor a template, if no argument values exist such that an
136
  invocation of the function or constructor could be an evaluated
137
- subexpression of a core constant expression ([[expr.const]]), or, for a
138
- constructor, a constant initializer for some object (
139
- [[basic.start.static]]), the program is ill-formed, no diagnostic
 
140
  required.
141
 
142
  [*Example 4*:
143
 
144
  ``` cpp
@@ -161,27 +158,33 @@ struct D : B {
161
 
162
  — *end example*]
163
 
164
  If the instantiated template specialization of a constexpr function
165
  template or member function of a class template would fail to satisfy
166
- the requirements for a constexpr function or constexpr constructor, that
167
- specialization is still a constexpr function or constexpr constructor,
168
- even though a call to such a function cannot appear in a constant
169
- expression. If no specialization of the template would satisfy the
170
- requirements for a constexpr function or constexpr constructor when
171
- considered as a non-template function or constructor, the template is
172
- ill-formed, no diagnostic required.
173
 
174
- A call to a constexpr function produces the same result as a call to an
175
- equivalent non-constexpr function in all respects except that
 
176
 
177
- - a call to a constexpr function can appear in a constant expression (
178
- [[expr.const]]) and
179
- - copy elision is mandatory in a constant expression ([[class.copy]]).
 
180
 
181
- The `constexpr` specifier has no effect on the type of a constexpr
182
- function or a constexpr constructor.
 
 
 
 
 
183
 
184
  [*Example 5*:
185
 
186
  ``` cpp
187
  constexpr int bar(int x, int y) // OK
@@ -192,14 +195,14 @@ int bar(int x, int y) // error: redefinition of bar
192
  ```
193
 
194
  — *end example*]
195
 
196
  A `constexpr` specifier used in an object declaration declares the
197
- object as `const`. Such an object shall have literal type and shall be
198
  initialized. In any `constexpr` variable declaration, the
199
- full-expression of the initialization shall be a constant expression (
200
- [[expr.const]]).
201
 
202
  [*Example 6*:
203
 
204
  ``` cpp
205
  struct pixel {
 
1
+ ### The `constexpr` and `consteval` specifiers <a id="dcl.constexpr">[[dcl.constexpr]]</a>
2
 
3
  The `constexpr` specifier shall be applied only to the definition of a
4
  variable or variable template or the declaration of a function or
5
+ function template. The `consteval` specifier shall be applied only to
6
+ the declaration of a function or function template. A function or static
7
+ data member declared with the `constexpr` or `consteval` specifier is
8
+ implicitly an inline function or variable [[dcl.inline]]. If any
9
+ declaration of a function or function template has a `constexpr` or
10
+ `consteval` specifier, then all its declarations shall contain the same
11
+ specifier.
12
 
13
  [*Note 1*: An explicit specialization can differ from the template
14
+ declaration with respect to the `constexpr` or `consteval`
15
+ specifier. — *end note*]
16
 
17
  [*Note 2*: Function parameters cannot be declared
18
  `constexpr`. — *end note*]
19
 
20
  [*Example 1*:
 
29
  };
30
  constexpr pixel::pixel(int a)
31
  : x(a), y(x) // OK: definition
32
  { square(x); }
33
  constexpr pixel small(2); // error: square not defined, so small(2)
34
+ // not constant[expr.const] so constexpr not satisfied
35
 
36
  constexpr void square(int &x) { // OK: definition
37
  x *= x;
38
  }
39
  constexpr pixel large(4); // OK: square defined
 
43
  extern constexpr int memsz; // error: not a definition
44
  ```
45
 
46
  — *end example*]
47
 
48
+ A `constexpr` or `consteval` specifier used in the declaration of a
49
+ function declares that function to be a *constexpr function*. A function
50
+ or constructor declared with the `consteval` specifier is called an
51
+ *immediate function*. A destructor, an allocation function, or a
52
+ deallocation function shall not be declared with the `consteval`
53
+ specifier.
54
 
55
  The definition of a constexpr function shall satisfy the following
56
  requirements:
57
 
58
+ - its return type (if any) shall be a literal type;
 
59
  - each of its parameter types shall be a literal type;
60
+ - it shall not be a coroutine [[dcl.fct.def.coroutine]];
61
+ - if the function is a constructor or destructor, its class shall not
62
+ have any virtual base classes;
63
+ - its *function-body* shall not enclose [[stmt.pre]]
64
  - a `goto` statement,
65
+ - an identifier label [[stmt.label]],
 
66
  - a definition of a variable of non-literal type or of static or
67
+ thread storage duration.
68
+
69
+ \[*Note 3*: A *function-body* that is `= delete` or `= default`
70
+ encloses none of the above. — *end note*]
71
 
72
  [*Example 2*:
73
 
74
  ``` cpp
75
  constexpr int square(int x)
 
84
  constexpr int first(int n) {
85
  static int value = n; // error: variable has static storage duration
86
  return value;
87
  }
88
  constexpr int uninit() {
89
+ struct { int a; } s;
90
+ return s.a; // error: uninitialized read of s.a
91
  }
92
  constexpr int prev(int x)
93
  { return --x; } // OK
94
  constexpr int g(int x, int n) { // OK
95
  int r = 1;
 
98
  }
99
  ```
100
 
101
  — *end example*]
102
 
103
+ The definition of a constexpr constructor whose *function-body* is not
104
+ `= delete` shall additionally satisfy the following requirements:
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  - for a non-delegating constructor, every constructor selected to
107
  initialize non-static data members and base class subobjects shall be
108
  a constexpr constructor;
109
  - for a delegating constructor, the target constructor shall be a
110
  constexpr constructor.
 
119
  };
120
  ```
121
 
122
  — *end example*]
123
 
124
+ The definition of a constexpr destructor whose *function-body* is not
125
+ `= delete` shall additionally satisfy the following requirement:
126
+
127
+ - for every subobject of class type or (possibly multi-dimensional)
128
+ array thereof, that class type shall have a constexpr destructor.
129
+
130
  For a constexpr function or constexpr constructor that is neither
131
  defaulted nor a template, if no argument values exist such that an
132
  invocation of the function or constructor could be an evaluated
133
+ subexpression of a core constant expression [[expr.const]], or, for a
134
+ constructor, an evaluated subexpression of the initialization
135
+ full-expression of some constant-initialized object
136
+ [[basic.start.static]], the program is ill-formed, no diagnostic
137
  required.
138
 
139
  [*Example 4*:
140
 
141
  ``` cpp
 
158
 
159
  — *end example*]
160
 
161
  If the instantiated template specialization of a constexpr function
162
  template or member function of a class template would fail to satisfy
163
+ the requirements for a constexpr function, that specialization is still
164
+ a constexpr function, even though a call to such a function cannot
165
+ appear in a constant expression. If no specialization of the template
166
+ would satisfy the requirements for a constexpr function when considered
167
+ as a non-template function, the template is ill-formed, no diagnostic
168
+ required.
 
169
 
170
+ An invocation of a constexpr function in a given context produces the
171
+ same result as an invocation of an equivalent non-constexpr function in
172
+ the same context in all respects except that
173
 
174
+ - an invocation of a constexpr function can appear in a constant
175
+ expression [[expr.const]] and
176
+ - copy elision is not performed in a constant expression
177
+ [[class.copy.elision]].
178
 
179
+ [*Note 4*: Declaring a function constexpr can change whether an
180
+ expression is a constant expression. This can indirectly cause calls to
181
+ `std::is_constant_evaluated` within an invocation of the function to
182
+ produce a different value. — *end note*]
183
+
184
+ The `constexpr` and `consteval` specifiers have no effect on the type of
185
+ a constexpr function.
186
 
187
  [*Example 5*:
188
 
189
  ``` cpp
190
  constexpr int bar(int x, int y) // OK
 
195
  ```
196
 
197
  — *end example*]
198
 
199
  A `constexpr` specifier used in an object declaration declares the
200
+ object as const. Such an object shall have literal type and shall be
201
  initialized. In any `constexpr` variable declaration, the
202
+ full-expression of the initialization shall be a constant expression
203
+ [[expr.const]]. A `constexpr` variable shall have constant destruction.
204
 
205
  [*Example 6*:
206
 
207
  ``` cpp
208
  struct pixel {