From Jason Turner

[class.copy.ctor]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpbm4qj6u5/{from.md → to.md} +235 -0
tmp/tmpbm4qj6u5/{from.md → to.md} RENAMED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
2
+
3
+ A non-template constructor for class `X` is a copy constructor if its
4
+ first parameter is of type `X&`, `const X&`, `volatile X&` or
5
+ `const volatile X&`, and either there are no other parameters or else
6
+ all other parameters have default arguments ([[dcl.fct.default]]).
7
+
8
+ [*Example 1*:
9
+
10
+ `X::X(const X&)`
11
+
12
+ and `X::X(X&,int=1)` are copy constructors.
13
+
14
+ ``` cpp
15
+ struct X {
16
+ X(int);
17
+ X(const X&, int = 1);
18
+ };
19
+ X a(1); // calls X(int);
20
+ X b(a, 0); // calls X(const X&, int);
21
+ X c = b; // calls X(const X&, int);
22
+ ```
23
+
24
+ — *end example*]
25
+
26
+ A non-template constructor for class `X` is a move constructor if its
27
+ first parameter is of type `X&&`, `const X&&`, `volatile X&&`, or
28
+ `const volatile X&&`, and either there are no other parameters or else
29
+ all other parameters have default arguments ([[dcl.fct.default]]).
30
+
31
+ [*Example 2*:
32
+
33
+ `Y::Y(Y&&)` is a move constructor.
34
+
35
+ ``` cpp
36
+ struct Y {
37
+ Y(const Y&);
38
+ Y(Y&&);
39
+ };
40
+ extern Y f(int);
41
+ Y d(f(1)); // calls Y(Y&&)
42
+ Y e = d; // calls Y(const Y&)
43
+ ```
44
+
45
+ — *end example*]
46
+
47
+ [*Note 1*:
48
+
49
+ All forms of copy/move constructor may be declared for a class.
50
+
51
+ [*Example 3*:
52
+
53
+ ``` cpp
54
+ struct X {
55
+ X(const X&);
56
+ X(X&); // OK
57
+ X(X&&);
58
+ X(const X&&); // OK, but possibly not sensible
59
+ };
60
+ ```
61
+
62
+ — *end example*]
63
+
64
+ — *end note*]
65
+
66
+ [*Note 2*:
67
+
68
+ If a class `X` only has a copy constructor with a parameter of type
69
+ `X&`, an initializer of type `const` `X` or `volatile` `X` cannot
70
+ initialize an object of type (possibly cv-qualified) `X`.
71
+
72
+ [*Example 4*:
73
+
74
+ ``` cpp
75
+ struct X {
76
+ X(); // default constructor
77
+ X(X&); // copy constructor with a non-const parameter
78
+ };
79
+ const X cx;
80
+ X x = cx; // error: X::X(X&) cannot copy cx into x
81
+ ```
82
+
83
+ — *end example*]
84
+
85
+ — *end note*]
86
+
87
+ A declaration of a constructor for a class `X` is ill-formed if its
88
+ first parameter is of type (optionally cv-qualified) `X` and either
89
+ there are no other parameters or else all other parameters have default
90
+ arguments. A member function template is never instantiated to produce
91
+ such a constructor signature.
92
+
93
+ [*Example 5*:
94
+
95
+ ``` cpp
96
+ struct S {
97
+ template<typename T> S(T);
98
+ S();
99
+ };
100
+
101
+ S g;
102
+
103
+ void h() {
104
+ S a(g); // does not instantiate the member template to produce S::S<S>(S);
105
+ // uses the implicitly declared copy constructor
106
+ }
107
+ ```
108
+
109
+ — *end example*]
110
+
111
+ If the class definition does not explicitly declare a copy constructor,
112
+ a non-explicit one is declared *implicitly*. If the class definition
113
+ declares a move constructor or move assignment operator, the implicitly
114
+ declared copy constructor is defined as deleted; otherwise, it is
115
+ defined as defaulted ([[dcl.fct.def]]). The latter case is deprecated
116
+ if the class has a user-declared copy assignment operator or a
117
+ user-declared destructor.
118
+
119
+ The implicitly-declared copy constructor for a class `X` will have the
120
+ form
121
+
122
+ ``` cpp
123
+ X::X(const X&)
124
+ ```
125
+
126
+ if each potentially constructed subobject of a class type `M` (or array
127
+ thereof) has a copy constructor whose first parameter is of type `const`
128
+ `M&` or `const` `volatile` `M&`.[^4] Otherwise, the implicitly-declared
129
+ copy constructor will have the form
130
+
131
+ ``` cpp
132
+ X::X(X&)
133
+ ```
134
+
135
+ If the definition of a class `X` does not explicitly declare a move
136
+ constructor, a non-explicit one will be implicitly declared as defaulted
137
+ if and only if
138
+
139
+ - `X` does not have a user-declared copy constructor,
140
+ - `X` does not have a user-declared copy assignment operator,
141
+ - `X` does not have a user-declared move assignment operator, and
142
+ - `X` does not have a user-declared destructor.
143
+
144
+ [*Note 3*: When the move constructor is not implicitly declared or
145
+ explicitly supplied, expressions that otherwise would have invoked the
146
+ move constructor may instead invoke a copy constructor. — *end note*]
147
+
148
+ The implicitly-declared move constructor for class `X` will have the
149
+ form
150
+
151
+ ``` cpp
152
+ X::X(X&&)
153
+ ```
154
+
155
+ An implicitly-declared copy/move constructor is an `inline` `public`
156
+ member of its class. A defaulted copy/move constructor for a class `X`
157
+ is defined as deleted ([[dcl.fct.def.delete]]) if `X` has:
158
+
159
+ - a variant member with a non-trivial corresponding constructor and `X`
160
+ is a union-like class,
161
+ - a potentially constructed subobject type `M` (or array thereof) that
162
+ cannot be copied/moved because overload resolution ([[over.match]]),
163
+ as applied to find `M`’s corresponding constructor, results in an
164
+ ambiguity or a function that is deleted or inaccessible from the
165
+ defaulted constructor,
166
+ - any potentially constructed subobject of a type with a destructor that
167
+ is deleted or inaccessible from the defaulted constructor, or,
168
+ - for the copy constructor, a non-static data member of rvalue reference
169
+ type.
170
+
171
+ A defaulted move constructor that is defined as deleted is ignored by
172
+ overload resolution ([[over.match]], [[over.over]]).
173
+
174
+ [*Note 4*: A deleted move constructor would otherwise interfere with
175
+ initialization from an rvalue which can use the copy constructor
176
+ instead. — *end note*]
177
+
178
+ A copy/move constructor for class `X` is trivial if it is not
179
+ user-provided and if:
180
+
181
+ - class `X` has no virtual functions ([[class.virtual]]) and no virtual
182
+ base classes ([[class.mi]]), and
183
+ - the constructor selected to copy/move each direct base class subobject
184
+ is trivial, and
185
+ - for each non-static data member of `X` that is of class type (or array
186
+ thereof), the constructor selected to copy/move that member is
187
+ trivial;
188
+
189
+ otherwise the copy/move constructor is *non-trivial*.
190
+
191
+ A copy/move constructor that is defaulted and not defined as deleted is
192
+ *implicitly defined* if it is odr-used ([[basic.def.odr]]) or when it
193
+ is explicitly defaulted after its first declaration.
194
+
195
+ [*Note 5*: The copy/move constructor is implicitly defined even if the
196
+ implementation elided its odr-use ([[basic.def.odr]],
197
+ [[class.temporary]]). — *end note*]
198
+
199
+ If the implicitly-defined constructor would satisfy the requirements of
200
+ a constexpr constructor ([[dcl.constexpr]]), the implicitly-defined
201
+ constructor is `constexpr`.
202
+
203
+ Before the defaulted copy/move constructor for a class is implicitly
204
+ defined, all non-user-provided copy/move constructors for its
205
+ potentially constructed subobjects shall have been implicitly defined.
206
+
207
+ [*Note 6*: An implicitly-declared copy/move constructor has an implied
208
+ exception specification ([[except.spec]]). — *end note*]
209
+
210
+ The implicitly-defined copy/move constructor for a non-union class `X`
211
+ performs a memberwise copy/move of its bases and members.
212
+
213
+ [*Note 7*: Default member initializers of non-static data members are
214
+ ignored. See also the example in  [[class.base.init]]. — *end note*]
215
+
216
+ The order of initialization is the same as the order of initialization
217
+ of bases and members in a user-defined constructor (see 
218
+ [[class.base.init]]). Let `x` be either the parameter of the constructor
219
+ or, for the move constructor, an xvalue referring to the parameter. Each
220
+ base or non-static data member is copied/moved in the manner appropriate
221
+ to its type:
222
+
223
+ - if the member is an array, each element is direct-initialized with the
224
+ corresponding subobject of `x`;
225
+ - if a member `m` has rvalue reference type `T&&`, it is
226
+ direct-initialized with `static_cast<T&&>(x.m)`;
227
+ - otherwise, the base or member is direct-initialized with the
228
+ corresponding base or member of `x`.
229
+
230
+ Virtual base class subobjects shall be initialized only once by the
231
+ implicitly-defined copy/move constructor (see  [[class.base.init]]).
232
+
233
+ The implicitly-defined copy/move constructor for a union `X` copies the
234
+ object representation ([[basic.types]]) of `X`.
235
+