- tmp/tmp3vcc6u4h/{from.md → to.md} +289 -93
tmp/tmp3vcc6u4h/{from.md → to.md}
RENAMED
|
@@ -8,12 +8,14 @@ An object of class type (or array thereof) can be explicitly
|
|
| 8 |
initialized; see [[class.expl.init]] and [[class.base.init]].
|
| 9 |
|
| 10 |
When an array of class objects is initialized (either explicitly or
|
| 11 |
implicitly) and the elements are initialized by constructor, the
|
| 12 |
constructor shall be called for each element of the array, following the
|
| 13 |
-
subscript order; see [[dcl.array]].
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
### Explicit initialization <a id="class.expl.init">[[class.expl.init]]</a>
|
| 17 |
|
| 18 |
An object of class type can be initialized with a parenthesized
|
| 19 |
*expression-list*, where the *expression-list* is construed as an
|
|
@@ -21,43 +23,42 @@ argument list for a constructor that is called to initialize the object.
|
|
| 21 |
Alternatively, a single *assignment-expression* can be specified as an
|
| 22 |
*initializer* using the `=` form of initialization. Either
|
| 23 |
direct-initialization semantics or copy-initialization semantics apply;
|
| 24 |
see [[dcl.init]].
|
| 25 |
|
|
|
|
|
|
|
| 26 |
``` cpp
|
| 27 |
struct complex {
|
| 28 |
complex();
|
| 29 |
complex(double);
|
| 30 |
complex(double,double);
|
| 31 |
};
|
| 32 |
|
| 33 |
complex sqrt(complex,complex);
|
| 34 |
|
| 35 |
-
complex a(1); // initialize by a call of
|
| 36 |
-
// complex(double)
|
| 37 |
complex b = a; // initialize by a copy of a
|
| 38 |
-
complex c = complex(1,2); // construct complex(1,2)
|
| 39 |
-
// using complex(double,double)
|
| 40 |
// copy/move it into c
|
| 41 |
-
complex d = sqrt(b,c); // call sqrt(complex,complex)
|
| 42 |
-
|
| 43 |
-
complex
|
| 44 |
-
|
| 45 |
-
complex f = 3; // construct complex(3) using
|
| 46 |
-
// complex(double)
|
| 47 |
-
// copy/move it into f
|
| 48 |
-
complex g = { 1, 2 }; // initialize by a call of
|
| 49 |
-
// complex(double, double)
|
| 50 |
```
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
An object of class type can also be initialized by a *braced-init-list*.
|
| 56 |
List-initialization semantics apply; see [[dcl.init]] and
|
| 57 |
[[dcl.init.list]].
|
| 58 |
|
|
|
|
|
|
|
| 59 |
``` cpp
|
| 60 |
complex v[6] = { 1, complex(1,2), complex(), 2 };
|
| 61 |
```
|
| 62 |
|
| 63 |
Here, `complex::complex(double)` is called for the initialization of
|
|
@@ -73,37 +74,41 @@ struct X {
|
|
| 73 |
} x = { 99, 88.8, 77.7 };
|
| 74 |
```
|
| 75 |
|
| 76 |
Here, `x.i` is initialized with 99, `x.f` is initialized with 88.8, and
|
| 77 |
`complex::complex(double)` is called for the initialization of `x.c`.
|
| 78 |
-
Braces can be elided in the *initializer-list* for any aggregate, even
|
| 79 |
-
if the aggregate has members of a class type with user-defined type
|
| 80 |
-
conversions; see [[dcl.init.aggr]].
|
| 81 |
|
| 82 |
-
|
| 83 |
-
an object of type `T` (or array thereof) is ill-formed if no
|
| 84 |
-
*initializer* is explicitly specified (see [[class.init]] and
|
| 85 |
-
[[dcl.init]]).
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
### Initializing bases and members <a id="class.base.init">[[class.base.init]]</a>
|
| 91 |
|
| 92 |
In the definition of a constructor for a class, initializers for direct
|
| 93 |
-
and virtual base subobjects and non-static data members can be
|
| 94 |
-
by a *ctor-initializer*, which has the form
|
| 95 |
|
| 96 |
``` bnf
|
| 97 |
ctor-initializer:
|
| 98 |
':' mem-initializer-list
|
| 99 |
```
|
| 100 |
|
| 101 |
``` bnf
|
| 102 |
mem-initializer-list:
|
| 103 |
mem-initializer '...'ₒₚₜ
|
| 104 |
-
mem-initializer '...'ₒₚₜ
|
| 105 |
```
|
| 106 |
|
| 107 |
``` bnf
|
| 108 |
mem-initializer:
|
| 109 |
mem-initializer-id '(' expression-listₒₚₜ ')'
|
|
@@ -117,123 +122,163 @@ mem-initializer-id:
|
|
| 117 |
```
|
| 118 |
|
| 119 |
In a *mem-initializer-id* an initial unqualified *identifier* is looked
|
| 120 |
up in the scope of the constructor’s class and, if not found in that
|
| 121 |
scope, it is looked up in the scope containing the constructor’s
|
| 122 |
-
definition.
|
|
|
|
|
|
|
| 123 |
name as a direct or virtual base class of the class, a
|
| 124 |
*mem-initializer-id* naming the member or base class and composed of a
|
| 125 |
single identifier refers to the class member. A *mem-initializer-id* for
|
| 126 |
-
the hidden base class may be specified using a qualified
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
|
| 131 |
A *mem-initializer-list* can initialize a base class using any
|
| 132 |
*class-or-decltype* that denotes that base class type.
|
| 133 |
|
|
|
|
|
|
|
| 134 |
``` cpp
|
| 135 |
struct A { A(); };
|
| 136 |
typedef A global_A;
|
| 137 |
struct B { };
|
| 138 |
struct C: public A, public B { C(); };
|
| 139 |
C::C(): global_A() { } // mem-initializer for base A
|
| 140 |
```
|
| 141 |
|
|
|
|
|
|
|
| 142 |
If a *mem-initializer-id* is ambiguous because it designates both a
|
| 143 |
direct non-virtual base class and an inherited virtual base class, the
|
| 144 |
*mem-initializer* is ill-formed.
|
| 145 |
|
|
|
|
|
|
|
| 146 |
``` cpp
|
| 147 |
struct A { A(); };
|
| 148 |
struct B: public virtual A { };
|
| 149 |
struct C: public A, public B { C(); };
|
| 150 |
C::C(): A() { } // ill-formed: which A?
|
| 151 |
```
|
| 152 |
|
|
|
|
|
|
|
| 153 |
A *ctor-initializer* may initialize a variant member of the
|
| 154 |
constructor’s class. If a *ctor-initializer* specifies more than one
|
| 155 |
*mem-initializer* for the same member or for the same base class, the
|
| 156 |
*ctor-initializer* is ill-formed.
|
| 157 |
|
| 158 |
A *mem-initializer-list* can delegate to another constructor of the
|
| 159 |
constructor’s class using any *class-or-decltype* that denotes the
|
| 160 |
-
constructor’s class itself. If a designates the
|
| 161 |
-
shall be the only
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
|
| 171 |
``` cpp
|
| 172 |
struct C {
|
| 173 |
C( int ) { } // #1: non-delegating constructor
|
| 174 |
C(): C(42) { } // #2: delegates to #1
|
| 175 |
C( char c ) : C(42.0) { } // #3: ill-formed due to recursion with #4
|
| 176 |
C( double d ) : C('a') { } // #4: ill-formed due to recursion with #3
|
| 177 |
};
|
| 178 |
```
|
| 179 |
|
|
|
|
|
|
|
| 180 |
The *expression-list* or *braced-init-list* in a *mem-initializer* is
|
| 181 |
used to initialize the designated subobject (or, in the case of a
|
| 182 |
delegating constructor, the complete class object) according to the
|
| 183 |
initialization rules of [[dcl.init]] for direct-initialization.
|
| 184 |
|
|
|
|
|
|
|
| 185 |
``` cpp
|
| 186 |
-
struct B1 { B1(int);
|
| 187 |
-
struct B2 { B2(int);
|
| 188 |
struct D : B1, B2 {
|
| 189 |
D(int);
|
| 190 |
B1 b;
|
| 191 |
const int c;
|
| 192 |
};
|
| 193 |
|
| 194 |
-
D::D(int a) : B2(a+1), B1(a+2), c(a+3), b(a+4)
|
| 195 |
-
{ /* ... */ }
|
| 196 |
D d(10);
|
| 197 |
```
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
In a non-delegating constructor, if a given potentially constructed
|
| 207 |
subobject is not designated by a *mem-initializer-id* (including the
|
| 208 |
case where there is no *mem-initializer-list* because the constructor
|
| 209 |
has no *ctor-initializer*), then
|
| 210 |
|
| 211 |
-
- if the entity is a non-static data member that has a
|
| 212 |
-
|
| 213 |
- the constructor’s class is a union ([[class.union]]), and no other
|
| 214 |
variant member of that union is designated by a *mem-initializer-id*
|
| 215 |
or
|
| 216 |
- the constructor’s class is not a union, and, if the entity is a
|
| 217 |
member of an anonymous union, no other member of that union is
|
| 218 |
designated by a *mem-initializer-id*,
|
| 219 |
|
| 220 |
-
the entity is initialized
|
|
|
|
| 221 |
- otherwise, if the entity is an anonymous union or a variant member (
|
| 222 |
-
[[class.union]]), no initialization is performed;
|
| 223 |
- otherwise, the entity is default-initialized ([[dcl.init]]).
|
| 224 |
|
| 225 |
-
An abstract class ([[class.abstract]]) is never a most
|
| 226 |
-
thus its constructors never initialize virtual base
|
| 227 |
-
the corresponding *mem-initializer*s may be
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
constructor was not invoked as part of value-initialization and a member
|
| 232 |
of `X` is neither initialized nor given a value during execution of the
|
| 233 |
*compound-statement* of the body of the constructor, the member has an
|
| 234 |
-
indeterminate value.
|
|
|
|
|
|
|
| 235 |
|
| 236 |
``` cpp
|
| 237 |
struct A {
|
| 238 |
A();
|
| 239 |
};
|
|
@@ -249,32 +294,59 @@ struct C {
|
|
| 249 |
int i; // OK: i has indeterminate value
|
| 250 |
int j = 5; // OK: j has the value 5
|
| 251 |
};
|
| 252 |
```
|
| 253 |
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
``` cpp
|
| 260 |
struct A {
|
| 261 |
int i = /* some integer expression with side effects */ ;
|
| 262 |
A(int arg) : i(arg) { }
|
| 263 |
// ...
|
| 264 |
};
|
| 265 |
```
|
| 266 |
|
| 267 |
the `A(int)` constructor will simply initialize `i` to the value of
|
| 268 |
-
`arg`, and the side effects in `i`’s
|
| 269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
In a non-delegating constructor, the destructor for each potentially
|
| 272 |
constructed subobject of class type is potentially invoked (
|
| 273 |
-
[[class.dtor]]).
|
| 274 |
-
|
| 275 |
-
[
|
|
|
|
|
|
|
| 276 |
|
| 277 |
In a non-delegating constructor, initialization proceeds in the
|
| 278 |
following order:
|
| 279 |
|
| 280 |
- First, and only for the constructor of the most derived class (
|
|
@@ -283,18 +355,21 @@ following order:
|
|
| 283 |
acyclic graph of base classes, where “left-to-right” is the order of
|
| 284 |
appearance of the base classes in the derived class
|
| 285 |
*base-specifier-list*.
|
| 286 |
- Then, direct base classes are initialized in declaration order as they
|
| 287 |
appear in the *base-specifier-list* (regardless of the order of the
|
| 288 |
-
*mem-
|
| 289 |
- Then, non-static data members are initialized in the order they were
|
| 290 |
declared in the class definition (again regardless of the order of the
|
| 291 |
-
*mem-
|
| 292 |
- Finally, the *compound-statement* of the constructor body is executed.
|
| 293 |
|
| 294 |
-
The declaration order is mandated to ensure that base and
|
| 295 |
-
subobjects are destroyed in the reverse order of
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
``` cpp
|
| 298 |
struct V {
|
| 299 |
V();
|
| 300 |
V(int);
|
|
@@ -313,24 +388,28 @@ struct B : virtual V {
|
|
| 313 |
struct C : A, B, virtual V {
|
| 314 |
C();
|
| 315 |
C(int);
|
| 316 |
};
|
| 317 |
|
| 318 |
-
A::A(int i) : V(i) {
|
| 319 |
-
B::B(int i) {
|
| 320 |
-
C::C(int i) {
|
| 321 |
|
| 322 |
V v(1); // use V(int)
|
| 323 |
A a(2); // use V(int)
|
| 324 |
B b(3); // use V()
|
| 325 |
C c(4); // use V()
|
| 326 |
```
|
| 327 |
|
|
|
|
|
|
|
| 328 |
Names in the *expression-list* or *braced-init-list* of a
|
| 329 |
*mem-initializer* are evaluated in the scope of the constructor for
|
| 330 |
which the *mem-initializer* is specified.
|
| 331 |
|
|
|
|
|
|
|
| 332 |
``` cpp
|
| 333 |
class X {
|
| 334 |
int a;
|
| 335 |
int b;
|
| 336 |
int i;
|
|
@@ -343,22 +422,28 @@ public:
|
|
| 343 |
|
| 344 |
initializes `X::r` to refer to `X::a`, initializes `X::b` with the value
|
| 345 |
of the constructor parameter `i`, initializes `X::i` with the value of
|
| 346 |
the constructor parameter `i`, and initializes `X::j` with the value of
|
| 347 |
`X::i`; this takes place each time an object of class `X` is created.
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
|
| 352 |
Member functions (including virtual member functions, [[class.virtual]])
|
| 353 |
can be called for an object under construction. Similarly, an object
|
| 354 |
under construction can be the operand of the `typeid` operator (
|
| 355 |
[[expr.typeid]]) or of a `dynamic_cast` ([[expr.dynamic.cast]]).
|
| 356 |
However, if these operations are performed in a *ctor-initializer* (or
|
| 357 |
in a function called directly or indirectly from a *ctor-initializer*)
|
| 358 |
before all the *mem-initializer*s for base classes have completed, the
|
| 359 |
-
|
|
|
|
|
|
|
| 360 |
|
| 361 |
``` cpp
|
| 362 |
class A {
|
| 363 |
public:
|
| 364 |
A(int);
|
|
@@ -366,12 +451,11 @@ public:
|
|
| 366 |
|
| 367 |
class B : public A {
|
| 368 |
int j;
|
| 369 |
public:
|
| 370 |
int f();
|
| 371 |
-
B() : A(f()), // undefined: calls member function
|
| 372 |
-
// but base A not yet initialized
|
| 373 |
j(f()) { } // well-defined: bases are all initialized
|
| 374 |
};
|
| 375 |
|
| 376 |
class C {
|
| 377 |
public:
|
|
@@ -379,28 +463,140 @@ public:
|
|
| 379 |
};
|
| 380 |
|
| 381 |
class D : public B, C {
|
| 382 |
int i;
|
| 383 |
public:
|
| 384 |
-
D() : C(f()), // undefined: calls member function
|
| 385 |
-
// but base C not yet initialized
|
| 386 |
i(f()) { } // well-defined: bases are all initialized
|
| 387 |
};
|
| 388 |
```
|
| 389 |
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
construction
|
|
|
|
|
|
|
| 394 |
|
| 395 |
A *mem-initializer* followed by an ellipsis is a pack expansion (
|
| 396 |
[[temp.variadic]]) that initializes the base classes specified by a pack
|
| 397 |
expansion in the *base-specifier-list* for the class.
|
| 398 |
|
|
|
|
|
|
|
| 399 |
``` cpp
|
| 400 |
template<class... Mixins>
|
| 401 |
class X : public Mixins... {
|
| 402 |
public:
|
| 403 |
X(const Mixins&... mixins) : Mixins(mixins)... { }
|
| 404 |
};
|
| 405 |
```
|
| 406 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
initialized; see [[class.expl.init]] and [[class.base.init]].
|
| 9 |
|
| 10 |
When an array of class objects is initialized (either explicitly or
|
| 11 |
implicitly) and the elements are initialized by constructor, the
|
| 12 |
constructor shall be called for each element of the array, following the
|
| 13 |
+
subscript order; see [[dcl.array]].
|
| 14 |
+
|
| 15 |
+
[*Note 1*: Destructors for the array elements are called in reverse
|
| 16 |
+
order of their construction. — *end note*]
|
| 17 |
|
| 18 |
### Explicit initialization <a id="class.expl.init">[[class.expl.init]]</a>
|
| 19 |
|
| 20 |
An object of class type can be initialized with a parenthesized
|
| 21 |
*expression-list*, where the *expression-list* is construed as an
|
|
|
|
| 23 |
Alternatively, a single *assignment-expression* can be specified as an
|
| 24 |
*initializer* using the `=` form of initialization. Either
|
| 25 |
direct-initialization semantics or copy-initialization semantics apply;
|
| 26 |
see [[dcl.init]].
|
| 27 |
|
| 28 |
+
[*Example 1*:
|
| 29 |
+
|
| 30 |
``` cpp
|
| 31 |
struct complex {
|
| 32 |
complex();
|
| 33 |
complex(double);
|
| 34 |
complex(double,double);
|
| 35 |
};
|
| 36 |
|
| 37 |
complex sqrt(complex,complex);
|
| 38 |
|
| 39 |
+
complex a(1); // initialize by a call of complex(double)
|
|
|
|
| 40 |
complex b = a; // initialize by a copy of a
|
| 41 |
+
complex c = complex(1,2); // construct complex(1,2) using complex(double,double),
|
|
|
|
| 42 |
// copy/move it into c
|
| 43 |
+
complex d = sqrt(b,c); // call sqrt(complex,complex) and copy/move the result into d
|
| 44 |
+
complex e; // initialize by a call of complex()
|
| 45 |
+
complex f = 3; // construct complex(3) using complex(double), copy/move it into f
|
| 46 |
+
complex g = { 1, 2 }; // initialize by a call of complex(double, double)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
```
|
| 48 |
|
| 49 |
+
— *end example*]
|
| 50 |
+
|
| 51 |
+
[*Note 1*: Overloading of the assignment operator ([[over.ass]]) has
|
| 52 |
+
no effect on initialization. — *end note*]
|
| 53 |
|
| 54 |
An object of class type can also be initialized by a *braced-init-list*.
|
| 55 |
List-initialization semantics apply; see [[dcl.init]] and
|
| 56 |
[[dcl.init.list]].
|
| 57 |
|
| 58 |
+
[*Example 2*:
|
| 59 |
+
|
| 60 |
``` cpp
|
| 61 |
complex v[6] = { 1, complex(1,2), complex(), 2 };
|
| 62 |
```
|
| 63 |
|
| 64 |
Here, `complex::complex(double)` is called for the initialization of
|
|
|
|
| 74 |
} x = { 99, 88.8, 77.7 };
|
| 75 |
```
|
| 76 |
|
| 77 |
Here, `x.i` is initialized with 99, `x.f` is initialized with 88.8, and
|
| 78 |
`complex::complex(double)` is called for the initialization of `x.c`.
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
[*Note 2*: Braces can be elided in the *initializer-list* for any
|
| 83 |
+
aggregate, even if the aggregate has members of a class type with
|
| 84 |
+
user-defined type conversions; see [[dcl.init.aggr]]. — *end note*]
|
| 85 |
+
|
| 86 |
+
[*Note 3*: If `T` is a class type with no default constructor, any
|
| 87 |
+
declaration of an object of type `T` (or array thereof) is ill-formed if
|
| 88 |
+
no *initializer* is explicitly specified (see [[class.init]] and
|
| 89 |
+
[[dcl.init]]). — *end note*]
|
| 90 |
+
|
| 91 |
+
[*Note 4*: The order in which objects with static or thread storage
|
| 92 |
+
duration are initialized is described in [[basic.start.dynamic]] and
|
| 93 |
+
[[stmt.dcl]]. — *end note*]
|
| 94 |
|
| 95 |
### Initializing bases and members <a id="class.base.init">[[class.base.init]]</a>
|
| 96 |
|
| 97 |
In the definition of a constructor for a class, initializers for direct
|
| 98 |
+
and virtual base class subobjects and non-static data members can be
|
| 99 |
+
specified by a *ctor-initializer*, which has the form
|
| 100 |
|
| 101 |
``` bnf
|
| 102 |
ctor-initializer:
|
| 103 |
':' mem-initializer-list
|
| 104 |
```
|
| 105 |
|
| 106 |
``` bnf
|
| 107 |
mem-initializer-list:
|
| 108 |
mem-initializer '...'ₒₚₜ
|
| 109 |
+
mem-initializer-list ',' mem-initializer '...'ₒₚₜ
|
| 110 |
```
|
| 111 |
|
| 112 |
``` bnf
|
| 113 |
mem-initializer:
|
| 114 |
mem-initializer-id '(' expression-listₒₚₜ ')'
|
|
|
|
| 122 |
```
|
| 123 |
|
| 124 |
In a *mem-initializer-id* an initial unqualified *identifier* is looked
|
| 125 |
up in the scope of the constructor’s class and, if not found in that
|
| 126 |
scope, it is looked up in the scope containing the constructor’s
|
| 127 |
+
definition.
|
| 128 |
+
|
| 129 |
+
[*Note 1*: If the constructor’s class contains a member with the same
|
| 130 |
name as a direct or virtual base class of the class, a
|
| 131 |
*mem-initializer-id* naming the member or base class and composed of a
|
| 132 |
single identifier refers to the class member. A *mem-initializer-id* for
|
| 133 |
+
the hidden base class may be specified using a qualified
|
| 134 |
+
name. — *end note*]
|
| 135 |
+
|
| 136 |
+
Unless the *mem-initializer-id* names the constructor’s class, a
|
| 137 |
+
non-static data member of the constructor’s class, or a direct or
|
| 138 |
+
virtual base of that class, the *mem-initializer* is ill-formed.
|
| 139 |
|
| 140 |
A *mem-initializer-list* can initialize a base class using any
|
| 141 |
*class-or-decltype* that denotes that base class type.
|
| 142 |
|
| 143 |
+
[*Example 1*:
|
| 144 |
+
|
| 145 |
``` cpp
|
| 146 |
struct A { A(); };
|
| 147 |
typedef A global_A;
|
| 148 |
struct B { };
|
| 149 |
struct C: public A, public B { C(); };
|
| 150 |
C::C(): global_A() { } // mem-initializer for base A
|
| 151 |
```
|
| 152 |
|
| 153 |
+
— *end example*]
|
| 154 |
+
|
| 155 |
If a *mem-initializer-id* is ambiguous because it designates both a
|
| 156 |
direct non-virtual base class and an inherited virtual base class, the
|
| 157 |
*mem-initializer* is ill-formed.
|
| 158 |
|
| 159 |
+
[*Example 2*:
|
| 160 |
+
|
| 161 |
``` cpp
|
| 162 |
struct A { A(); };
|
| 163 |
struct B: public virtual A { };
|
| 164 |
struct C: public A, public B { C(); };
|
| 165 |
C::C(): A() { } // ill-formed: which A?
|
| 166 |
```
|
| 167 |
|
| 168 |
+
— *end example*]
|
| 169 |
+
|
| 170 |
A *ctor-initializer* may initialize a variant member of the
|
| 171 |
constructor’s class. If a *ctor-initializer* specifies more than one
|
| 172 |
*mem-initializer* for the same member or for the same base class, the
|
| 173 |
*ctor-initializer* is ill-formed.
|
| 174 |
|
| 175 |
A *mem-initializer-list* can delegate to another constructor of the
|
| 176 |
constructor’s class using any *class-or-decltype* that denotes the
|
| 177 |
+
constructor’s class itself. If a *mem-initializer-id* designates the
|
| 178 |
+
constructor’s class, it shall be the only *mem-initializer*; the
|
| 179 |
+
constructor is a *delegating constructor*, and the constructor selected
|
| 180 |
+
by the *mem-initializer* is the *target constructor*. The target
|
| 181 |
+
constructor is selected by overload resolution. Once the target
|
| 182 |
+
constructor returns, the body of the delegating constructor is executed.
|
| 183 |
+
If a constructor delegates to itself directly or indirectly, the program
|
| 184 |
+
is ill-formed, no diagnostic required.
|
| 185 |
+
|
| 186 |
+
[*Example 3*:
|
| 187 |
|
| 188 |
``` cpp
|
| 189 |
struct C {
|
| 190 |
C( int ) { } // #1: non-delegating constructor
|
| 191 |
C(): C(42) { } // #2: delegates to #1
|
| 192 |
C( char c ) : C(42.0) { } // #3: ill-formed due to recursion with #4
|
| 193 |
C( double d ) : C('a') { } // #4: ill-formed due to recursion with #3
|
| 194 |
};
|
| 195 |
```
|
| 196 |
|
| 197 |
+
— *end example*]
|
| 198 |
+
|
| 199 |
The *expression-list* or *braced-init-list* in a *mem-initializer* is
|
| 200 |
used to initialize the designated subobject (or, in the case of a
|
| 201 |
delegating constructor, the complete class object) according to the
|
| 202 |
initialization rules of [[dcl.init]] for direct-initialization.
|
| 203 |
|
| 204 |
+
[*Example 4*:
|
| 205 |
+
|
| 206 |
``` cpp
|
| 207 |
+
struct B1 { B1(int); ... };
|
| 208 |
+
struct B2 { B2(int); ... };
|
| 209 |
struct D : B1, B2 {
|
| 210 |
D(int);
|
| 211 |
B1 b;
|
| 212 |
const int c;
|
| 213 |
};
|
| 214 |
|
| 215 |
+
D::D(int a) : B2(a+1), B1(a+2), c(a+3), b(a+4) { ... }
|
|
|
|
| 216 |
D d(10);
|
| 217 |
```
|
| 218 |
|
| 219 |
+
— *end example*]
|
| 220 |
+
|
| 221 |
+
[*Note 2*: The initialization performed by each *mem-initializer*
|
| 222 |
+
constitutes a full-expression ([[intro.execution]]). Any expression in
|
| 223 |
+
a *mem-initializer* is evaluated as part of the full-expression that
|
| 224 |
+
performs the initialization. — *end note*]
|
| 225 |
+
|
| 226 |
+
A *mem-initializer* where the *mem-initializer-id* denotes a virtual
|
| 227 |
+
base class is ignored during execution of a constructor of any class
|
| 228 |
+
that is not the most derived class.
|
| 229 |
+
|
| 230 |
+
A temporary expression bound to a reference member in a
|
| 231 |
+
*mem-initializer* is ill-formed.
|
| 232 |
+
|
| 233 |
+
[*Example 5*:
|
| 234 |
+
|
| 235 |
+
``` cpp
|
| 236 |
+
struct A {
|
| 237 |
+
A() : v(42) { } // error
|
| 238 |
+
const int& v;
|
| 239 |
+
};
|
| 240 |
+
```
|
| 241 |
+
|
| 242 |
+
— *end example*]
|
| 243 |
|
| 244 |
In a non-delegating constructor, if a given potentially constructed
|
| 245 |
subobject is not designated by a *mem-initializer-id* (including the
|
| 246 |
case where there is no *mem-initializer-list* because the constructor
|
| 247 |
has no *ctor-initializer*), then
|
| 248 |
|
| 249 |
+
- if the entity is a non-static data member that has a default member
|
| 250 |
+
initializer ([[class.mem]]) and either
|
| 251 |
- the constructor’s class is a union ([[class.union]]), and no other
|
| 252 |
variant member of that union is designated by a *mem-initializer-id*
|
| 253 |
or
|
| 254 |
- the constructor’s class is not a union, and, if the entity is a
|
| 255 |
member of an anonymous union, no other member of that union is
|
| 256 |
designated by a *mem-initializer-id*,
|
| 257 |
|
| 258 |
+
the entity is initialized from its default member initializer as
|
| 259 |
+
specified in [[dcl.init]];
|
| 260 |
- otherwise, if the entity is an anonymous union or a variant member (
|
| 261 |
+
[[class.union.anon]]), no initialization is performed;
|
| 262 |
- otherwise, the entity is default-initialized ([[dcl.init]]).
|
| 263 |
|
| 264 |
+
[*Note 3*: An abstract class ([[class.abstract]]) is never a most
|
| 265 |
+
derived class, thus its constructors never initialize virtual base
|
| 266 |
+
classes, therefore the corresponding *mem-initializer*s may be
|
| 267 |
+
omitted. — *end note*]
|
| 268 |
+
|
| 269 |
+
An attempt to initialize more than one non-static data member of a union
|
| 270 |
+
renders the program ill-formed.
|
| 271 |
+
|
| 272 |
+
[*Note 4*: After the call to a constructor for class `X` for an object
|
| 273 |
+
with automatic or dynamic storage duration has completed, if the
|
| 274 |
constructor was not invoked as part of value-initialization and a member
|
| 275 |
of `X` is neither initialized nor given a value during execution of the
|
| 276 |
*compound-statement* of the body of the constructor, the member has an
|
| 277 |
+
indeterminate value. — *end note*]
|
| 278 |
+
|
| 279 |
+
[*Example 6*:
|
| 280 |
|
| 281 |
``` cpp
|
| 282 |
struct A {
|
| 283 |
A();
|
| 284 |
};
|
|
|
|
| 294 |
int i; // OK: i has indeterminate value
|
| 295 |
int j = 5; // OK: j has the value 5
|
| 296 |
};
|
| 297 |
```
|
| 298 |
|
| 299 |
+
— *end example*]
|
| 300 |
+
|
| 301 |
+
If a given non-static data member has both a default member initializer
|
| 302 |
+
and a *mem-initializer*, the initialization specified by the
|
| 303 |
+
*mem-initializer* is performed, and the non-static data member’s default
|
| 304 |
+
member initializer is ignored.
|
| 305 |
+
|
| 306 |
+
[*Example 7*:
|
| 307 |
+
|
| 308 |
+
Given
|
| 309 |
|
| 310 |
``` cpp
|
| 311 |
struct A {
|
| 312 |
int i = /* some integer expression with side effects */ ;
|
| 313 |
A(int arg) : i(arg) { }
|
| 314 |
// ...
|
| 315 |
};
|
| 316 |
```
|
| 317 |
|
| 318 |
the `A(int)` constructor will simply initialize `i` to the value of
|
| 319 |
+
`arg`, and the side effects in `i`’s default member initializer will not
|
| 320 |
+
take place.
|
| 321 |
+
|
| 322 |
+
— *end example*]
|
| 323 |
+
|
| 324 |
+
A temporary expression bound to a reference member from a default member
|
| 325 |
+
initializer is ill-formed.
|
| 326 |
+
|
| 327 |
+
[*Example 8*:
|
| 328 |
+
|
| 329 |
+
``` cpp
|
| 330 |
+
struct A {
|
| 331 |
+
A() = default; // OK
|
| 332 |
+
A(int v) : v(v) { } // OK
|
| 333 |
+
const int& v = 42; // OK
|
| 334 |
+
};
|
| 335 |
+
A a1; // error: ill-formed binding of temporary to reference
|
| 336 |
+
A a2(1); // OK, unfortunately
|
| 337 |
+
```
|
| 338 |
+
|
| 339 |
+
— *end example*]
|
| 340 |
|
| 341 |
In a non-delegating constructor, the destructor for each potentially
|
| 342 |
constructed subobject of class type is potentially invoked (
|
| 343 |
+
[[class.dtor]]).
|
| 344 |
+
|
| 345 |
+
[*Note 5*: This provision ensures that destructors can be called for
|
| 346 |
+
fully-constructed subobjects in case an exception is thrown (
|
| 347 |
+
[[except.ctor]]). — *end note*]
|
| 348 |
|
| 349 |
In a non-delegating constructor, initialization proceeds in the
|
| 350 |
following order:
|
| 351 |
|
| 352 |
- First, and only for the constructor of the most derived class (
|
|
|
|
| 355 |
acyclic graph of base classes, where “left-to-right” is the order of
|
| 356 |
appearance of the base classes in the derived class
|
| 357 |
*base-specifier-list*.
|
| 358 |
- Then, direct base classes are initialized in declaration order as they
|
| 359 |
appear in the *base-specifier-list* (regardless of the order of the
|
| 360 |
+
*mem-initializer*s).
|
| 361 |
- Then, non-static data members are initialized in the order they were
|
| 362 |
declared in the class definition (again regardless of the order of the
|
| 363 |
+
*mem-initializer*s).
|
| 364 |
- Finally, the *compound-statement* of the constructor body is executed.
|
| 365 |
|
| 366 |
+
[*Note 6*: The declaration order is mandated to ensure that base and
|
| 367 |
+
member subobjects are destroyed in the reverse order of
|
| 368 |
+
initialization. — *end note*]
|
| 369 |
+
|
| 370 |
+
[*Example 9*:
|
| 371 |
|
| 372 |
``` cpp
|
| 373 |
struct V {
|
| 374 |
V();
|
| 375 |
V(int);
|
|
|
|
| 388 |
struct C : A, B, virtual V {
|
| 389 |
C();
|
| 390 |
C(int);
|
| 391 |
};
|
| 392 |
|
| 393 |
+
A::A(int i) : V(i) { ... }
|
| 394 |
+
B::B(int i) { ... }
|
| 395 |
+
C::C(int i) { ... }
|
| 396 |
|
| 397 |
V v(1); // use V(int)
|
| 398 |
A a(2); // use V(int)
|
| 399 |
B b(3); // use V()
|
| 400 |
C c(4); // use V()
|
| 401 |
```
|
| 402 |
|
| 403 |
+
— *end example*]
|
| 404 |
+
|
| 405 |
Names in the *expression-list* or *braced-init-list* of a
|
| 406 |
*mem-initializer* are evaluated in the scope of the constructor for
|
| 407 |
which the *mem-initializer* is specified.
|
| 408 |
|
| 409 |
+
[*Example 10*:
|
| 410 |
+
|
| 411 |
``` cpp
|
| 412 |
class X {
|
| 413 |
int a;
|
| 414 |
int b;
|
| 415 |
int i;
|
|
|
|
| 422 |
|
| 423 |
initializes `X::r` to refer to `X::a`, initializes `X::b` with the value
|
| 424 |
of the constructor parameter `i`, initializes `X::i` with the value of
|
| 425 |
the constructor parameter `i`, and initializes `X::j` with the value of
|
| 426 |
`X::i`; this takes place each time an object of class `X` is created.
|
| 427 |
+
|
| 428 |
+
— *end example*]
|
| 429 |
+
|
| 430 |
+
[*Note 7*: Because the *mem-initializer* are evaluated in the scope of
|
| 431 |
+
the constructor, the `this` pointer can be used in the *expression-list*
|
| 432 |
+
of a *mem-initializer* to refer to the object being
|
| 433 |
+
initialized. — *end note*]
|
| 434 |
|
| 435 |
Member functions (including virtual member functions, [[class.virtual]])
|
| 436 |
can be called for an object under construction. Similarly, an object
|
| 437 |
under construction can be the operand of the `typeid` operator (
|
| 438 |
[[expr.typeid]]) or of a `dynamic_cast` ([[expr.dynamic.cast]]).
|
| 439 |
However, if these operations are performed in a *ctor-initializer* (or
|
| 440 |
in a function called directly or indirectly from a *ctor-initializer*)
|
| 441 |
before all the *mem-initializer*s for base classes have completed, the
|
| 442 |
+
program has undefined behavior.
|
| 443 |
+
|
| 444 |
+
[*Example 11*:
|
| 445 |
|
| 446 |
``` cpp
|
| 447 |
class A {
|
| 448 |
public:
|
| 449 |
A(int);
|
|
|
|
| 451 |
|
| 452 |
class B : public A {
|
| 453 |
int j;
|
| 454 |
public:
|
| 455 |
int f();
|
| 456 |
+
B() : A(f()), // undefined: calls member function but base A not yet initialized
|
|
|
|
| 457 |
j(f()) { } // well-defined: bases are all initialized
|
| 458 |
};
|
| 459 |
|
| 460 |
class C {
|
| 461 |
public:
|
|
|
|
| 463 |
};
|
| 464 |
|
| 465 |
class D : public B, C {
|
| 466 |
int i;
|
| 467 |
public:
|
| 468 |
+
D() : C(f()), // undefined: calls member function but base C not yet initialized
|
|
|
|
| 469 |
i(f()) { } // well-defined: bases are all initialized
|
| 470 |
};
|
| 471 |
```
|
| 472 |
|
| 473 |
+
— *end example*]
|
| 474 |
+
|
| 475 |
+
[*Note 8*: [[class.cdtor]] describes the result of virtual function
|
| 476 |
+
calls, `typeid` and `dynamic_cast`s during construction for the
|
| 477 |
+
well-defined cases; that is, describes the *polymorphic behavior* of an
|
| 478 |
+
object under construction. — *end note*]
|
| 479 |
|
| 480 |
A *mem-initializer* followed by an ellipsis is a pack expansion (
|
| 481 |
[[temp.variadic]]) that initializes the base classes specified by a pack
|
| 482 |
expansion in the *base-specifier-list* for the class.
|
| 483 |
|
| 484 |
+
[*Example 12*:
|
| 485 |
+
|
| 486 |
``` cpp
|
| 487 |
template<class... Mixins>
|
| 488 |
class X : public Mixins... {
|
| 489 |
public:
|
| 490 |
X(const Mixins&... mixins) : Mixins(mixins)... { }
|
| 491 |
};
|
| 492 |
```
|
| 493 |
|
| 494 |
+
— *end example*]
|
| 495 |
+
|
| 496 |
+
### Initialization by inherited constructor <a id="class.inhctor.init">[[class.inhctor.init]]</a>
|
| 497 |
+
|
| 498 |
+
When a constructor for type `B` is invoked to initialize an object of a
|
| 499 |
+
different type `D` (that is, when the constructor was inherited (
|
| 500 |
+
[[namespace.udecl]])), initialization proceeds as if a defaulted default
|
| 501 |
+
constructor were used to initialize the `D` object and each base class
|
| 502 |
+
subobject from which the constructor was inherited, except that the `B`
|
| 503 |
+
subobject is initialized by the invocation of the inherited constructor.
|
| 504 |
+
The complete initialization is considered to be a single function call;
|
| 505 |
+
in particular, the initialization of the inherited constructor’s
|
| 506 |
+
parameters is sequenced before the initialization of any part of the `D`
|
| 507 |
+
object.
|
| 508 |
+
|
| 509 |
+
[*Example 1*:
|
| 510 |
+
|
| 511 |
+
``` cpp
|
| 512 |
+
struct B1 {
|
| 513 |
+
B1(int, ...) { }
|
| 514 |
+
};
|
| 515 |
+
|
| 516 |
+
struct B2 {
|
| 517 |
+
B2(double) { }
|
| 518 |
+
};
|
| 519 |
+
|
| 520 |
+
int get();
|
| 521 |
+
|
| 522 |
+
struct D1 : B1 {
|
| 523 |
+
using B1::B1; // inherits B1(int, ...)
|
| 524 |
+
int x;
|
| 525 |
+
int y = get();
|
| 526 |
+
};
|
| 527 |
+
|
| 528 |
+
void test() {
|
| 529 |
+
D1 d(2, 3, 4); // OK: B1 is initialized by calling B1(2, 3, 4),
|
| 530 |
+
// then d.x is default-initialized (no initialization is performed),
|
| 531 |
+
// then d.y is initialized by calling get()
|
| 532 |
+
D1 e; // error: D1 has a deleted default constructor
|
| 533 |
+
}
|
| 534 |
+
|
| 535 |
+
struct D2 : B2 {
|
| 536 |
+
using B2::B2;
|
| 537 |
+
B1 b;
|
| 538 |
+
};
|
| 539 |
+
|
| 540 |
+
D2 f(1.0); // error: B1 has a deleted default constructor
|
| 541 |
+
|
| 542 |
+
struct W { W(int); };
|
| 543 |
+
struct X : virtual W { using W::W; X() = delete; };
|
| 544 |
+
struct Y : X { using X::X; };
|
| 545 |
+
struct Z : Y, virtual W { using Y::Y; };
|
| 546 |
+
Z z(0); // OK: initialization of Y does not invoke default constructor of X
|
| 547 |
+
|
| 548 |
+
template<class T> struct Log : T {
|
| 549 |
+
using T::T; // inherits all constructors from class T
|
| 550 |
+
~Log() { std::clog << "Destroying wrapper" << std::endl; }
|
| 551 |
+
};
|
| 552 |
+
```
|
| 553 |
+
|
| 554 |
+
Class template `Log` wraps any class and forwards all of its
|
| 555 |
+
constructors, while writing a message to the standard log whenever an
|
| 556 |
+
object of class `Log` is destroyed.
|
| 557 |
+
|
| 558 |
+
— *end example*]
|
| 559 |
+
|
| 560 |
+
If the constructor was inherited from multiple base class subobjects of
|
| 561 |
+
type `B`, the program is ill-formed.
|
| 562 |
+
|
| 563 |
+
[*Example 2*:
|
| 564 |
+
|
| 565 |
+
``` cpp
|
| 566 |
+
struct A { A(int); };
|
| 567 |
+
struct B : A { using A::A; };
|
| 568 |
+
|
| 569 |
+
struct C1 : B { using B::B; };
|
| 570 |
+
struct C2 : B { using B::B; };
|
| 571 |
+
|
| 572 |
+
struct D1 : C1, C2 {
|
| 573 |
+
using C1::C1;
|
| 574 |
+
using C2::C2;
|
| 575 |
+
};
|
| 576 |
+
|
| 577 |
+
struct V1 : virtual B { using B::B; };
|
| 578 |
+
struct V2 : virtual B { using B::B; };
|
| 579 |
+
|
| 580 |
+
struct D2 : V1, V2 {
|
| 581 |
+
using V1::V1;
|
| 582 |
+
using V2::V2;
|
| 583 |
+
};
|
| 584 |
+
|
| 585 |
+
D1 d1(0); // ill-formed: ambiguous
|
| 586 |
+
D2 d2(0); // OK: initializes virtual B base class, which initializes the A base class
|
| 587 |
+
// then initializes the V1 and V2 base classes as if by a defaulted default constructor
|
| 588 |
+
|
| 589 |
+
struct M { M(); M(int); };
|
| 590 |
+
struct N : M { using M::M; };
|
| 591 |
+
struct O : M {};
|
| 592 |
+
struct P : N, O { using N::N; using O::O; };
|
| 593 |
+
P p(0); // OK: use M(0) to initialize N's base class,
|
| 594 |
+
// use M() to initialize O's base class
|
| 595 |
+
```
|
| 596 |
+
|
| 597 |
+
— *end example*]
|
| 598 |
+
|
| 599 |
+
When an object is initialized by an inherited constructor,
|
| 600 |
+
initialization of the object is complete when the initialization of all
|
| 601 |
+
subobjects is complete.
|
| 602 |
+
|