From Jason Turner

[dcl.init.general]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp9rkfcd2d/{from.md → to.md} +361 -0
tmp/tmp9rkfcd2d/{from.md → to.md} RENAMED
@@ -0,0 +1,361 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### General <a id="dcl.init.general">[[dcl.init.general]]</a>
2
+
3
+ The process of initialization described in [[dcl.init]] applies to all
4
+ initializations regardless of syntactic context, including the
5
+ initialization of a function parameter [[expr.call]], the initialization
6
+ of a return value [[stmt.return]], or when an initializer follows a
7
+ declarator.
8
+
9
+ ``` bnf
10
+ initializer:
11
+ brace-or-equal-initializer
12
+ '(' expression-list ')'
13
+ ```
14
+
15
+ ``` bnf
16
+ brace-or-equal-initializer:
17
+ '=' initializer-clause
18
+ braced-init-list
19
+ ```
20
+
21
+ ``` bnf
22
+ initializer-clause:
23
+ assignment-expression
24
+ braced-init-list
25
+ ```
26
+
27
+ ``` bnf
28
+ braced-init-list:
29
+ '{' initializer-list ','ₒₚₜ '}'
30
+ '{' designated-initializer-list ','ₒₚₜ '}'
31
+ '{' '}'
32
+ ```
33
+
34
+ ``` bnf
35
+ initializer-list:
36
+ initializer-clause '...'ₒₚₜ
37
+ initializer-list ',' initializer-clause '...'ₒₚₜ
38
+ ```
39
+
40
+ ``` bnf
41
+ designated-initializer-list:
42
+ designated-initializer-clause
43
+ designated-initializer-list ',' designated-initializer-clause
44
+ ```
45
+
46
+ ``` bnf
47
+ designated-initializer-clause:
48
+ designator brace-or-equal-initializer
49
+ ```
50
+
51
+ ``` bnf
52
+ designator:
53
+ '.' identifier
54
+ ```
55
+
56
+ ``` bnf
57
+ expr-or-braced-init-list:
58
+ expression
59
+ braced-init-list
60
+ ```
61
+
62
+ [*Note 1*: The rules in [[dcl.init]] apply even if the grammar permits
63
+ only the *brace-or-equal-initializer* form of *initializer* in a given
64
+ context. — *end note*]
65
+
66
+ Except for objects declared with the `constexpr` specifier, for which
67
+ see  [[dcl.constexpr]], an *initializer* in the definition of a variable
68
+ can consist of arbitrary expressions involving literals and previously
69
+ declared variables and functions, regardless of the variable’s storage
70
+ duration.
71
+
72
+ [*Example 1*:
73
+
74
+ ``` cpp
75
+ int f(int);
76
+ int a = 2;
77
+ int b = f(a);
78
+ int c(b);
79
+ ```
80
+
81
+ — *end example*]
82
+
83
+ [*Note 2*: Default arguments are more restricted; see 
84
+ [[dcl.fct.default]]. — *end note*]
85
+
86
+ [*Note 3*: The order of initialization of variables with static storage
87
+ duration is described in  [[basic.start]] and 
88
+ [[stmt.dcl]]. — *end note*]
89
+
90
+ A declaration D of a variable with linkage shall not have an
91
+ *initializer* if D inhabits a block scope.
92
+
93
+ To *zero-initialize* an object or reference of type `T` means:
94
+
95
+ - if `T` is a scalar type [[term.scalar.type]], the object is
96
+ initialized to the value obtained by converting the integer literal
97
+ `0` (zero) to `T`;[^5]
98
+ - if `T` is a (possibly cv-qualified) non-union class type, its padding
99
+ bits [[term.padding.bits]] are initialized to zero bits and each
100
+ non-static data member, each non-virtual base class subobject, and, if
101
+ the object is not a base class subobject, each virtual base class
102
+ subobject is zero-initialized;
103
+ - if `T` is a (possibly cv-qualified) union type, its padding bits
104
+ [[term.padding.bits]] are initialized to zero bits and the object’s
105
+ first non-static named data member is zero-initialized;
106
+ - if `T` is an array type, each element is zero-initialized;
107
+ - if `T` is a reference type, no initialization is performed.
108
+
109
+ To *default-initialize* an object of type `T` means:
110
+
111
+ - If `T` is a (possibly cv-qualified) class type [[class]], constructors
112
+ are considered. The applicable constructors are enumerated
113
+ [[over.match.ctor]], and the best one for the *initializer* `()` is
114
+ chosen through overload resolution [[over.match]]. The constructor
115
+ thus selected is called, with an empty argument list, to initialize
116
+ the object.
117
+ - If `T` is an array type, each element is default-initialized.
118
+ - Otherwise, no initialization is performed.
119
+
120
+ A class type `T` is *const-default-constructible* if
121
+ default-initialization of `T` would invoke a user-provided constructor
122
+ of `T` (not inherited from a base class) or if
123
+
124
+ - each direct non-variant non-static data member `M` of `T` has a
125
+ default member initializer or, if `M` is of class type `X` (or array
126
+ thereof), `X` is const-default-constructible,
127
+ - if `T` is a union with at least one non-static data member, exactly
128
+ one variant member has a default member initializer,
129
+ - if `T` is not a union, for each anonymous union member with at least
130
+ one non-static data member (if any), exactly one non-static data
131
+ member has a default member initializer, and
132
+ - each potentially constructed base class of `T` is
133
+ const-default-constructible.
134
+
135
+ If a program calls for the default-initialization of an object of a
136
+ const-qualified type `T`, `T` shall be a const-default-constructible
137
+ class type or array thereof.
138
+
139
+ To *value-initialize* an object of type `T` means:
140
+
141
+ - if `T` is a (possibly cv-qualified) class type [[class]], then
142
+ - if `T` has either no default constructor [[class.default.ctor]] or a
143
+ default constructor that is user-provided or deleted, then the
144
+ object is default-initialized;
145
+ - otherwise, the object is zero-initialized and the semantic
146
+ constraints for default-initialization are checked, and if `T` has a
147
+ non-trivial default constructor, the object is default-initialized;
148
+ - if `T` is an array type, then each element is value-initialized;
149
+ - otherwise, the object is zero-initialized.
150
+
151
+ A program that calls for default-initialization or value-initialization
152
+ of an entity of reference type is ill-formed.
153
+
154
+ [*Note 4*: For every object of static storage duration, static
155
+ initialization [[basic.start.static]] is performed at program startup
156
+ before any other initialization takes place. In some cases, additional
157
+ initialization is done later. — *end note*]
158
+
159
+ If no initializer is specified for an object, the object is
160
+ default-initialized.
161
+
162
+ If the entity being initialized does not have class type, the
163
+ *expression-list* in a parenthesized initializer shall be a single
164
+ expression.
165
+
166
+ The initialization that occurs in the `=` form of a
167
+ *brace-or-equal-initializer* or *condition* [[stmt.select]], as well as
168
+ in argument passing, function return, throwing an exception
169
+ [[except.throw]], handling an exception [[except.handle]], and aggregate
170
+ member initialization other than by a *designated-initializer-clause*
171
+ [[dcl.init.aggr]], is called *copy-initialization*.
172
+
173
+ [*Note 5*: Copy-initialization can invoke a move
174
+ [[class.copy.ctor]]. — *end note*]
175
+
176
+ The initialization that occurs
177
+
178
+ - for an *initializer* that is a parenthesized *expression-list* or a
179
+ *braced-init-list*,
180
+ - for a *new-initializer* [[expr.new]],
181
+ - in a `static_cast` expression [[expr.static.cast]],
182
+ - in a functional notation type conversion [[expr.type.conv]], and
183
+ - in the *braced-init-list* form of a *condition*
184
+
185
+ is called *direct-initialization*.
186
+
187
+ The semantics of initializers are as follows. The *destination type* is
188
+ the type of the object or reference being initialized and the *source
189
+ type* is the type of the initializer expression. If the initializer is
190
+ not a single (possibly parenthesized) expression, the source type is not
191
+ defined.
192
+
193
+ - If the initializer is a (non-parenthesized) *braced-init-list* or is
194
+ `=` *braced-init-list*, the object or reference is list-initialized
195
+ [[dcl.init.list]].
196
+ - If the destination type is a reference type, see  [[dcl.init.ref]].
197
+ - If the destination type is an array of characters, an array of
198
+ `char8_t`, an array of `char16_t`, an array of `char32_t`, or an array
199
+ of `wchar_t`, and the initializer is a *string-literal*, see 
200
+ [[dcl.init.string]].
201
+ - If the initializer is `()`, the object is value-initialized.
202
+ \[*Note 6*:
203
+ Since `()` is not permitted by the syntax for *initializer*,
204
+ ``` cpp
205
+ X a();
206
+ ```
207
+
208
+ is not the declaration of an object of class `X`, but the declaration
209
+ of a function taking no arguments and returning an `X`. The form `()`
210
+ is permitted in certain other initialization contexts
211
+ [[expr.new]], [[expr.type.conv]], [[class.base.init]].
212
+ — *end note*]
213
+ - Otherwise, if the destination type is an array, the object is
214
+ initialized as follows. Let x₁, …, xₖ be the elements of the
215
+ *expression-list*. If the destination type is an array of unknown
216
+ bound, it is defined as having k elements. Let n denote the array size
217
+ after this potential adjustment. If k is greater than n, the program
218
+ is ill-formed. Otherwise, the iᵗʰ array element is copy-initialized
219
+ with xᵢ for each 1 ≤ i ≤ k, and value-initialized for each k < i ≤ n.
220
+ For each 1 ≤ i < j ≤ n, every value computation and side effect
221
+ associated with the initialization of the iᵗʰ element of the array is
222
+ sequenced before those associated with the initialization of the jᵗʰ
223
+ element.
224
+ - Otherwise, if the destination type is a (possibly cv-qualified) class
225
+ type:
226
+ - If the initializer expression is a prvalue and the cv-unqualified
227
+ version of the source type is the same class as the class of the
228
+ destination, the initializer expression is used to initialize the
229
+ destination object. \[*Example 2*: `T x = T(T(T()));`
230
+ value-initializes `x`. — *end example*]
231
+ - Otherwise, if the initialization is direct-initialization, or if it
232
+ is copy-initialization where the cv-unqualified version of the
233
+ source type is the same class as, or a derived class of, the class
234
+ of the destination, constructors are considered. The applicable
235
+ constructors are enumerated [[over.match.ctor]], and the best one is
236
+ chosen through overload resolution [[over.match]]. Then:
237
+ - If overload resolution is successful, the selected constructor is
238
+ called to initialize the object, with the initializer expression
239
+ or *expression-list* as its argument(s).
240
+ - Otherwise, if no constructor is viable, the destination type is an
241
+ aggregate class, and the initializer is a parenthesized
242
+ *expression-list*, the object is initialized as follows. Let e₁,
243
+ …, eₙ be the elements of the aggregate [[dcl.init.aggr]]. Let x₁,
244
+ …, xₖ be the elements of the *expression-list*. If k is greater
245
+ than n, the program is ill-formed. The element eᵢ is
246
+ copy-initialized with xᵢ for 1 ≤ i ≤ k. The remaining elements are
247
+ initialized with their default member initializers, if any, and
248
+ otherwise are value-initialized. For each 1 ≤ i < j ≤ n, every
249
+ value computation and side effect associated with the
250
+ initialization of eᵢ is sequenced before those associated with the
251
+ initialization of eⱼ.
252
+ \[*Note 7*:
253
+ By contrast with direct-list-initialization, narrowing conversions
254
+ [[dcl.init.list]] are permitted, designators are not permitted, a
255
+ temporary object bound to a reference does not have its lifetime
256
+ extended [[class.temporary]], and there is no brace elision.
257
+ \[*Example 3*:
258
+ ``` cpp
259
+ struct A {
260
+ int a;
261
+ int&& r;
262
+ };
263
+
264
+ int f();
265
+ int n = 10;
266
+
267
+ A a1{1, f()}; // OK, lifetime is extended
268
+ A a2(1, f()); // well-formed, but dangling reference
269
+ A a3{1.0, 1}; // error: narrowing conversion
270
+ A a4(1.0, 1); // well-formed, but dangling reference
271
+ A a5(1.0, std::move(n)); // OK
272
+ ```
273
+
274
+ — *end example*]
275
+ — *end note*]
276
+ - Otherwise, the initialization is ill-formed.
277
+ - Otherwise (i.e., for the remaining copy-initialization cases),
278
+ user-defined conversions that can convert from the source type to
279
+ the destination type or (when a conversion function is used) to a
280
+ derived class thereof are enumerated as described in 
281
+ [[over.match.copy]], and the best one is chosen through overload
282
+ resolution [[over.match]]. If the conversion cannot be done or is
283
+ ambiguous, the initialization is ill-formed. The function selected
284
+ is called with the initializer expression as its argument; if the
285
+ function is a constructor, the call is a prvalue of the
286
+ cv-unqualified version of the destination type whose result object
287
+ is initialized by the constructor. The call is used to
288
+ direct-initialize, according to the rules above, the object that is
289
+ the destination of the copy-initialization.
290
+ - Otherwise, if the source type is a (possibly cv-qualified) class type,
291
+ conversion functions are considered. The applicable conversion
292
+ functions are enumerated [[over.match.conv]], and the best one is
293
+ chosen through overload resolution [[over.match]]. The user-defined
294
+ conversion so selected is called to convert the initializer expression
295
+ into the object being initialized. If the conversion cannot be done or
296
+ is ambiguous, the initialization is ill-formed.
297
+ - Otherwise, if the initialization is direct-initialization, the source
298
+ type is `std::nullptr_t`, and the destination type is `bool`, the
299
+ initial value of the object being initialized is `false`.
300
+ - Otherwise, the initial value of the object being initialized is the
301
+ (possibly converted) value of the initializer expression. A standard
302
+ conversion sequence [[conv]] will be used, if necessary, to convert
303
+ the initializer expression to the cv-unqualified version of the
304
+ destination type; no user-defined conversions are considered. If the
305
+ conversion cannot be done, the initialization is ill-formed. When
306
+ initializing a bit-field with a value that it cannot represent, the
307
+ resulting value of the bit-field is *implementation-defined*.
308
+ \[*Note 8*:
309
+ An expression of type “*cv1* `T`” can initialize an object of type
310
+ “*cv2* `T`” independently of the cv-qualifiers *cv1* and *cv2*.
311
+ ``` cpp
312
+ int a;
313
+ const int b = a;
314
+ int c = b;
315
+ ```
316
+
317
+ — *end note*]
318
+
319
+ An immediate invocation [[expr.const]] that is not evaluated where it
320
+ appears [[dcl.fct.default]], [[class.mem.general]] is evaluated and
321
+ checked for whether it is a constant expression at the point where the
322
+ enclosing *initializer* is used in a function call, a constructor
323
+ definition, or an aggregate initialization.
324
+
325
+ An *initializer-clause* followed by an ellipsis is a pack expansion
326
+ [[temp.variadic]].
327
+
328
+ Initialization includes the evaluation of all subexpressions of each
329
+ *initializer-clause* of the initializer (possibly nested within
330
+ *braced-init-list*s) and the creation of any temporary objects for
331
+ function arguments or return values [[class.temporary]].
332
+
333
+ If the initializer is a parenthesized *expression-list*, the expressions
334
+ are evaluated in the order specified for function calls [[expr.call]].
335
+
336
+ The same *identifier* shall not appear in multiple *designator*s of a
337
+ *designated-initializer-list*.
338
+
339
+ An object whose initialization has completed is deemed to be
340
+ constructed, even if the object is of non-class type or no constructor
341
+ of the object’s class is invoked for the initialization.
342
+
343
+ [*Note 9*: Such an object might have been value-initialized or
344
+ initialized by aggregate initialization [[dcl.init.aggr]] or by an
345
+ inherited constructor [[class.inhctor.init]]. — *end note*]
346
+
347
+ Destroying an object of class type invokes the destructor of the class.
348
+ Destroying a scalar type has no effect other than ending the lifetime of
349
+ the object [[basic.life]]. Destroying an array destroys each element in
350
+ reverse subscript order.
351
+
352
+ A declaration that specifies the initialization of a variable, whether
353
+ from an explicit initializer or by default-initialization, is called the
354
+ *initializing declaration* of that variable.
355
+
356
+ [*Note 10*: In most cases this is the defining declaration
357
+ [[basic.def]] of the variable, but the initializing declaration of a
358
+ non-inline static data member [[class.static.data]] can be the
359
+ declaration within the class definition and not the definition (if any)
360
+ outside it. — *end note*]
361
+