From Jason Turner

[dcl.constexpr]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpqu6u_hlu/{from.md → to.md} +76 -49
tmp/tmpqu6u_hlu/{from.md → to.md} RENAMED
@@ -1,16 +1,22 @@
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, the declaration of a function or function
5
- template, or the declaration of a static data member of a literal type (
6
- [[basic.types]]). If any declaration of a function, function template,
7
- or variable template has a `constexpr` specifier, then all its
8
- declarations shall contain the `constexpr` specifier. An explicit
9
- specialization can differ from the template declaration with respect to
10
- the `constexpr` specifier. Function parameters cannot be declared
11
- `constexpr`.
 
 
 
 
 
 
12
 
13
  ``` cpp
14
  constexpr void square(int &x); // OK: declaration
15
  constexpr int bufsz = 1024; // OK: definition
16
  constexpr struct pixel { // error: pixel is a type
@@ -32,31 +38,34 @@ int next(constexpr int x) { // error: not for parameters
32
  return x + 1;
33
  }
34
  extern constexpr int memsz; // error: not a definition
35
  ```
36
 
 
 
37
  A `constexpr` specifier used in the declaration of a function that is
38
  not a constructor declares that function to be a *constexpr function*.
39
  Similarly, a `constexpr` specifier used in a constructor declaration
40
- declares that constructor to be a *constexpr constructor*. `constexpr`
41
- functions and `constexpr` constructors are implicitly `inline` (
42
- [[dcl.fct.spec]]).
43
 
44
- The definition of a `constexpr` function shall satisfy the following
45
- constraints:
46
 
47
  - it shall not be virtual ([[class.virtual]]);
48
  - its return type shall be a literal type;
49
  - each of its parameter types shall be a literal type;
50
  - its *function-body* shall be `= delete`, `= default`, or a
51
  *compound-statement* that does not contain
52
  - an *asm-definition*,
53
  - a `goto` statement,
 
54
  - a *try-block*, or
55
  - a definition of a variable of non-literal type or of static or
56
  thread storage duration or for which no initialization is performed.
57
 
 
 
58
  ``` cpp
59
  constexpr int square(int x)
60
  { return x * x; } // OK
61
  constexpr long long_max()
62
  { return 2147483647; } // OK
@@ -80,51 +89,60 @@ constexpr int g(int x, int n) { // OK
80
  while (--n > 0) r *= x;
81
  return r;
82
  }
83
  ```
84
 
85
- The definition of a `constexpr` constructor shall satisfy the following
86
- constraints:
 
 
87
 
88
  - the class shall not have any virtual base classes;
89
  - each of the parameter types shall be a literal type;
90
- - its *function-body* shall not be a *function-try-block*;
91
 
92
  In addition, either its *function-body* shall be `= delete`, or it shall
93
- satisfy the following constraints:
94
 
95
  - either its *function-body* shall be `= default`, or the
96
  *compound-statement* of its *function-body* shall satisfy the
97
- constraints for a *function-body* of a `constexpr` function;
98
- - every non-variant non-static data member and base class sub-object
99
  shall be initialized ([[class.base.init]]);
100
  - if the class is a union having variant members ([[class.union]]),
101
  exactly one of them shall be initialized;
102
  - if the class is a union-like class, but is not a union, for each of
103
  its anonymous union members having variant members, exactly one of
104
  them shall be initialized;
105
  - for a non-delegating constructor, every constructor selected to
106
- initialize non-static data members and base class sub-objects shall be
107
- a `constexpr` constructor;
108
  - for a delegating constructor, the target constructor shall be a
109
- `constexpr` constructor.
 
 
110
 
111
  ``` cpp
112
  struct Length {
113
  constexpr explicit Length(int i = 0) : val(i) { }
114
  private:
115
  int val;
116
  };
117
  ```
118
 
119
- For a non-template, non-defaulted `constexpr` function or a
120
- non-template, non-defaulted, non-inheriting `constexpr` constructor, if
121
- no argument values exist such that an invocation of the function or
122
- constructor could be an evaluated subexpression of a core constant
123
- expression ([[expr.const]]), the program is ill-formed; no diagnostic
 
 
 
