From Jason Turner

[class.inhctor.init]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpk2vxdrim/{from.md → to.md} +107 -0
tmp/tmpk2vxdrim/{from.md → to.md} RENAMED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Initialization by inherited constructor <a id="class.inhctor.init">[[class.inhctor.init]]</a>
2
+
3
+ When a constructor for type `B` is invoked to initialize an object of a
4
+ different type `D` (that is, when the constructor was inherited (
5
+ [[namespace.udecl]])), initialization proceeds as if a defaulted default
6
+ constructor were used to initialize the `D` object and each base class
7
+ subobject from which the constructor was inherited, except that the `B`
8
+ subobject is initialized by the invocation of the inherited constructor.
9
+ The complete initialization is considered to be a single function call;
10
+ in particular, the initialization of the inherited constructor’s
11
+ parameters is sequenced before the initialization of any part of the `D`
12
+ object.
13
+
14
+ [*Example 1*:
15
+
16
+ ``` cpp
17
+ struct B1 {
18
+ B1(int, ...) { }
19
+ };
20
+
21
+ struct B2 {
22
+ B2(double) { }
23
+ };
24
+
25
+ int get();
26
+
27
+ struct D1 : B1 {
28
+ using B1::B1; // inherits B1(int, ...)
29
+ int x;
30
+ int y = get();
31
+ };
32
+
33
+ void test() {
34
+ D1 d(2, 3, 4); // OK: B1 is initialized by calling B1(2, 3, 4),
35
+ // then d.x is default-initialized (no initialization is performed),
36
+ // then d.y is initialized by calling get()
37
+ D1 e; // error: D1 has a deleted default constructor
38
+ }
39
+
40
+ struct D2 : B2 {
41
+ using B2::B2;
42
+ B1 b;
43
+ };
44
+
45
+ D2 f(1.0); // error: B1 has a deleted default constructor
46
+
47
+ struct W { W(int); };
48
+ struct X : virtual W { using W::W; X() = delete; };
49
+ struct Y : X { using X::X; };
50
+ struct Z : Y, virtual W { using Y::Y; };
51
+ Z z(0); // OK: initialization of Y does not invoke default constructor of X
52
+
53
+ template<class T> struct Log : T {
54
+ using T::T; // inherits all constructors from class T
55
+ ~Log() { std::clog << "Destroying wrapper" << std::endl; }
56
+ };
57
+ ```
58
+
59
+ Class template `Log` wraps any class and forwards all of its
60
+ constructors, while writing a message to the standard log whenever an
61
+ object of class `Log` is destroyed.
62
+
63
+ — *end example*]
64
+
65
+ If the constructor was inherited from multiple base class subobjects of
66
+ type `B`, the program is ill-formed.
67
+
68
+ [*Example 2*:
69
+
70
+ ``` cpp
71
+ struct A { A(int); };
72
+ struct B : A { using A::A; };
73
+
74
+ struct C1 : B { using B::B; };
75
+ struct C2 : B { using B::B; };
76
+
77
+ struct D1 : C1, C2 {
78
+ using C1::C1;
79
+ using C2::C2;
80
+ };
81
+
82
+ struct V1 : virtual B { using B::B; };
83
+ struct V2 : virtual B { using B::B; };
84
+
85
+ struct D2 : V1, V2 {
86
+ using V1::V1;
87
+ using V2::V2;
88
+ };
89
+
90
+ D1 d1(0); // ill-formed: ambiguous
91
+ D2 d2(0); // OK: initializes virtual B base class, which initializes the A base class
92
+ // then initializes the V1 and V2 base classes as if by a defaulted default constructor
93
+
94
+ struct M { M(); M(int); };
95
+ struct N : M { using M::M; };
96
+ struct O : M {};
97
+ struct P : N, O { using N::N; using O::O; };
98
+ P p(0); // OK: use M(0) to initialize N's base class,
99
+ // use M() to initialize O's base class
100
+ ```
101
+
102
+ — *end example*]
103
+
104
+ When an object is initialized by an inherited constructor,
105
+ initialization of the object is complete when the initialization of all
106
+ subobjects is complete.
107
+