- tmp/tmpwucwoqum/{from.md → to.md} +151 -67
tmp/tmpwucwoqum/{from.md → to.md}
RENAMED
|
@@ -1,20 +1,20 @@
|
|
| 1 |
### Initializing bases and members <a id="class.base.init">[[class.base.init]]</a>
|
| 2 |
|
| 3 |
In the definition of a constructor for a class, initializers for direct
|
| 4 |
-
and virtual base subobjects and non-static data members can be
|
| 5 |
-
by a *ctor-initializer*, which has the form
|
| 6 |
|
| 7 |
``` bnf
|
| 8 |
ctor-initializer:
|
| 9 |
':' mem-initializer-list
|
| 10 |
```
|
| 11 |
|
| 12 |
``` bnf
|
| 13 |
mem-initializer-list:
|
| 14 |
mem-initializer '...'ₒₚₜ
|
| 15 |
-
mem-initializer '...'ₒₚₜ
|
| 16 |
```
|
| 17 |
|
| 18 |
``` bnf
|
| 19 |
mem-initializer:
|
| 20 |
mem-initializer-id '(' expression-listₒₚₜ ')'
|
|
@@ -28,123 +28,163 @@ mem-initializer-id:
|
|
| 28 |
```
|
| 29 |
|
| 30 |
In a *mem-initializer-id* an initial unqualified *identifier* is looked
|
| 31 |
up in the scope of the constructor’s class and, if not found in that
|
| 32 |
scope, it is looked up in the scope containing the constructor’s
|
| 33 |
-
definition.
|
|
|
|
|
|
|
| 34 |
name as a direct or virtual base class of the class, a
|
| 35 |
*mem-initializer-id* naming the member or base class and composed of a
|
| 36 |
single identifier refers to the class member. A *mem-initializer-id* for
|
| 37 |
-
the hidden base class may be specified using a qualified
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
A *mem-initializer-list* can initialize a base class using any
|
| 43 |
*class-or-decltype* that denotes that base class type.
|
| 44 |
|
|
|
|
|
|
|
| 45 |
``` cpp
|
| 46 |
struct A { A(); };
|
| 47 |
typedef A global_A;
|
| 48 |
struct B { };
|
| 49 |
struct C: public A, public B { C(); };
|
| 50 |
C::C(): global_A() { } // mem-initializer for base A
|
| 51 |
```
|
| 52 |
|
|
|
|
|
|
|
| 53 |
If a *mem-initializer-id* is ambiguous because it designates both a
|
| 54 |
direct non-virtual base class and an inherited virtual base class, the
|
| 55 |
*mem-initializer* is ill-formed.
|
| 56 |
|
|
|
|
|
|
|
| 57 |
``` cpp
|
| 58 |
struct A { A(); };
|
| 59 |
struct B: public virtual A { };
|
| 60 |
struct C: public A, public B { C(); };
|
| 61 |
C::C(): A() { } // ill-formed: which A?
|
| 62 |
```
|
| 63 |
|
|
|
|
|
|
|
| 64 |
A *ctor-initializer* may initialize a variant member of the
|
| 65 |
constructor’s class. If a *ctor-initializer* specifies more than one
|
| 66 |
*mem-initializer* for the same member or for the same base class, the
|
| 67 |
*ctor-initializer* is ill-formed.
|
| 68 |
|
| 69 |
A *mem-initializer-list* can delegate to another constructor of the
|
| 70 |
constructor’s class using any *class-or-decltype* that denotes the
|
| 71 |
-
constructor’s class itself. If a designates the
|
| 72 |
-
shall be the only
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
``` cpp
|
| 83 |
struct C {
|
| 84 |
C( int ) { } // #1: non-delegating constructor
|
| 85 |
C(): C(42) { } // #2: delegates to #1
|
| 86 |
C( char c ) : C(42.0) { } // #3: ill-formed due to recursion with #4
|
| 87 |
C( double d ) : C('a') { } // #4: ill-formed due to recursion with #3
|
| 88 |
};
|
| 89 |
```
|
| 90 |
|
|
|
|
|
|
|
| 91 |
The *expression-list* or *braced-init-list* in a *mem-initializer* is
|
| 92 |
used to initialize the designated subobject (or, in the case of a
|
| 93 |
delegating constructor, the complete class object) according to the
|
| 94 |
initialization rules of [[dcl.init]] for direct-initialization.
|
| 95 |
|
|
|
|
|
|
|
| 96 |
``` cpp
|
| 97 |
-
struct B1 { B1(int);
|
| 98 |
-
struct B2 { B2(int);
|
| 99 |
struct D : B1, B2 {
|
| 100 |
D(int);
|
| 101 |
B1 b;
|
| 102 |
const int c;
|
| 103 |
};
|
| 104 |
|
| 105 |
-
D::D(int a) : B2(a+1), B1(a+2), c(a+3), b(a+4)
|
| 106 |
-
{ /* ... */ }
|
| 107 |
D d(10);
|
| 108 |
```
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
In a non-delegating constructor, if a given potentially constructed
|
| 118 |
subobject is not designated by a *mem-initializer-id* (including the
|
| 119 |
case where there is no *mem-initializer-list* because the constructor
|
| 120 |
has no *ctor-initializer*), then
|
| 121 |
|
| 122 |
-
- if the entity is a non-static data member that has a
|
| 123 |
-
|
| 124 |
- the constructor’s class is a union ([[class.union]]), and no other
|
| 125 |
variant member of that union is designated by a *mem-initializer-id*
|
| 126 |
or
|
| 127 |
- the constructor’s class is not a union, and, if the entity is a
|
| 128 |
member of an anonymous union, no other member of that union is
|
| 129 |
designated by a *mem-initializer-id*,
|
| 130 |
|
| 131 |
-
the entity is initialized
|
|
|
|
| 132 |
- otherwise, if the entity is an anonymous union or a variant member (
|
| 133 |
-
[[class.union]]), no initialization is performed;
|
| 134 |
- otherwise, the entity is default-initialized ([[dcl.init]]).
|
| 135 |
|
| 136 |
-
An abstract class ([[class.abstract]]) is never a most
|
| 137 |
-
thus its constructors never initialize virtual base
|
| 138 |
-
the corresponding *mem-initializer*s may be
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
constructor was not invoked as part of value-initialization and a member
|
| 143 |
of `X` is neither initialized nor given a value during execution of the
|
| 144 |
*compound-statement* of the body of the constructor, the member has an
|
| 145 |
-
indeterminate value.
|
|
|
|
|
|
|
| 146 |
|
| 147 |
``` cpp
|
| 148 |
struct A {
|
| 149 |
A();
|
| 150 |
};
|
|
@@ -160,32 +200,59 @@ struct C {
|
|
| 160 |
int i; // OK: i has indeterminate value
|
| 161 |
int j = 5; // OK: j has the value 5
|
| 162 |
};
|
| 163 |
```
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
``` cpp
|
| 171 |
struct A {
|
| 172 |
int i = /* some integer expression with side effects */ ;
|
| 173 |
A(int arg) : i(arg) { }
|
| 174 |
// ...
|
| 175 |
};
|
| 176 |
```
|
| 177 |
|
| 178 |
the `A(int)` constructor will simply initialize `i` to the value of
|
| 179 |
-
`arg`, and the side effects in `i`’s
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
In a non-delegating constructor, the destructor for each potentially
|
| 183 |
constructed subobject of class type is potentially invoked (
|
| 184 |
-
[[class.dtor]]).
|
| 185 |
-
|
| 186 |
-
[
|
|
|
|
|
|
|
| 187 |
|
| 188 |
In a non-delegating constructor, initialization proceeds in the
|
| 189 |
following order:
|
| 190 |
|
| 191 |
- First, and only for the constructor of the most derived class (
|
|
@@ -194,18 +261,21 @@ following order:
|
|
| 194 |
acyclic graph of base classes, where “left-to-right” is the order of
|
| 195 |
appearance of the base classes in the derived class
|
| 196 |
*base-specifier-list*.
|
| 197 |
- Then, direct base classes are initialized in declaration order as they
|
| 198 |
appear in the *base-specifier-list* (regardless of the order of the
|
| 199 |
-
*mem-
|
| 200 |
- Then, non-static data members are initialized in the order they were
|
| 201 |
declared in the class definition (again regardless of the order of the
|
| 202 |
-
*mem-
|
| 203 |
- Finally, the *compound-statement* of the constructor body is executed.
|
| 204 |
|
| 205 |
-
The declaration order is mandated to ensure that base and
|
| 206 |
-
subobjects are destroyed in the reverse order of
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
``` cpp
|
| 209 |
struct V {
|
| 210 |
V();
|
| 211 |
V(int);
|
|
@@ -224,24 +294,28 @@ struct B : virtual V {
|
|
| 224 |
struct C : A, B, virtual V {
|
| 225 |
C();
|
| 226 |
C(int);
|
| 227 |
};
|
| 228 |
|
| 229 |
-
A::A(int i) : V(i) {
|
| 230 |
-
B::B(int i) {
|
| 231 |
-
C::C(int i) {
|
| 232 |
|
| 233 |
V v(1); // use V(int)
|
| 234 |
A a(2); // use V(int)
|
| 235 |
B b(3); // use V()
|
| 236 |
C c(4); // use V()
|
| 237 |
```
|
| 238 |
|
|
|
|
|
|
|
| 239 |
Names in the *expression-list* or *braced-init-list* of a
|
| 240 |
*mem-initializer* are evaluated in the scope of the constructor for
|
| 241 |
which the *mem-initializer* is specified.
|
| 242 |
|
|
|
|
|
|
|
| 243 |
``` cpp
|
| 244 |
class X {
|
| 245 |
int a;
|
| 246 |
int b;
|
| 247 |
int i;
|
|
@@ -254,22 +328,28 @@ public:
|
|
| 254 |
|
| 255 |
initializes `X::r` to refer to `X::a`, initializes `X::b` with the value
|
| 256 |
of the constructor parameter `i`, initializes `X::i` with the value of
|
| 257 |
the constructor parameter `i`, and initializes `X::j` with the value of
|
| 258 |
`X::i`; this takes place each time an object of class `X` is created.
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
Member functions (including virtual member functions, [[class.virtual]])
|
| 264 |
can be called for an object under construction. Similarly, an object
|
| 265 |
under construction can be the operand of the `typeid` operator (
|
| 266 |
[[expr.typeid]]) or of a `dynamic_cast` ([[expr.dynamic.cast]]).
|
| 267 |
However, if these operations are performed in a *ctor-initializer* (or
|
| 268 |
in a function called directly or indirectly from a *ctor-initializer*)
|
| 269 |
before all the *mem-initializer*s for base classes have completed, the
|
| 270 |
-
|
|
|
|
|
|
|
| 271 |
|
| 272 |
``` cpp
|
| 273 |
class A {
|
| 274 |
public:
|
| 275 |
A(int);
|
|
@@ -277,12 +357,11 @@ public:
|
|
| 277 |
|
| 278 |
class B : public A {
|
| 279 |
int j;
|
| 280 |
public:
|
| 281 |
int f();
|
| 282 |
-
B() : A(f()), // undefined: calls member function
|
| 283 |
-
// but base A not yet initialized
|
| 284 |
j(f()) { } // well-defined: bases are all initialized
|
| 285 |
};
|
| 286 |
|
| 287 |
class C {
|
| 288 |
public:
|
|
@@ -290,28 +369,33 @@ public:
|
|
| 290 |
};
|
| 291 |
|
| 292 |
class D : public B, C {
|
| 293 |
int i;
|
| 294 |
public:
|
| 295 |
-
D() : C(f()), // undefined: calls member function
|
| 296 |
-
// but base C not yet initialized
|
| 297 |
i(f()) { } // well-defined: bases are all initialized
|
| 298 |
};
|
| 299 |
```
|
| 300 |
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
construction
|
|
|
|
|
|
|
| 305 |
|
| 306 |
A *mem-initializer* followed by an ellipsis is a pack expansion (
|
| 307 |
[[temp.variadic]]) that initializes the base classes specified by a pack
|
| 308 |
expansion in the *base-specifier-list* for the class.
|
| 309 |
|
|
|
|
|
|
|
| 310 |
``` cpp
|
| 311 |
template<class... Mixins>
|
| 312 |
class X : public Mixins... {
|
| 313 |
public:
|
| 314 |
X(const Mixins&... mixins) : Mixins(mixins)... { }
|
| 315 |
};
|
| 316 |
```
|
| 317 |
|
|
|
|
|
|
|
|
|
| 1 |
### Initializing bases and members <a id="class.base.init">[[class.base.init]]</a>
|
| 2 |
|
| 3 |
In the definition of a constructor for a class, initializers for direct
|
| 4 |
+
and virtual base class subobjects and non-static data members can be
|
| 5 |
+
specified by a *ctor-initializer*, which has the form
|
| 6 |
|
| 7 |
``` bnf
|
| 8 |
ctor-initializer:
|
| 9 |
':' mem-initializer-list
|
| 10 |
```
|
| 11 |
|
| 12 |
``` bnf
|
| 13 |
mem-initializer-list:
|
| 14 |
mem-initializer '...'ₒₚₜ
|
| 15 |
+
mem-initializer-list ',' mem-initializer '...'ₒₚₜ
|
| 16 |
```
|
| 17 |
|
| 18 |
``` bnf
|
| 19 |
mem-initializer:
|
| 20 |
mem-initializer-id '(' expression-listₒₚₜ ')'
|
|
|
|
| 28 |
```
|
| 29 |
|
| 30 |
In a *mem-initializer-id* an initial unqualified *identifier* is looked
|
| 31 |
up in the scope of the constructor’s class and, if not found in that
|
| 32 |
scope, it is looked up in the scope containing the constructor’s
|
| 33 |
+
definition.
|
| 34 |
+
|
| 35 |
+
[*Note 1*: If the constructor’s class contains a member with the same
|
| 36 |
name as a direct or virtual base class of the class, a
|
| 37 |
*mem-initializer-id* naming the member or base class and composed of a
|
| 38 |
single identifier refers to the class member. A *mem-initializer-id* for
|
| 39 |
+
the hidden base class may be specified using a qualified
|
| 40 |
+
name. — *end note*]
|
| 41 |
+
|
| 42 |
+
Unless the *mem-initializer-id* names the constructor’s class, a
|
| 43 |
+
non-static data member of the constructor’s class, or a direct or
|
| 44 |
+
virtual base of that class, the *mem-initializer* is ill-formed.
|
| 45 |
|
| 46 |
A *mem-initializer-list* can initialize a base class using any
|
| 47 |
*class-or-decltype* that denotes that base class type.
|
| 48 |
|
| 49 |
+
[*Example 1*:
|
| 50 |
+
|
| 51 |
``` cpp
|
| 52 |
struct A { A(); };
|
| 53 |
typedef A global_A;
|
| 54 |
struct B { };
|
| 55 |
struct C: public A, public B { C(); };
|
| 56 |
C::C(): global_A() { } // mem-initializer for base A
|
| 57 |
```
|
| 58 |
|
| 59 |
+
— *end example*]
|
| 60 |
+
|
| 61 |
If a *mem-initializer-id* is ambiguous because it designates both a
|
| 62 |
direct non-virtual base class and an inherited virtual base class, the
|
| 63 |
*mem-initializer* is ill-formed.
|
| 64 |
|
| 65 |
+
[*Example 2*:
|
| 66 |
+
|
| 67 |
``` cpp
|
| 68 |
struct A { A(); };
|
| 69 |
struct B: public virtual A { };
|
| 70 |
struct C: public A, public B { C(); };
|
| 71 |
C::C(): A() { } // ill-formed: which A?
|
| 72 |
```
|
| 73 |
|
| 74 |
+
— *end example*]
|
| 75 |
+
|
| 76 |
A *ctor-initializer* may initialize a variant member of the
|
| 77 |
constructor’s class. If a *ctor-initializer* specifies more than one
|
| 78 |
*mem-initializer* for the same member or for the same base class, the
|
| 79 |
*ctor-initializer* is ill-formed.
|
| 80 |
|
| 81 |
A *mem-initializer-list* can delegate to another constructor of the
|
| 82 |
constructor’s class using any *class-or-decltype* that denotes the
|
| 83 |
+
constructor’s class itself. If a *mem-initializer-id* designates the
|
| 84 |
+
constructor’s class, it shall be the only *mem-initializer*; the
|
| 85 |
+
constructor is a *delegating constructor*, and the constructor selected
|
| 86 |
+
by the *mem-initializer* is the *target constructor*. The target
|
| 87 |
+
constructor is selected by overload resolution. Once the target
|
| 88 |
+
constructor returns, the body of the delegating constructor is executed.
|
| 89 |
+
If a constructor delegates to itself directly or indirectly, the program
|
| 90 |
+
is ill-formed, no diagnostic required.
|
| 91 |
+
|
| 92 |
+
[*Example 3*:
|
| 93 |
|
| 94 |
``` cpp
|
| 95 |
struct C {
|
| 96 |
C( int ) { } // #1: non-delegating constructor
|
| 97 |
C(): C(42) { } // #2: delegates to #1
|
| 98 |
C( char c ) : C(42.0) { } // #3: ill-formed due to recursion with #4
|
| 99 |
C( double d ) : C('a') { } // #4: ill-formed due to recursion with #3
|
| 100 |
};
|
| 101 |
```
|
| 102 |
|
| 103 |
+
— *end example*]
|
| 104 |
+
|
| 105 |
The *expression-list* or *braced-init-list* in a *mem-initializer* is
|
| 106 |
used to initialize the designated subobject (or, in the case of a
|
| 107 |
delegating constructor, the complete class object) according to the
|
| 108 |
initialization rules of [[dcl.init]] for direct-initialization.
|
| 109 |
|
| 110 |
+
[*Example 4*:
|
| 111 |
+
|
| 112 |
``` cpp
|
| 113 |
+
struct B1 { B1(int); ... };
|
| 114 |
+
struct B2 { B2(int); ... };
|
| 115 |
struct D : B1, B2 {
|
| 116 |
D(int);
|
| 117 |
B1 b;
|
| 118 |
const int c;
|
| 119 |
};
|
| 120 |
|
| 121 |
+
D::D(int a) : B2(a+1), B1(a+2), c(a+3), b(a+4) { ... }
|
|
|
|
| 122 |
D d(10);
|
| 123 |
```
|
| 124 |
|
| 125 |
+
— *end example*]
|
| 126 |
+
|
| 127 |
+
[*Note 2*: The initialization performed by each *mem-initializer*
|
| 128 |
+
constitutes a full-expression ([[intro.execution]]). Any expression in
|
| 129 |
+
a *mem-initializer* is evaluated as part of the full-expression that
|
| 130 |
+
performs the initialization. — *end note*]
|
| 131 |
+
|
| 132 |
+
A *mem-initializer* where the *mem-initializer-id* denotes a virtual
|
| 133 |
+
base class is ignored during execution of a constructor of any class
|
| 134 |
+
that is not the most derived class.
|
| 135 |
+
|
| 136 |
+
A temporary expression bound to a reference member in a
|
| 137 |
+
*mem-initializer* is ill-formed.
|
| 138 |
+
|
| 139 |
+
[*Example 5*:
|
| 140 |
+
|
| 141 |
+
``` cpp
|
| 142 |
+
struct A {
|
| 143 |
+
A() : v(42) { } // error
|
| 144 |
+
const int& v;
|
| 145 |
+
};
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
— *end example*]
|
| 149 |
|
| 150 |
In a non-delegating constructor, if a given potentially constructed
|
| 151 |
subobject is not designated by a *mem-initializer-id* (including the
|
| 152 |
case where there is no *mem-initializer-list* because the constructor
|
| 153 |
has no *ctor-initializer*), then
|
| 154 |
|
| 155 |
+
- if the entity is a non-static data member that has a default member
|
| 156 |
+
initializer ([[class.mem]]) and either
|
| 157 |
- the constructor’s class is a union ([[class.union]]), and no other
|
| 158 |
variant member of that union is designated by a *mem-initializer-id*
|
| 159 |
or
|
| 160 |
- the constructor’s class is not a union, and, if the entity is a
|
| 161 |
member of an anonymous union, no other member of that union is
|
| 162 |
designated by a *mem-initializer-id*,
|
| 163 |
|
| 164 |
+
the entity is initialized from its default member initializer as
|
| 165 |
+
specified in [[dcl.init]];
|
| 166 |
- otherwise, if the entity is an anonymous union or a variant member (
|
| 167 |
+
[[class.union.anon]]), no initialization is performed;
|
| 168 |
- otherwise, the entity is default-initialized ([[dcl.init]]).
|
| 169 |
|
| 170 |
+
[*Note 3*: An abstract class ([[class.abstract]]) is never a most
|
| 171 |
+
derived class, thus its constructors never initialize virtual base
|
| 172 |
+
classes, therefore the corresponding *mem-initializer*s may be
|
| 173 |
+
omitted. — *end note*]
|
| 174 |
+
|
| 175 |
+
An attempt to initialize more than one non-static data member of a union
|
| 176 |
+
renders the program ill-formed.
|
| 177 |
+
|
| 178 |
+
[*Note 4*: After the call to a constructor for class `X` for an object
|
| 179 |
+
with automatic or dynamic storage duration has completed, if the
|
| 180 |
constructor was not invoked as part of value-initialization and a member
|
| 181 |
of `X` is neither initialized nor given a value during execution of the
|
| 182 |
*compound-statement* of the body of the constructor, the member has an
|
| 183 |
+
indeterminate value. — *end note*]
|
| 184 |
+
|
| 185 |
+
[*Example 6*:
|
| 186 |
|
| 187 |
``` cpp
|
| 188 |
struct A {
|
| 189 |
A();
|
| 190 |
};
|
|
|
|
| 200 |
int i; // OK: i has indeterminate value
|
| 201 |
int j = 5; // OK: j has the value 5
|
| 202 |
};
|
| 203 |
```
|
| 204 |
|
| 205 |
+
— *end example*]
|
| 206 |
+
|
| 207 |
+
If a given non-static data member has both a default member initializer
|
| 208 |
+
and a *mem-initializer*, the initialization specified by the
|
| 209 |
+
*mem-initializer* is performed, and the non-static data member’s default
|
| 210 |
+
member initializer is ignored.
|
| 211 |
+
|
| 212 |
+
[*Example 7*:
|
| 213 |
+
|
| 214 |
+
Given
|
| 215 |
|
| 216 |
``` cpp
|
| 217 |
struct A {
|
| 218 |
int i = /* some integer expression with side effects */ ;
|
| 219 |
A(int arg) : i(arg) { }
|
| 220 |
// ...
|
| 221 |
};
|
| 222 |
```
|
| 223 |
|
| 224 |
the `A(int)` constructor will simply initialize `i` to the value of
|
| 225 |
+
`arg`, and the side effects in `i`’s default member initializer will not
|
| 226 |
+
take place.
|
| 227 |
+
|
| 228 |
+
— *end example*]
|
| 229 |
+
|
| 230 |
+
A temporary expression bound to a reference member from a default member
|
| 231 |
+
initializer is ill-formed.
|
| 232 |
+
|
| 233 |
+
[*Example 8*:
|
| 234 |
+
|
| 235 |
+
``` cpp
|
| 236 |
+
struct A {
|
| 237 |
+
A() = default; // OK
|
| 238 |
+
A(int v) : v(v) { } // OK
|
| 239 |
+
const int& v = 42; // OK
|
| 240 |
+
};
|
| 241 |
+
A a1; // error: ill-formed binding of temporary to reference
|
| 242 |
+
A a2(1); // OK, unfortunately
|
| 243 |
+
```
|
| 244 |
+
|
| 245 |
+
— *end example*]
|
| 246 |
|
| 247 |
In a non-delegating constructor, the destructor for each potentially
|
| 248 |
constructed subobject of class type is potentially invoked (
|
| 249 |
+
[[class.dtor]]).
|
| 250 |
+
|
| 251 |
+
[*Note 5*: This provision ensures that destructors can be called for
|
| 252 |
+
fully-constructed subobjects in case an exception is thrown (
|
| 253 |
+
[[except.ctor]]). — *end note*]
|
| 254 |
|
| 255 |
In a non-delegating constructor, initialization proceeds in the
|
| 256 |
following order:
|
| 257 |
|
| 258 |
- First, and only for the constructor of the most derived class (
|
|
|
|
| 261 |
acyclic graph of base classes, where “left-to-right” is the order of
|
| 262 |
appearance of the base classes in the derived class
|
| 263 |
*base-specifier-list*.
|
| 264 |
- Then, direct base classes are initialized in declaration order as they
|
| 265 |
appear in the *base-specifier-list* (regardless of the order of the
|
| 266 |
+
*mem-initializer*s).
|
| 267 |
- Then, non-static data members are initialized in the order they were
|
| 268 |
declared in the class definition (again regardless of the order of the
|
| 269 |
+
*mem-initializer*s).
|
| 270 |
- Finally, the *compound-statement* of the constructor body is executed.
|
| 271 |
|
| 272 |
+
[*Note 6*: The declaration order is mandated to ensure that base and
|
| 273 |
+
member subobjects are destroyed in the reverse order of
|
| 274 |
+
initialization. — *end note*]
|
| 275 |
+
|
| 276 |
+
[*Example 9*:
|
| 277 |
|
| 278 |
``` cpp
|
| 279 |
struct V {
|
| 280 |
V();
|
| 281 |
V(int);
|
|
|
|
| 294 |
struct C : A, B, virtual V {
|
| 295 |
C();
|
| 296 |
C(int);
|
| 297 |
};
|
| 298 |
|
| 299 |
+
A::A(int i) : V(i) { ... }
|
| 300 |
+
B::B(int i) { ... }
|
| 301 |
+
C::C(int i) { ... }
|
| 302 |
|
| 303 |
V v(1); // use V(int)
|
| 304 |
A a(2); // use V(int)
|
| 305 |
B b(3); // use V()
|
| 306 |
C c(4); // use V()
|
| 307 |
```
|
| 308 |
|
| 309 |
+
— *end example*]
|
| 310 |
+
|
| 311 |
Names in the *expression-list* or *braced-init-list* of a
|
| 312 |
*mem-initializer* are evaluated in the scope of the constructor for
|
| 313 |
which the *mem-initializer* is specified.
|
| 314 |
|
| 315 |
+
[*Example 10*:
|
| 316 |
+
|
| 317 |
``` cpp
|
| 318 |
class X {
|
| 319 |
int a;
|
| 320 |
int b;
|
| 321 |
int i;
|
|
|
|
| 328 |
|
| 329 |
initializes `X::r` to refer to `X::a`, initializes `X::b` with the value
|
| 330 |
of the constructor parameter `i`, initializes `X::i` with the value of
|
| 331 |
the constructor parameter `i`, and initializes `X::j` with the value of
|
| 332 |
`X::i`; this takes place each time an object of class `X` is created.
|
| 333 |
+
|
| 334 |
+
— *end example*]
|
| 335 |
+
|
| 336 |
+
[*Note 7*: Because the *mem-initializer* are evaluated in the scope of
|
| 337 |
+
the constructor, the `this` pointer can be used in the *expression-list*
|
| 338 |
+
of a *mem-initializer* to refer to the object being
|
| 339 |
+
initialized. — *end note*]
|
| 340 |
|
| 341 |
Member functions (including virtual member functions, [[class.virtual]])
|
| 342 |
can be called for an object under construction. Similarly, an object
|
| 343 |
under construction can be the operand of the `typeid` operator (
|
| 344 |
[[expr.typeid]]) or of a `dynamic_cast` ([[expr.dynamic.cast]]).
|
| 345 |
However, if these operations are performed in a *ctor-initializer* (or
|
| 346 |
in a function called directly or indirectly from a *ctor-initializer*)
|
| 347 |
before all the *mem-initializer*s for base classes have completed, the
|
| 348 |
+
program has undefined behavior.
|
| 349 |
+
|
| 350 |
+
[*Example 11*:
|
| 351 |
|
| 352 |
``` cpp
|
| 353 |
class A {
|
| 354 |
public:
|
| 355 |
A(int);
|
|
|
|
| 357 |
|
| 358 |
class B : public A {
|
| 359 |
int j;
|
| 360 |
public:
|
| 361 |
int f();
|
| 362 |
+
B() : A(f()), // undefined: calls member function but base A not yet initialized
|
|
|
|
| 363 |
j(f()) { } // well-defined: bases are all initialized
|
| 364 |
};
|
| 365 |
|
| 366 |
class C {
|
| 367 |
public:
|
|
|
|
| 369 |
};
|
| 370 |
|
| 371 |
class D : public B, C {
|
| 372 |
int i;
|
| 373 |
public:
|
| 374 |
+
D() : C(f()), // undefined: calls member function but base C not yet initialized
|
|
|
|
| 375 |
i(f()) { } // well-defined: bases are all initialized
|
| 376 |
};
|
| 377 |
```
|
| 378 |
|
| 379 |
+
— *end example*]
|
| 380 |
+
|
| 381 |
+
[*Note 8*: [[class.cdtor]] describes the result of virtual function
|
| 382 |
+
calls, `typeid` and `dynamic_cast`s during construction for the
|
| 383 |
+
well-defined cases; that is, describes the *polymorphic behavior* of an
|
| 384 |
+
object under construction. — *end note*]
|
| 385 |
|
| 386 |
A *mem-initializer* followed by an ellipsis is a pack expansion (
|
| 387 |
[[temp.variadic]]) that initializes the base classes specified by a pack
|
| 388 |
expansion in the *base-specifier-list* for the class.
|
| 389 |
|
| 390 |
+
[*Example 12*:
|
| 391 |
+
|
| 392 |
``` cpp
|
| 393 |
template<class... Mixins>
|
| 394 |
class X : public Mixins... {
|
| 395 |
public:
|
| 396 |
X(const Mixins&... mixins) : Mixins(mixins)... { }
|
| 397 |
};
|
| 398 |
```
|
| 399 |
|
| 400 |
+
— *end example*]
|
| 401 |
+
|