124
  required.
125
 
 
 
126
  ``` cpp
127
  constexpr int f(bool b)
128
  { return b ? throw 0 : 0; } // OK
129
  constexpr int f() { return f(true); } // ill-formed, no diagnostic required
130
 
@@ -139,48 +157,57 @@ struct D : B {
139
  constexpr D() : B(global) { } // ill-formed, no diagnostic required
140
  // lvalue-to-rvalue conversion on non-constant global
141
  };
142
  ```
143
 
144
- If the instantiated template specialization of a `constexpr` function
 
 
145
  template or member function of a class template would fail to satisfy
146
- the requirements for a `constexpr` function or `constexpr` constructor,
147
- that specialization is still a `constexpr` function or `constexpr`
148
- constructor, even though a call to such a function cannot appear in a
149
- constant expression. If no specialization of the template would satisfy
150
- the requirements for a `constexpr` function or `constexpr` constructor
151
- when considered as a non-template function or constructor, the template
152
- is ill-formed; no diagnostic required.
153
-
154
- A call to a `constexpr` function produces the same result as a call to
155
- an equivalent non-`constexpr` function in all respects except that a
156
- call to a `constexpr` function can appear in a constant expression.
157
-
158
- The `constexpr` specifier has no effect on the type of a `constexpr`
159
- function or a `constexpr` constructor.
 
 
 
 
 
160
 
161
  ``` cpp
162
  constexpr int bar(int x, int y) // OK
163
  { return x + y + x*y; }
164
  // ...
165
  int bar(int x, int y) // error: redefinition of bar
166
  { return x * 2 + 3 * y; }
167
  ```
168
 
 
 
169
  A `constexpr` specifier used in an object declaration declares the
170
  object as `const`. Such an object shall have literal type and shall be
171
- initialized. If it is initialized by a constructor call, that call shall
172
- be a constant expression ([[expr.const]]). Otherwise, or if a
173
- `constexpr` specifier is used in a reference declaration, every
174
- full-expression that appears in its initializer shall be a constant
175
- expression. Each implicit conversion used in converting the initializer
176
- expressions and each constructor call used for the initialization is
177
- part of such a full-expression.
178
 
179
  ``` cpp
180
  struct pixel {
181
  int x, y;
182
  };
183
  constexpr pixel ur = { 1294, 1024 }; // OK
184
  constexpr pixel origin; // error: initializer missing
185
  ```
186
 
 
 
 
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*:
18
 
19
  ``` cpp
20
  constexpr void square(int &x); // OK: declaration
21
  constexpr int bufsz = 1024; // OK: definition
22
  constexpr struct pixel { // error: pixel is a type
 
38
  return x + 1;
39
  }
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)
69
  { return x * x; } // OK
70
  constexpr long long_max()
71
  { return 2147483647; } // OK
 
89
  while (--n > 0) r *= x;
90
  return r;
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.
121
+
122
+ [*Example 3*:
123
 
124
  ``` cpp
125
  struct Length {
126
  constexpr explicit Length(int i = 0) : val(i) { }
127
  private:
128
  int val;
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
145
  constexpr int f(bool b)
146
  { return b ? throw 0 : 0; } // OK
147
  constexpr int f() { return f(true); } // ill-formed, no diagnostic required
148
 
 
157
  constexpr D() : B(global) { } // ill-formed, no diagnostic required
158
  // lvalue-to-rvalue conversion on non-constant global
159
  };
160
  ```
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
188
  { return x + y + x*y; }
189
  // ...
190
  int bar(int x, int y) // error: redefinition of bar
191
  { return x * 2 + 3 * y; }
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 {
206
  int x, y;
207
  };
208
  constexpr pixel ur = { 1294, 1024 }; // OK
209
  constexpr pixel origin; // error: initializer missing
210
  ```
211
 
212
+ — *end example*]
213
+