- tmp/tmp2atvq6yh/{from.md → to.md} +294 -97
tmp/tmp2atvq6yh/{from.md → to.md}
RENAMED
|
@@ -1,34 +1,29 @@
|
|
| 1 |
-
## Constructors <a id="class.ctor">[[class.ctor]]</a>
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
``` bnf
|
| 7 |
ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 8 |
```
|
| 9 |
|
| 10 |
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 11 |
optional *attribute-specifier-seq*, and optional surrounding
|
| 12 |
parentheses, and the *id-expression* has one of the following forms:
|
| 13 |
|
| 14 |
- in a *member-declaration* that belongs to the *member-specification*
|
| 15 |
-
of a class but is not a friend declaration
|
| 16 |
-
*id-expression* is the injected-class-name
|
| 17 |
-
immediately-enclosing
|
| 18 |
-
- in a *member-declaration* that belongs to the *member-specification*
|
| 19 |
-
of a class template but is not a friend declaration, the
|
| 20 |
-
*id-expression* is a *class-name* that names the current
|
| 21 |
-
instantiation ([[temp.dep.type]]) of the immediately-enclosing class
|
| 22 |
-
template; or
|
| 23 |
- in a declaration at namespace scope or in a friend declaration, the
|
| 24 |
-
*id-expression* is a *qualified-id* that names a constructor
|
| 25 |
-
[[class.qual]]
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
[*Example 1*:
|
| 32 |
|
| 33 |
``` cpp
|
| 34 |
struct S {
|
|
@@ -40,169 +35,371 @@ S::S() { } // defines the constructor
|
|
| 40 |
|
| 41 |
— *end example*]
|
| 42 |
|
| 43 |
A constructor is used to initialize objects of its class type. Because
|
| 44 |
constructors do not have names, they are never found during name lookup;
|
| 45 |
-
however an explicit type conversion using the functional notation
|
| 46 |
-
[[expr.type.conv]]
|
| 47 |
an object.
|
| 48 |
|
| 49 |
-
[*Note 1*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
[[class.init]]. — *end note*]
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
A constructor can be invoked for a `const`, `volatile` or `const`
|
| 53 |
-
`volatile` object. `const` and `volatile` semantics
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
ends.
|
| 57 |
|
| 58 |
-
A
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
for which each parameter that is not a function parameter pack has a
|
| 60 |
default argument (including the case of a constructor with no
|
| 61 |
parameters). If there is no user-declared constructor for class `X`, a
|
| 62 |
non-explicit constructor having no parameters is implicitly declared as
|
| 63 |
-
defaulted
|
| 64 |
-
|
| 65 |
|
| 66 |
A defaulted default constructor for class `X` is defined as deleted if:
|
| 67 |
|
| 68 |
- `X` is a union that has a variant member with a non-trivial default
|
| 69 |
constructor and no variant member of `X` has a default member
|
| 70 |
initializer,
|
| 71 |
- `X` is a non-union class that has a variant member `M` with a
|
| 72 |
non-trivial default constructor and no variant member of the anonymous
|
| 73 |
union containing `M` has a default member initializer,
|
| 74 |
-
- any non-static data member with no default member initializer
|
| 75 |
-
[[class.mem]]
|
| 76 |
- any non-variant non-static data member of const-qualified type (or
|
| 77 |
-
array thereof) with no *brace-or-equal-initializer*
|
| 78 |
-
|
| 79 |
- `X` is a union and all of its variant members are of const-qualified
|
| 80 |
type (or array thereof),
|
| 81 |
- `X` is a non-union class and all members of any anonymous union member
|
| 82 |
are of const-qualified type (or array thereof),
|
| 83 |
- any potentially constructed subobject, except for a non-static data
|
| 84 |
member with a *brace-or-equal-initializer*, has class type `M` (or
|
| 85 |
array thereof) and either `M` has no default constructor or overload
|
| 86 |
-
resolution
|
| 87 |
constructor results in an ambiguity or in a function that is deleted
|
| 88 |
or inaccessible from the defaulted default constructor, or
|
| 89 |
- any potentially constructed subobject has a type with a destructor
|
| 90 |
that is deleted or inaccessible from the defaulted default
|
| 91 |
constructor.
|
| 92 |
|
| 93 |
A default constructor is *trivial* if it is not user-provided and if:
|
| 94 |
|
| 95 |
-
- its class has no virtual functions
|
| 96 |
-
base classes
|
| 97 |
- no non-static data member of its class has a default member
|
| 98 |
-
initializer
|
| 99 |
- all the direct base classes of its class have trivial default
|
| 100 |
constructors, and
|
| 101 |
- for all the non-static data members of its class that are of class
|
| 102 |
type (or array thereof), each such class has a trivial default
|
| 103 |
constructor.
|
| 104 |
|
| 105 |
Otherwise, the default constructor is *non-trivial*.
|
| 106 |
|
| 107 |
A default constructor that is defaulted and not defined as deleted is
|
| 108 |
-
*implicitly defined* when it is odr-used
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
| 114 |
*compound-statement*. If that user-written default constructor would be
|
| 115 |
ill-formed, the program is ill-formed. If that user-written default
|
| 116 |
-
constructor would satisfy the requirements of a constexpr constructor
|
| 117 |
-
[[dcl.constexpr]]
|
| 118 |
`constexpr`. Before the defaulted default constructor for a class is
|
| 119 |
implicitly defined, all the non-user-provided default constructors for
|
| 120 |
-
its base classes and its non-static data members
|
| 121 |
-
implicitly defined.
|
| 122 |
|
| 123 |
-
[*Note
|
| 124 |
-
specification
|
| 125 |
-
|
| 126 |
[[dcl.fct.def]]. — *end note*]
|
| 127 |
|
| 128 |
Default constructors are called implicitly to create class objects of
|
| 129 |
static, thread, or automatic storage duration ([[basic.stc.static]],
|
| 130 |
-
[[basic.stc.thread]], [[basic.stc.auto]]) defined without an
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
[[expr.
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
accessible (Clause [[class.access]]).
|
| 138 |
|
| 139 |
-
[*Note
|
| 140 |
constructors for base classes and non-static data members are called and
|
| 141 |
describes how arguments can be specified for the calls to these
|
| 142 |
constructors. — *end note*]
|
| 143 |
|
| 144 |
-
|
| 145 |
-
return value. The address of a constructor shall not be taken.
|
| 146 |
|
| 147 |
-
A
|
| 148 |
-
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
[*
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
[*Example 2*:
|
| 154 |
|
|
|
|
|
|
|
| 155 |
``` cpp
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
```
|
| 159 |
|
| 160 |
— *end example*]
|
| 161 |
|
| 162 |
-
|
| 163 |
|
| 164 |
-
|
| 165 |
-
objects. — *end note*]
|
| 166 |
-
|
| 167 |
-
[*Note 6*: Explicit constructor calls do not yield lvalues, see
|
| 168 |
-
[[basic.lval]]. — *end note*]
|
| 169 |
-
|
| 170 |
-
[*Note 7*: Some language constructs have special semantics when used
|
| 171 |
-
during construction; see [[class.base.init]] and
|
| 172 |
-
[[class.cdtor]]. — *end note*]
|
| 173 |
-
|
| 174 |
-
During the construction of an object, if the value of the object or any
|
| 175 |
-
of its subobjects is accessed through a glvalue that is not obtained,
|
| 176 |
-
directly or indirectly, from the constructor’s `this` pointer, the value
|
| 177 |
-
of the object or subobject thus obtained is unspecified.
|
| 178 |
|
| 179 |
[*Example 3*:
|
| 180 |
|
| 181 |
``` cpp
|
| 182 |
-
struct
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
|
|
|
| 188 |
};
|
| 189 |
|
| 190 |
-
|
| 191 |
|
| 192 |
-
void
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
cout << cobj.c * 100 // value of cobj.c is unspecified
|
| 196 |
-
<< '\n';
|
| 197 |
}
|
| 198 |
-
|
| 199 |
-
extern struct D d;
|
| 200 |
-
struct D {
|
| 201 |
-
D(int a) : a(a), b(d.a) {}
|
| 202 |
-
int a, b;
|
| 203 |
-
};
|
| 204 |
-
D d = D(1); // value of d.b is unspecified
|
| 205 |
```
|
| 206 |
|
| 207 |
— *end example*]
|
| 208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### Constructors <a id="class.ctor">[[class.ctor]]</a>
|
| 2 |
|
| 3 |
+
A *constructor* is introduced by a declaration whose *declarator* is a
|
| 4 |
+
function declarator [[dcl.fct]] of the form
|
| 5 |
|
| 6 |
``` bnf
|
| 7 |
ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 8 |
```
|
| 9 |
|
| 10 |
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 11 |
optional *attribute-specifier-seq*, and optional surrounding
|
| 12 |
parentheses, and the *id-expression* has one of the following forms:
|
| 13 |
|
| 14 |
- in a *member-declaration* that belongs to the *member-specification*
|
| 15 |
+
of a class or class template but is not a friend declaration
|
| 16 |
+
[[class.friend]], the *id-expression* is the injected-class-name
|
| 17 |
+
[[class.pre]] of the immediately-enclosing entity or
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
- in a declaration at namespace scope or in a friend declaration, the
|
| 19 |
+
*id-expression* is a *qualified-id* that names a constructor
|
| 20 |
+
[[class.qual]].
|
| 21 |
|
| 22 |
+
Constructors do not have names. In a constructor declaration, each
|
| 23 |
+
*decl-specifier* in the optional *decl-specifier-seq* shall be `friend`,
|
| 24 |
+
`inline`, `constexpr`, or an *explicit-specifier*.
|
| 25 |
|
| 26 |
[*Example 1*:
|
| 27 |
|
| 28 |
``` cpp
|
| 29 |
struct S {
|
|
|
|
| 35 |
|
| 36 |
— *end example*]
|
| 37 |
|
| 38 |
A constructor is used to initialize objects of its class type. Because
|
| 39 |
constructors do not have names, they are never found during name lookup;
|
| 40 |
+
however an explicit type conversion using the functional notation
|
| 41 |
+
[[expr.type.conv]] will cause a constructor to be called to initialize
|
| 42 |
an object.
|
| 43 |
|
| 44 |
+
[*Note 1*: The syntax looks like an explicit call of the
|
| 45 |
+
constructor. — *end note*]
|
| 46 |
+
|
| 47 |
+
[*Example 2*:
|
| 48 |
+
|
| 49 |
+
``` cpp
|
| 50 |
+
complex zz = complex(1,2.3);
|
| 51 |
+
cprint( complex(7.8,1.2) );
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
— *end example*]
|
| 55 |
+
|
| 56 |
+
[*Note 2*: For initialization of objects of class type see
|
| 57 |
[[class.init]]. — *end note*]
|
| 58 |
|
| 59 |
+
An object created in this way is unnamed.
|
| 60 |
+
|
| 61 |
+
[*Note 3*: [[class.temporary]] describes the lifetime of temporary
|
| 62 |
+
objects. — *end note*]
|
| 63 |
+
|
| 64 |
+
[*Note 4*: Explicit constructor calls do not yield lvalues, see
|
| 65 |
+
[[basic.lval]]. — *end note*]
|
| 66 |
+
|
| 67 |
+
[*Note 5*: Some language constructs have special semantics when used
|
| 68 |
+
during construction; see [[class.base.init]] and
|
| 69 |
+
[[class.cdtor]]. — *end note*]
|
| 70 |
+
|
| 71 |
A constructor can be invoked for a `const`, `volatile` or `const`
|
| 72 |
+
`volatile` object. `const` and `volatile` semantics [[dcl.type.cv]] are
|
| 73 |
+
not applied on an object under construction. They come into effect when
|
| 74 |
+
the constructor for the most derived object [[intro.object]] ends.
|
|
|
|
| 75 |
|
| 76 |
+
A `return` statement in the body of a constructor shall not specify a
|
| 77 |
+
return value. The address of a constructor shall not be taken.
|
| 78 |
+
|
| 79 |
+
A constructor shall not be a coroutine.
|
| 80 |
+
|
| 81 |
+
#### Default constructors <a id="class.default.ctor">[[class.default.ctor]]</a>
|
| 82 |
+
|
| 83 |
+
A *default constructor* for a class `X` is a constructor of class `X`
|
| 84 |
for which each parameter that is not a function parameter pack has a
|
| 85 |
default argument (including the case of a constructor with no
|
| 86 |
parameters). If there is no user-declared constructor for class `X`, a
|
| 87 |
non-explicit constructor having no parameters is implicitly declared as
|
| 88 |
+
defaulted [[dcl.fct.def]]. An implicitly-declared default constructor is
|
| 89 |
+
an inline public member of its class.
|
| 90 |
|
| 91 |
A defaulted default constructor for class `X` is defined as deleted if:
|
| 92 |
|
| 93 |
- `X` is a union that has a variant member with a non-trivial default
|
| 94 |
constructor and no variant member of `X` has a default member
|
| 95 |
initializer,
|
| 96 |
- `X` is a non-union class that has a variant member `M` with a
|
| 97 |
non-trivial default constructor and no variant member of the anonymous
|
| 98 |
union containing `M` has a default member initializer,
|
| 99 |
+
- any non-static data member with no default member initializer
|
| 100 |
+
[[class.mem]] is of reference type,
|
| 101 |
- any non-variant non-static data member of const-qualified type (or
|
| 102 |
+
array thereof) with no *brace-or-equal-initializer* is not
|
| 103 |
+
const-default-constructible [[dcl.init]],
|
| 104 |
- `X` is a union and all of its variant members are of const-qualified
|
| 105 |
type (or array thereof),
|
| 106 |
- `X` is a non-union class and all members of any anonymous union member
|
| 107 |
are of const-qualified type (or array thereof),
|
| 108 |
- any potentially constructed subobject, except for a non-static data
|
| 109 |
member with a *brace-or-equal-initializer*, has class type `M` (or
|
| 110 |
array thereof) and either `M` has no default constructor or overload
|
| 111 |
+
resolution [[over.match]] as applied to find `M`’s corresponding
|
| 112 |
constructor results in an ambiguity or in a function that is deleted
|
| 113 |
or inaccessible from the defaulted default constructor, or
|
| 114 |
- any potentially constructed subobject has a type with a destructor
|
| 115 |
that is deleted or inaccessible from the defaulted default
|
| 116 |
constructor.
|
| 117 |
|
| 118 |
A default constructor is *trivial* if it is not user-provided and if:
|
| 119 |
|
| 120 |
+
- its class has no virtual functions [[class.virtual]] and no virtual
|
| 121 |
+
base classes [[class.mi]], and
|
| 122 |
- no non-static data member of its class has a default member
|
| 123 |
+
initializer [[class.mem]], and
|
| 124 |
- all the direct base classes of its class have trivial default
|
| 125 |
constructors, and
|
| 126 |
- for all the non-static data members of its class that are of class
|
| 127 |
type (or array thereof), each such class has a trivial default
|
| 128 |
constructor.
|
| 129 |
|
| 130 |
Otherwise, the default constructor is *non-trivial*.
|
| 131 |
|
| 132 |
A default constructor that is defaulted and not defined as deleted is
|
| 133 |
+
*implicitly defined* when it is odr-used [[basic.def.odr]] to create an
|
| 134 |
+
object of its class type [[intro.object]], when it is needed for
|
| 135 |
+
constant evaluation [[expr.const]], or when it is explicitly defaulted
|
| 136 |
+
after its first declaration. The implicitly-defined default constructor
|
| 137 |
+
performs the set of initializations of the class that would be performed
|
| 138 |
+
by a user-written default constructor for that class with no
|
| 139 |
+
*ctor-initializer* [[class.base.init]] and an empty
|
| 140 |
*compound-statement*. If that user-written default constructor would be
|
| 141 |
ill-formed, the program is ill-formed. If that user-written default
|
| 142 |
+
constructor would satisfy the requirements of a constexpr constructor
|
| 143 |
+
[[dcl.constexpr]], the implicitly-defined default constructor is
|
| 144 |
`constexpr`. Before the defaulted default constructor for a class is
|
| 145 |
implicitly defined, all the non-user-provided default constructors for
|
| 146 |
+
its base classes and its non-static data members are implicitly defined.
|
|
|
|
| 147 |
|
| 148 |
+
[*Note 1*: An implicitly-declared default constructor has an exception
|
| 149 |
+
specification [[except.spec]]. An explicitly-defaulted definition might
|
| 150 |
+
have an implicit exception specification, see
|
| 151 |
[[dcl.fct.def]]. — *end note*]
|
| 152 |
|
| 153 |
Default constructors are called implicitly to create class objects of
|
| 154 |
static, thread, or automatic storage duration ([[basic.stc.static]],
|
| 155 |
+
[[basic.stc.thread]], [[basic.stc.auto]]) defined without an initializer
|
| 156 |
+
[[dcl.init]], are called to create class objects of dynamic storage
|
| 157 |
+
duration [[basic.stc.dynamic]] created by a *new-expression* in which
|
| 158 |
+
the *new-initializer* is omitted [[expr.new]], or are called when the
|
| 159 |
+
explicit type conversion syntax [[expr.type.conv]] is used. A program is
|
| 160 |
+
ill-formed if the default constructor for an object is implicitly used
|
| 161 |
+
and the constructor is not accessible [[class.access]].
|
|
|
|
| 162 |
|
| 163 |
+
[*Note 2*: [[class.base.init]] describes the order in which
|
| 164 |
constructors for base classes and non-static data members are called and
|
| 165 |
describes how arguments can be specified for the calls to these
|
| 166 |
constructors. — *end note*]
|
| 167 |
|
| 168 |
+
#### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
|
|
|
|
| 169 |
|
| 170 |
+
A non-template constructor for class `X` is a copy constructor if its
|
| 171 |
+
first parameter is of type `X&`, `const X&`, `volatile X&` or
|
| 172 |
+
`const volatile X&`, and either there are no other parameters or else
|
| 173 |
+
all other parameters have default arguments [[dcl.fct.default]].
|
| 174 |
|
| 175 |
+
[*Example 1*:
|
| 176 |
+
|
| 177 |
+
`X::X(const X&)`
|
| 178 |
+
|
| 179 |
+
and `X::X(X&,int=1)` are copy constructors.
|
| 180 |
+
|
| 181 |
+
``` cpp
|
| 182 |
+
struct X {
|
| 183 |
+
X(int);
|
| 184 |
+
X(const X&, int = 1);
|
| 185 |
+
};
|
| 186 |
+
X a(1); // calls X(int);
|
| 187 |
+
X b(a, 0); // calls X(const X&, int);
|
| 188 |
+
X c = b; // calls X(const X&, int);
|
| 189 |
+
```
|
| 190 |
+
|
| 191 |
+
— *end example*]
|
| 192 |
+
|
| 193 |
+
A non-template constructor for class `X` is a move constructor if its
|
| 194 |
+
first parameter is of type `X&&`, `const X&&`, `volatile X&&`, or
|
| 195 |
+
`const volatile X&&`, and either there are no other parameters or else
|
| 196 |
+
all other parameters have default arguments [[dcl.fct.default]].
|
| 197 |
|
| 198 |
[*Example 2*:
|
| 199 |
|
| 200 |
+
`Y::Y(Y&&)` is a move constructor.
|
| 201 |
+
|
| 202 |
``` cpp
|
| 203 |
+
struct Y {
|
| 204 |
+
Y(const Y&);
|
| 205 |
+
Y(Y&&);
|
| 206 |
+
};
|
| 207 |
+
extern Y f(int);
|
| 208 |
+
Y d(f(1)); // calls Y(Y&&)
|
| 209 |
+
Y e = d; // calls Y(const Y&)
|
| 210 |
```
|
| 211 |
|
| 212 |
— *end example*]
|
| 213 |
|
| 214 |
+
[*Note 1*:
|
| 215 |
|
| 216 |
+
All forms of copy/move constructor may be declared for a class.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
[*Example 3*:
|
| 219 |
|
| 220 |
``` cpp
|
| 221 |
+
struct X {
|
| 222 |
+
X(const X&);
|
| 223 |
+
X(X&); // OK
|
| 224 |
+
X(X&&);
|
| 225 |
+
X(const X&&); // OK, but possibly not sensible
|
| 226 |
+
};
|
| 227 |
+
```
|
| 228 |
+
|
| 229 |
+
— *end example*]
|
| 230 |
+
|
| 231 |
+
— *end note*]
|
| 232 |
+
|
| 233 |
+
[*Note 2*:
|
| 234 |
+
|
| 235 |
+
If a class `X` only has a copy constructor with a parameter of type
|
| 236 |
+
`X&`, an initializer of type `const` `X` or `volatile` `X` cannot
|
| 237 |
+
initialize an object of type cv `X`.
|
| 238 |
+
|
| 239 |
+
[*Example 4*:
|
| 240 |
+
|
| 241 |
+
``` cpp
|
| 242 |
+
struct X {
|
| 243 |
+
X(); // default constructor
|
| 244 |
+
X(X&); // copy constructor with a non-const parameter
|
| 245 |
+
};
|
| 246 |
+
const X cx;
|
| 247 |
+
X x = cx; // error: X::X(X&) cannot copy cx into x
|
| 248 |
+
```
|
| 249 |
+
|
| 250 |
+
— *end example*]
|
| 251 |
+
|
| 252 |
+
— *end note*]
|
| 253 |
+
|
| 254 |
+
A declaration of a constructor for a class `X` is ill-formed if its
|
| 255 |
+
first parameter is of type cv `X` and either there are no other
|
| 256 |
+
parameters or else all other parameters have default arguments. A member
|
| 257 |
+
function template is never instantiated to produce such a constructor
|
| 258 |
+
signature.
|
| 259 |
+
|
| 260 |
+
[*Example 5*:
|
| 261 |
|
| 262 |
+
``` cpp
|
| 263 |
+
struct S {
|
| 264 |
+
template<typename T> S(T);
|
| 265 |
+
S();
|
| 266 |
};
|
| 267 |
|
| 268 |
+
S g;
|
| 269 |
|
| 270 |
+
void h() {
|
| 271 |
+
S a(g); // does not instantiate the member template to produce S::S<S>(S);
|
| 272 |
+
// uses the implicitly declared copy constructor
|
|
|
|
|
|
|
| 273 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
```
|
| 275 |
|
| 276 |
— *end example*]
|
| 277 |
|
| 278 |
+
If the class definition does not explicitly declare a copy constructor,
|
| 279 |
+
a non-explicit one is declared *implicitly*. If the class definition
|
| 280 |
+
declares a move constructor or move assignment operator, the implicitly
|
| 281 |
+
declared copy constructor is defined as deleted; otherwise, it is
|
| 282 |
+
defined as defaulted [[dcl.fct.def]]. The latter case is deprecated if
|
| 283 |
+
the class has a user-declared copy assignment operator or a
|
| 284 |
+
user-declared destructor [[depr.impldec]].
|
| 285 |
+
|
| 286 |
+
The implicitly-declared copy constructor for a class `X` will have the
|
| 287 |
+
form
|
| 288 |
+
|
| 289 |
+
``` cpp
|
| 290 |
+
X::X(const X&)
|
| 291 |
+
```
|
| 292 |
+
|
| 293 |
+
if each potentially constructed subobject of a class type `M` (or array
|
| 294 |
+
thereof) has a copy constructor whose first parameter is of type `const`
|
| 295 |
+
`M&` or `const` `volatile` `M&`.[^3] Otherwise, the implicitly-declared
|
| 296 |
+
copy constructor will have the form
|
| 297 |
+
|
| 298 |
+
``` cpp
|
| 299 |
+
X::X(X&)
|
| 300 |
+
```
|
| 301 |
+
|
| 302 |
+
If the definition of a class `X` does not explicitly declare a move
|
| 303 |
+
constructor, a non-explicit one will be implicitly declared as defaulted
|
| 304 |
+
if and only if
|
| 305 |
+
|
| 306 |
+
- `X` does not have a user-declared copy constructor,
|
| 307 |
+
- `X` does not have a user-declared copy assignment operator,
|
| 308 |
+
- `X` does not have a user-declared move assignment operator, and
|
| 309 |
+
- `X` does not have a user-declared destructor.
|
| 310 |
+
|
| 311 |
+
[*Note 3*: When the move constructor is not implicitly declared or
|
| 312 |
+
explicitly supplied, expressions that otherwise would have invoked the
|
| 313 |
+
move constructor may instead invoke a copy constructor. — *end note*]
|
| 314 |
+
|
| 315 |
+
The implicitly-declared move constructor for class `X` will have the
|
| 316 |
+
form
|
| 317 |
+
|
| 318 |
+
``` cpp
|
| 319 |
+
X::X(X&&)
|
| 320 |
+
```
|
| 321 |
+
|
| 322 |
+
An implicitly-declared copy/move constructor is an inline public member
|
| 323 |
+
of its class. A defaulted copy/move constructor for a class `X` is
|
| 324 |
+
defined as deleted [[dcl.fct.def.delete]] if `X` has:
|
| 325 |
+
|
| 326 |
+
- a potentially constructed subobject type `M` (or array thereof) that
|
| 327 |
+
cannot be copied/moved because overload resolution [[over.match]], as
|
| 328 |
+
applied to find `M`’s corresponding constructor, results in an
|
| 329 |
+
ambiguity or a function that is deleted or inaccessible from the
|
| 330 |
+
defaulted constructor,
|
| 331 |
+
- a variant member whose corresponding constructor as selected by
|
| 332 |
+
overload resolution is non-trivial,
|
| 333 |
+
- any potentially constructed subobject of a type with a destructor that
|
| 334 |
+
is deleted or inaccessible from the defaulted constructor, or,
|
| 335 |
+
- for the copy constructor, a non-static data member of rvalue reference
|
| 336 |
+
type.
|
| 337 |
+
|
| 338 |
+
[*Note 4*: A defaulted move constructor that is defined as deleted is
|
| 339 |
+
ignored by overload resolution ([[over.match]], [[over.over]]). Such a
|
| 340 |
+
constructor would otherwise interfere with initialization from an rvalue
|
| 341 |
+
which can use the copy constructor instead. — *end note*]
|
| 342 |
+
|
| 343 |
+
A copy/move constructor for class `X` is trivial if it is not
|
| 344 |
+
user-provided and if:
|
| 345 |
+
|
| 346 |
+
- class `X` has no virtual functions [[class.virtual]] and no virtual
|
| 347 |
+
base classes [[class.mi]], and
|
| 348 |
+
- the constructor selected to copy/move each direct base class subobject
|
| 349 |
+
is trivial, and
|
| 350 |
+
- for each non-static data member of `X` that is of class type (or array
|
| 351 |
+
thereof), the constructor selected to copy/move that member is
|
| 352 |
+
trivial;
|
| 353 |
+
|
| 354 |
+
otherwise the copy/move constructor is *non-trivial*.
|
| 355 |
+
|
| 356 |
+
A copy/move constructor that is defaulted and not defined as deleted is
|
| 357 |
+
*implicitly defined* when it is odr-used [[basic.def.odr]], when it is
|
| 358 |
+
needed for constant evaluation [[expr.const]], or when it is explicitly
|
| 359 |
+
defaulted after its first declaration.
|
| 360 |
+
|
| 361 |
+
[*Note 5*: The copy/move constructor is implicitly defined even if the
|
| 362 |
+
implementation elided its odr-use ([[basic.def.odr]],
|
| 363 |
+
[[class.temporary]]). — *end note*]
|
| 364 |
+
|
| 365 |
+
If the implicitly-defined constructor would satisfy the requirements of
|
| 366 |
+
a constexpr constructor [[dcl.constexpr]], the implicitly-defined
|
| 367 |
+
constructor is `constexpr`.
|
| 368 |
+
|
| 369 |
+
Before the defaulted copy/move constructor for a class is implicitly
|
| 370 |
+
defined, all non-user-provided copy/move constructors for its
|
| 371 |
+
potentially constructed subobjects are implicitly defined.
|
| 372 |
+
|
| 373 |
+
[*Note 6*: An implicitly-declared copy/move constructor has an implied
|
| 374 |
+
exception specification [[except.spec]]. — *end note*]
|
| 375 |
+
|
| 376 |
+
The implicitly-defined copy/move constructor for a non-union class `X`
|
| 377 |
+
performs a memberwise copy/move of its bases and members.
|
| 378 |
+
|
| 379 |
+
[*Note 7*: Default member initializers of non-static data members are
|
| 380 |
+
ignored. See also the example in [[class.base.init]]. — *end note*]
|
| 381 |
+
|
| 382 |
+
The order of initialization is the same as the order of initialization
|
| 383 |
+
of bases and members in a user-defined constructor (see
|
| 384 |
+
[[class.base.init]]). Let `x` be either the parameter of the constructor
|
| 385 |
+
or, for the move constructor, an xvalue referring to the parameter. Each
|
| 386 |
+
base or non-static data member is copied/moved in the manner appropriate
|
| 387 |
+
to its type:
|
| 388 |
+
|
| 389 |
+
- if the member is an array, each element is direct-initialized with the
|
| 390 |
+
corresponding subobject of `x`;
|
| 391 |
+
- if a member `m` has rvalue reference type `T&&`, it is
|
| 392 |
+
direct-initialized with `static_cast<T&&>(x.m)`;
|
| 393 |
+
- otherwise, the base or member is direct-initialized with the
|
| 394 |
+
corresponding base or member of `x`.
|
| 395 |
+
|
| 396 |
+
Virtual base class subobjects shall be initialized only once by the
|
| 397 |
+
implicitly-defined copy/move constructor (see [[class.base.init]]).
|
| 398 |
+
|
| 399 |
+
The implicitly-defined copy/move constructor for a union `X` copies the
|
| 400 |
+
object representation [[basic.types]] of `X`. For each object nested
|
| 401 |
+
within [[intro.object]] the object that is the source of the copy, a
|
| 402 |
+
corresponding object o nested within the destination is identified (if
|
| 403 |
+
the object is a subobject) or created (otherwise), and the lifetime of o
|
| 404 |
+
begins before the copy is performed.
|
| 405 |
+
|