- tmp/tmpid3heaz2/{from.md → to.md} +283 -253
tmp/tmpid3heaz2/{from.md → to.md}
RENAMED
|
@@ -5,12 +5,14 @@ assignment operator ([[class.copy]]), move constructor and move
|
|
| 5 |
assignment operator ([[class.copy]]), and destructor ([[class.dtor]])
|
| 6 |
are *special member functions*. The implementation will implicitly
|
| 7 |
declare these member functions for some class types when the program
|
| 8 |
does not explicitly declare them. The implementation will implicitly
|
| 9 |
define them if they are odr-used ([[basic.def.odr]]). See
|
| 10 |
-
[[class.ctor]], [[class.dtor]] and [[class.copy]].
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
Programs may explicitly refer to implicitly-declared special member
|
| 14 |
functions. a program may explicitly call, take the address of or form a
|
| 15 |
pointer to member to an implicitly-declared special member function.
|
| 16 |
|
|
@@ -32,22 +34,44 @@ implicitly.
|
|
| 32 |
|
| 33 |
Special member functions obey the usual access rules (Clause
|
| 34 |
[[class.access]]). declaring a constructor `protected` ensures that only
|
| 35 |
derived classes and friends can create objects using it.
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
## Constructors <a id="class.ctor">[[class.ctor]]</a>
|
| 38 |
|
| 39 |
-
Constructors do not have names. A
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
- a parameter list
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
``` cpp
|
| 51 |
struct S {
|
| 52 |
S(); // declares the constructor
|
| 53 |
};
|
|
@@ -60,21 +84,15 @@ constructors do not have names, they are never found during name lookup;
|
|
| 60 |
however an explicit type conversion using the functional notation (
|
| 61 |
[[expr.type.conv]]) will cause a constructor to be called to initialize
|
| 62 |
an object. For initialization of objects of class type see
|
| 63 |
[[class.init]].
|
| 64 |
|
| 65 |
-
A
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
`volatile` or `const` `volatile` object. A constructor shall not be
|
| 71 |
-
declared `const`, `volatile`, or `const` `volatile` ([[class.this]]).
|
| 72 |
-
`const` and `volatile` semantics ([[dcl.type.cv]]) are not applied on
|
| 73 |
-
an object under construction. They come into effect when the constructor
|
| 74 |
-
for the most derived object ([[intro.object]]) ends. A constructor
|
| 75 |
-
shall not be declared with a *ref-qualifier*.
|
| 76 |
|
| 77 |
A *default* constructor for a class `X` is a constructor of class `X`
|
| 78 |
that can be called without an argument. If there is no user-declared
|
| 79 |
constructor for class `X`, a constructor having no parameters is
|
| 80 |
implicitly declared as defaulted ([[dcl.fct.def]]). An
|
|
@@ -91,19 +109,19 @@ as deleted if:
|
|
| 91 |
user-provided default constructor,
|
| 92 |
- `X` is a union and all of its variant members are of const-qualified
|
| 93 |
type (or array thereof),
|
| 94 |
- `X` is a non-union class and all members of any anonymous union member
|
| 95 |
are of const-qualified type (or array thereof),
|
| 96 |
-
- any
|
| 97 |
-
*brace-or-equal-initializer*, has class type `M` (or
|
| 98 |
-
and either `M` has no default constructor or overload
|
| 99 |
-
[[over.match]]) as applied to `M`’s default constructor
|
| 100 |
-
ambiguity or in a function that is deleted or
|
| 101 |
-
defaulted default constructor, or
|
| 102 |
-
- any
|
| 103 |
-
|
| 104 |
-
|
| 105 |
|
| 106 |
A default constructor is trivial if it is not user-provided and if:
|
| 107 |
|
| 108 |
- its class has no virtual functions ([[class.virtual]]) and no virtual
|
| 109 |
base classes ([[class.mi]]), and
|
|
@@ -149,16 +167,11 @@ accessible (Clause [[class.access]]).
|
|
| 149 |
|
| 150 |
[[class.base.init]] describes the order in which constructors for base
|
| 151 |
classes and non-static data members are called and describes how
|
| 152 |
arguments can be specified for the calls to these constructors.
|
| 153 |
|
| 154 |
-
A
|
| 155 |
-
type. A move constructor ([[class.copy]]) is used to move the contents
|
| 156 |
-
of objects of class type.
|
| 157 |
-
|
| 158 |
-
No return type (not even `void`) shall be specified for a constructor. A
|
| 159 |
-
`return` statement in the body of a constructor shall not specify a
|
| 160 |
return value. The address of a constructor shall not be taken.
|
| 161 |
|
| 162 |
A functional notation type conversion ([[expr.type.conv]]) can be used
|
| 163 |
to create new objects of its type. The syntax looks like an explicit
|
| 164 |
call of the constructor.
|
|
@@ -203,23 +216,21 @@ void no_opt(C* cptr) {
|
|
| 203 |
|
| 204 |
Temporaries of class type are created in various contexts: binding a
|
| 205 |
reference to a prvalue ([[dcl.init.ref]]), returning a prvalue (
|
| 206 |
[[stmt.return]]), a conversion that creates a prvalue ([[conv.lval]],
|
| 207 |
[[expr.static.cast]], [[expr.const.cast]], [[expr.cast]]), throwing an
|
| 208 |
-
exception ([[except.throw]]),
|
| 209 |
-
[[
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
*decltype-specifier* ([[expr.call]]), no temporary is introduced, so
|
| 220 |
-
the foregoing does not apply to the prvalue of any such function call.
|
| 221 |
|
| 222 |
Consider the following code:
|
| 223 |
|
| 224 |
``` cpp
|
| 225 |
class X {
|
|
@@ -280,18 +291,18 @@ point than the end of the full-expression. The first context is when a
|
|
| 280 |
default constructor is called to initialize an element of an array. If
|
| 281 |
the constructor has one or more default arguments, the destruction of
|
| 282 |
every temporary created in a default argument is sequenced before the
|
| 283 |
construction of the next array element, if any.
|
| 284 |
|
| 285 |
-
The second context is when a reference is bound to a temporary. The
|
| 286 |
temporary to which the reference is bound or the temporary that is the
|
| 287 |
complete object of a subobject to which the reference is bound persists
|
| 288 |
for the lifetime of the reference except:
|
| 289 |
|
| 290 |
- A temporary bound to a reference member in a constructor’s
|
| 291 |
-
ctor-initializer ([[class.base.init]]) persists until the
|
| 292 |
-
exits.
|
| 293 |
- A temporary bound to a reference parameter in a function call (
|
| 294 |
[[expr.call]]) persists until the completion of the full-expression
|
| 295 |
containing the call.
|
| 296 |
- The lifetime of a temporary bound to the returned value in a function
|
| 297 |
return statement ([[stmt.return]]) is not extended; the temporary is
|
|
@@ -340,21 +351,21 @@ the expression `S(16) + S(23)` creates three temporaries: a first
|
|
| 340 |
temporary `T1` to hold the result of the expression `S(16)`, a second
|
| 341 |
temporary `T2` to hold the result of the expression `S(23)`, and a third
|
| 342 |
temporary `T3` to hold the result of the addition of these two
|
| 343 |
expressions. The temporary `T3` is then bound to the reference `cr`. It
|
| 344 |
is unspecified whether `T1` or `T2` is created first. On an
|
| 345 |
-
implementation where `T1` is created before `T2`,
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
|
| 357 |
## Conversions <a id="class.conv">[[class.conv]]</a>
|
| 358 |
|
| 359 |
Type conversions of class objects can be specified by constructors and
|
| 360 |
by conversion functions. These conversions are called *user-defined
|
|
@@ -418,17 +429,19 @@ its class. Such a constructor is called a *converting constructor*.
|
|
| 418 |
|
| 419 |
``` cpp
|
| 420 |
struct X {
|
| 421 |
X(int);
|
| 422 |
X(const char*, int =0);
|
|
|
|
| 423 |
};
|
| 424 |
|
| 425 |
void f(X arg) {
|
| 426 |
X a = 1; // a = X(1)
|
| 427 |
X b = "Jessie"; // b = X("Jessie",0)
|
| 428 |
a = 2; // a = X(2)
|
| 429 |
f(3); // f(X(3))
|
|
|
|
| 430 |
}
|
| 431 |
```
|
| 432 |
|
| 433 |
An explicit constructor constructs objects just like non-explicit
|
| 434 |
constructors, but does so only where the direct-initialization syntax (
|
|
@@ -439,19 +452,21 @@ value-initialization ([[dcl.init]]).
|
|
| 439 |
|
| 440 |
``` cpp
|
| 441 |
struct Z {
|
| 442 |
explicit Z();
|
| 443 |
explicit Z(int);
|
|
|
|
| 444 |
};
|
| 445 |
|
| 446 |
Z a; // OK: default-initialization performed
|
| 447 |
Z a1 = 1; // error: no implicit conversion
|
| 448 |
Z a3 = Z(1); // OK: direct initialization syntax used
|
| 449 |
Z a2(1); // OK: direct initialization syntax used
|
| 450 |
Z* p = new Z(1); // OK: direct initialization syntax used
|
| 451 |
Z a4 = (Z)1; // OK: explicit cast used
|
| 452 |
Z a5 = static_cast<Z>(1); // OK: explicit cast used
|
|
|
|
| 453 |
```
|
| 454 |
|
| 455 |
A non-explicit copy/move constructor ([[class.copy]]) is a converting
|
| 456 |
constructor. An implicitly-declared copy/move constructor is not an
|
| 457 |
explicit constructor; it may be called for implicit type conversions.
|
|
@@ -482,11 +497,11 @@ return type can be specified. If a conversion function is a member
|
|
| 482 |
function, the type of the conversion function ([[dcl.fct]]) is
|
| 483 |
“function taking no parameter returning *conversion-type-id*”. A
|
| 484 |
conversion function is never used to convert a (possibly cv-qualified)
|
| 485 |
object to the (possibly cv-qualified) same object type (or a reference
|
| 486 |
to it), to a (possibly cv-qualified) base class of that type (or a
|
| 487 |
-
reference to it), or to (possibly cv-qualified) void.[^
|
| 488 |
|
| 489 |
``` cpp
|
| 490 |
struct X {
|
| 491 |
operator int();
|
| 492 |
};
|
|
@@ -547,29 +562,46 @@ Conversion functions can be virtual.
|
|
| 547 |
|
| 548 |
Conversion functions cannot be declared `static`.
|
| 549 |
|
| 550 |
## Destructors <a id="class.dtor">[[class.dtor]]</a>
|
| 551 |
|
| 552 |
-
A
|
| 553 |
-
|
| 554 |
-
name followed by an empty parameter list is used to declare the
|
| 555 |
-
destructor in a class definition. In such a declaration, the `~`
|
| 556 |
-
followed by the destructor’s class name can be enclosed in optional
|
| 557 |
-
parentheses; such parentheses are ignored. A *typedef-name* shall not be
|
| 558 |
-
used as the *class-name* following the `~` in the declarator for a
|
| 559 |
-
destructor declaration.
|
| 560 |
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 571 |
|
| 572 |
A declaration of a destructor that does not have an
|
| 573 |
*exception-specification* is implicitly considered to have the same
|
| 574 |
*exception-specification* as an implicit declaration ([[except.spec]]).
|
| 575 |
|
|
@@ -579,15 +611,13 @@ destructor is an `inline` `public` member of its class.
|
|
| 579 |
|
| 580 |
A defaulted destructor for a class `X` is defined as deleted if:
|
| 581 |
|
| 582 |
- `X` is a union-like class that has a variant member with a non-trivial
|
| 583 |
destructor,
|
| 584 |
-
- any
|
| 585 |
thereof) and `M` has a deleted destructor or a destructor that is
|
| 586 |
inaccessible from the defaulted destructor,
|
| 587 |
-
- any direct or virtual base class has a deleted destructor or a
|
| 588 |
-
destructor that is inaccessible from the defaulted destructor,
|
| 589 |
- or, for a virtual destructor, lookup of the non-array deallocation
|
| 590 |
function results in an ambiguity or in a function that is deleted or
|
| 591 |
inaccessible from the defaulted destructor.
|
| 592 |
|
| 593 |
A destructor is trivial if it is not user-provided and if:
|
|
@@ -631,30 +661,33 @@ defined. If a class has a base class with a virtual destructor, its
|
|
| 631 |
destructor (whether user- or implicitly-declared) is virtual.
|
| 632 |
|
| 633 |
some language constructs have special semantics when used during
|
| 634 |
destruction; see [[class.cdtor]].
|
| 635 |
|
| 636 |
-
|
| 637 |
|
| 638 |
-
- for constructed
|
| 639 |
[[basic.stc.static]]) at program termination ([[basic.start.term]]),
|
| 640 |
-
- for constructed
|
| 641 |
[[basic.stc.thread]]) at thread exit,
|
| 642 |
-
- for constructed
|
| 643 |
[[basic.stc.auto]]) when the block in which an object is created
|
| 644 |
exits ([[stmt.dcl]]),
|
| 645 |
-
- for constructed temporary
|
| 646 |
-
|
| 647 |
-
- for constructed objects allocated by a *new-expression* (
|
| 648 |
-
[[expr.new]]), through use of a *delete-expression* (
|
| 649 |
-
[[expr.delete]]),
|
| 650 |
-
- in several situations due to the handling of exceptions (
|
| 651 |
-
[[except.handle]]).
|
| 652 |
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
of
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
|
| 657 |
At the point of definition of a virtual destructor (including an
|
| 658 |
implicit definition ([[class.copy]])), the non-array deallocation
|
| 659 |
function is looked up in the scope of the destructor’s class (
|
| 660 |
[[class.member.lookup]]), and, if no declaration is found, the function
|
|
@@ -666,14 +699,16 @@ deallocation function corresponding to the dynamic type of an object is
|
|
| 666 |
available for the *delete-expression* ([[class.free]]).
|
| 667 |
|
| 668 |
In an explicit destructor call, the destructor name appears as a `~`
|
| 669 |
followed by a *type-name* or *decltype-specifier* that denotes the
|
| 670 |
destructor’s class type. The invocation of a destructor is subject to
|
| 671 |
-
the usual rules for member functions ([[class.mfct]])
|
| 672 |
object is not of the destructor’s class type and not of a class derived
|
| 673 |
-
from the destructor’s class type
|
| 674 |
-
|
|
|
|
|
|
|
| 675 |
|
| 676 |
``` cpp
|
| 677 |
struct B {
|
| 678 |
virtual ~B() { }
|
| 679 |
};
|
|
@@ -763,28 +798,23 @@ void foo(int i) {
|
|
| 763 |
When an object is deleted with a *delete-expression* ([[expr.delete]]),
|
| 764 |
a *deallocation function* (`operator delete()` for non-array objects or
|
| 765 |
`operator delete[]()` for arrays) is (implicitly) called to reclaim the
|
| 766 |
storage occupied by the object ([[basic.stc.dynamic.deallocation]]).
|
| 767 |
|
| 768 |
-
|
| 769 |
-
deallocation function
|
| 770 |
-
the *delete-expression* is used to deallocate a class object whose
|
| 771 |
static type has a virtual destructor, the deallocation function is the
|
| 772 |
one selected at the point of definition of the dynamic type’s virtual
|
| 773 |
-
destructor ([[class.dtor]]).[^
|
| 774 |
is used to deallocate an object of class `T` or array thereof, the
|
| 775 |
static and dynamic types of the object shall be identical and the
|
| 776 |
deallocation function’s name is looked up in the scope of `T`. If this
|
| 777 |
-
lookup fails to find the name,
|
| 778 |
-
|
| 779 |
-
the lookup selects a placement deallocation
|
| 780 |
-
ill-formed.
|
| 781 |
-
|
| 782 |
-
When a *delete-expression* is executed, the selected deallocation
|
| 783 |
-
function shall be called with the address of the block of storage to be
|
| 784 |
-
reclaimed as its first argument and (if the two-parameter style is used)
|
| 785 |
-
the size of the block as its second argument.[^3]
|
| 786 |
|
| 787 |
Any deallocation function for a class `X` is a static member (even if
|
| 788 |
not explicitly declared `static`).
|
| 789 |
|
| 790 |
``` cpp
|
|
@@ -901,13 +931,12 @@ complex d = sqrt(b,c); // call sqrt(complex,complex)
|
|
| 901 |
complex e; // initialize by a call of
|
| 902 |
// complex()
|
| 903 |
complex f = 3; // construct complex(3) using
|
| 904 |
// complex(double)
|
| 905 |
// copy/move it into f
|
| 906 |
-
complex g = { 1, 2 }; //
|
| 907 |
-
//
|
| 908 |
-
// and copy/move it into g
|
| 909 |
```
|
| 910 |
|
| 911 |
overloading of the assignment operator ([[over.ass]]) has no effect on
|
| 912 |
initialization.
|
| 913 |
|
|
@@ -958,11 +987,11 @@ ctor-initializer:
|
|
| 958 |
```
|
| 959 |
|
| 960 |
``` bnf
|
| 961 |
mem-initializer-list:
|
| 962 |
mem-initializer '...'ₒₚₜ
|
| 963 |
-
mem-initializer ',' mem-initializer-list
|
| 964 |
```
|
| 965 |
|
| 966 |
``` bnf
|
| 967 |
mem-initializer:
|
| 968 |
mem-initializer-id '(' expression-listₒₚₜ ')'
|
|
@@ -1060,31 +1089,39 @@ full-expression. Any expression in a *mem-initializer* is evaluated as
|
|
| 1060 |
part of the full-expression that performs the initialization. A
|
| 1061 |
*mem-initializer* where the *mem-initializer-id* denotes a virtual base
|
| 1062 |
class is ignored during execution of a constructor of any class that is
|
| 1063 |
not the most derived class.
|
| 1064 |
|
| 1065 |
-
In a non-delegating constructor, if a given
|
| 1066 |
-
|
| 1067 |
case where there is no *mem-initializer-list* because the constructor
|
| 1068 |
-
has no *ctor-initializer*)
|
| 1069 |
-
an abstract class ([[class.abstract]]), then
|
| 1070 |
|
| 1071 |
- if the entity is a non-static data member that has a
|
| 1072 |
-
*brace-or-equal-initializer*
|
| 1073 |
-
|
| 1074 |
-
|
| 1075 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1076 |
- otherwise, the entity is default-initialized ([[dcl.init]]).
|
| 1077 |
|
| 1078 |
An abstract class ([[class.abstract]]) is never a most derived class,
|
| 1079 |
thus its constructors never initialize virtual base classes, therefore
|
| 1080 |
the corresponding *mem-initializer*s may be omitted. An attempt to
|
| 1081 |
initialize more than one non-static data member of a union renders the
|
| 1082 |
-
program ill-formed. After the call to a constructor for class `X`
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
|
|
|
|
|
|
| 1086 |
|
| 1087 |
``` cpp
|
| 1088 |
struct A {
|
| 1089 |
A();
|
| 1090 |
};
|
|
@@ -1117,10 +1154,16 @@ struct A {
|
|
| 1117 |
|
| 1118 |
the `A(int)` constructor will simply initialize `i` to the value of
|
| 1119 |
`arg`, and the side effects in `i`’s *brace-or-equal-initializer* will
|
| 1120 |
not take place.
|
| 1121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1122 |
In a non-delegating constructor, initialization proceeds in the
|
| 1123 |
following order:
|
| 1124 |
|
| 1125 |
- First, and only for the constructor of the most derived class (
|
| 1126 |
[[intro.object]]), virtual base classes are initialized in the order
|
|
@@ -1508,59 +1551,35 @@ If the class definition does not explicitly declare a copy constructor,
|
|
| 1508 |
one is declared *implicitly*. If the class definition declares a move
|
| 1509 |
constructor or move assignment operator, the implicitly declared copy
|
| 1510 |
constructor is defined as deleted; otherwise, it is defined as
|
| 1511 |
defaulted ([[dcl.fct.def]]). The latter case is deprecated if the class
|
| 1512 |
has a user-declared copy assignment operator or a user-declared
|
| 1513 |
-
destructor.
|
| 1514 |
-
|
| 1515 |
-
``` cpp
|
| 1516 |
-
struct X {
|
| 1517 |
-
X(const X&, int);
|
| 1518 |
-
};
|
| 1519 |
-
```
|
| 1520 |
-
|
| 1521 |
-
a copy constructor is implicitly-declared. If the user-declared
|
| 1522 |
-
constructor is later defined as
|
| 1523 |
-
|
| 1524 |
-
``` cpp
|
| 1525 |
-
X::X(const X& x, int i =0) { /* ... */ }
|
| 1526 |
-
```
|
| 1527 |
-
|
| 1528 |
-
then any use of `X`’s copy constructor is ill-formed because of the
|
| 1529 |
-
ambiguity; no diagnostic is required.
|
| 1530 |
|
| 1531 |
The implicitly-declared copy constructor for a class `X` will have the
|
| 1532 |
form
|
| 1533 |
|
| 1534 |
``` cpp
|
| 1535 |
X::X(const X&)
|
| 1536 |
```
|
| 1537 |
|
| 1538 |
-
if
|
| 1539 |
-
|
| 1540 |
-
|
| 1541 |
-
|
| 1542 |
-
`B&`, and
|
| 1543 |
-
- for all the non-static data members of `X` that are of a class type
|
| 1544 |
-
`M` (or array thereof), each such class type has a copy constructor
|
| 1545 |
-
whose first parameter is of type `const` `M&` or `const` `volatile`
|
| 1546 |
-
`M&`.[^4]
|
| 1547 |
-
|
| 1548 |
-
Otherwise, the implicitly-declared copy constructor will have the form
|
| 1549 |
|
| 1550 |
``` cpp
|
| 1551 |
X::X(X&)
|
| 1552 |
```
|
| 1553 |
|
| 1554 |
If the definition of a class `X` does not explicitly declare a move
|
| 1555 |
constructor, one will be implicitly declared as defaulted if and only if
|
| 1556 |
|
| 1557 |
- `X` does not have a user-declared copy constructor,
|
| 1558 |
- `X` does not have a user-declared copy assignment operator,
|
| 1559 |
-
- `X` does not have a user-declared move assignment operator,
|
| 1560 |
-
- `X` does not have a user-declared destructor
|
| 1561 |
-
- the move constructor would not be implicitly defined as deleted.
|
| 1562 |
|
| 1563 |
When the move constructor is not implicitly declared or explicitly
|
| 1564 |
supplied, expressions that otherwise would have invoked the move
|
| 1565 |
constructor may instead invoke a copy constructor.
|
| 1566 |
|
|
@@ -1575,56 +1594,54 @@ An implicitly-declared copy/move constructor is an `inline` `public`
|
|
| 1575 |
member of its class. A defaulted copy/move constructor for a class `X`
|
| 1576 |
is defined as deleted ([[dcl.fct.def.delete]]) if `X` has:
|
| 1577 |
|
| 1578 |
- a variant member with a non-trivial corresponding constructor and `X`
|
| 1579 |
is a union-like class,
|
| 1580 |
-
- a
|
| 1581 |
cannot be copied/moved because overload resolution ([[over.match]]),
|
| 1582 |
as applied to `M`’s corresponding constructor, results in an ambiguity
|
| 1583 |
or a function that is deleted or inaccessible from the defaulted
|
| 1584 |
constructor,
|
| 1585 |
-
-
|
| 1586 |
-
|
| 1587 |
-
corresponding constructor, results in an ambiguity or a function that
|
| 1588 |
-
is deleted or inaccessible from the defaulted constructor,
|
| 1589 |
-
- any direct or virtual base class or non-static data member of a type
|
| 1590 |
-
with a destructor that is deleted or inaccessible from the defaulted
|
| 1591 |
-
constructor,
|
| 1592 |
- for the copy constructor, a non-static data member of rvalue reference
|
| 1593 |
-
type
|
| 1594 |
-
|
| 1595 |
-
|
| 1596 |
-
|
|
|
|
|
|
|
| 1597 |
|
| 1598 |
A copy/move constructor for class `X` is trivial if it is not
|
| 1599 |
-
user-provided
|
|
|
|
| 1600 |
|
| 1601 |
- class `X` has no virtual functions ([[class.virtual]]) and no virtual
|
| 1602 |
base classes ([[class.mi]]), and
|
|
|
|
|
|
|
| 1603 |
- the constructor selected to copy/move each direct base class subobject
|
| 1604 |
is trivial, and
|
| 1605 |
- for each non-static data member of `X` that is of class type (or array
|
| 1606 |
thereof), the constructor selected to copy/move that member is
|
| 1607 |
trivial;
|
| 1608 |
|
| 1609 |
otherwise the copy/move constructor is *non-trivial*.
|
| 1610 |
|
| 1611 |
A copy/move constructor that is defaulted and not defined as deleted is
|
| 1612 |
-
*implicitly defined* if it is odr-used ([[basic.def.odr]])
|
| 1613 |
-
initialize an object of its class type from a copy of an object of its
|
| 1614 |
-
class type or of a class type derived from its class type[^5] or when it
|
| 1615 |
is explicitly defaulted after its first declaration. The copy/move
|
| 1616 |
constructor is implicitly defined even if the implementation elided its
|
| 1617 |
odr-use ([[basic.def.odr]], [[class.temporary]]). If the
|
| 1618 |
implicitly-defined constructor would satisfy the requirements of a
|
| 1619 |
`constexpr` constructor ([[dcl.constexpr]]), the implicitly-defined
|
| 1620 |
constructor is `constexpr`.
|
| 1621 |
|
| 1622 |
Before the defaulted copy/move constructor for a class is implicitly
|
| 1623 |
-
defined, all non-user-provided copy/move constructors for its
|
| 1624 |
-
|
| 1625 |
-
|
| 1626 |
*exception-specification* ([[except.spec]]).
|
| 1627 |
|
| 1628 |
The implicitly-defined copy/move constructor for a non-union class `X`
|
| 1629 |
performs a memberwise copy/move of its bases and members.
|
| 1630 |
*brace-or-equal-initializer*s of non-static data members are ignored.
|
|
@@ -1650,11 +1667,11 @@ The implicitly-defined copy/move constructor for a union `X` copies the
|
|
| 1650 |
object representation ([[basic.types]]) of `X`.
|
| 1651 |
|
| 1652 |
A user-declared *copy* assignment operator `X::operator=` is a
|
| 1653 |
non-static non-template member function of class `X` with exactly one
|
| 1654 |
parameter of type `X`, `X&`, `const` `X&`, `volatile` `X&` or `const`
|
| 1655 |
-
`volatile` `X&`.[^
|
| 1656 |
to have only one parameter; see [[over.ass]]. More than one form of
|
| 1657 |
copy assignment operator may be declared for a class. If a class `X`
|
| 1658 |
only has a copy assignment operator with a parameter of type `X&`, an
|
| 1659 |
expression of type const `X` cannot be assigned to an object of type
|
| 1660 |
`X`.
|
|
@@ -1689,11 +1706,11 @@ if
|
|
| 1689 |
- each direct base class `B` of `X` has a copy assignment operator whose
|
| 1690 |
parameter is of type `const` `B&`, `const` `volatile` `B&` or `B`, and
|
| 1691 |
- for all the non-static data members of `X` that are of a class type
|
| 1692 |
`M` (or array thereof), each such class type has a copy assignment
|
| 1693 |
operator whose parameter is of type `const` `M&`, `const` `volatile`
|
| 1694 |
-
`M&` or `M`.[^
|
| 1695 |
|
| 1696 |
Otherwise, the implicitly-declared copy assignment operator will have
|
| 1697 |
the form
|
| 1698 |
|
| 1699 |
``` cpp
|
|
@@ -1711,14 +1728,12 @@ If the definition of a class `X` does not explicitly declare a move
|
|
| 1711 |
assignment operator, one will be implicitly declared as defaulted if and
|
| 1712 |
only if
|
| 1713 |
|
| 1714 |
- `X` does not have a user-declared copy constructor,
|
| 1715 |
- `X` does not have a user-declared move constructor,
|
| 1716 |
-
- `X` does not have a user-declared copy assignment operator,
|
| 1717 |
-
- `X` does not have a user-declared destructor
|
| 1718 |
-
- the move assignment operator would not be implicitly defined as
|
| 1719 |
-
deleted.
|
| 1720 |
|
| 1721 |
The class definition
|
| 1722 |
|
| 1723 |
``` cpp
|
| 1724 |
struct S {
|
|
@@ -1758,24 +1773,18 @@ deleted if `X` has:
|
|
| 1758 |
- a variant member with a non-trivial corresponding assignment operator
|
| 1759 |
and `X` is a union-like class, or
|
| 1760 |
- a non-static data member of `const` non-class type (or array thereof),
|
| 1761 |
or
|
| 1762 |
- a non-static data member of reference type, or
|
| 1763 |
-
- a
|
| 1764 |
-
cannot be copied/moved because overload resolution (
|
| 1765 |
-
as applied to `M`’s corresponding assignment
|
| 1766 |
-
ambiguity or a function that is deleted or
|
| 1767 |
-
defaulted assignment operator
|
| 1768 |
-
|
| 1769 |
-
|
| 1770 |
-
|
| 1771 |
-
function that is deleted or inaccessible from the defaulted assignment
|
| 1772 |
-
operator, or
|
| 1773 |
-
- for the move assignment operator, a non-static data member or direct
|
| 1774 |
-
base class with a type that does not have a move assignment operator
|
| 1775 |
-
and is not trivially copyable, or any direct or indirect virtual base
|
| 1776 |
-
class.
|
| 1777 |
|
| 1778 |
Because a copy/move assignment operator is implicitly declared for a
|
| 1779 |
class if not declared by the user, a base class copy/move assignment
|
| 1780 |
operator is always hidden by the corresponding assignment operator of a
|
| 1781 |
derived class ([[over.ass]]). A *using-declaration* (
|
|
@@ -1786,27 +1795,38 @@ declaration of such an operator and does not suppress the implicit
|
|
| 1786 |
declaration of the derived class operator; the operator introduced by
|
| 1787 |
the *using-declaration* is hidden by the implicitly-declared operator in
|
| 1788 |
the derived class.
|
| 1789 |
|
| 1790 |
A copy/move assignment operator for class `X` is trivial if it is not
|
| 1791 |
-
user-provided
|
|
|
|
| 1792 |
|
| 1793 |
- class `X` has no virtual functions ([[class.virtual]]) and no virtual
|
| 1794 |
base classes ([[class.mi]]), and
|
|
|
|
|
|
|
| 1795 |
- the assignment operator selected to copy/move each direct base class
|
| 1796 |
subobject is trivial, and
|
| 1797 |
- for each non-static data member of `X` that is of class type (or array
|
| 1798 |
thereof), the assignment operator selected to copy/move that member is
|
| 1799 |
trivial;
|
| 1800 |
|
| 1801 |
otherwise the copy/move assignment operator is *non-trivial*.
|
| 1802 |
|
| 1803 |
-
A copy/move assignment operator that is defaulted and
|
| 1804 |
-
deleted is *implicitly defined* when it is odr-used (
|
| 1805 |
-
(e.g., when it is selected by overload resolution to
|
| 1806 |
-
of its class type) or when it is explicitly
|
| 1807 |
-
declaration.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1808 |
|
| 1809 |
Before the defaulted copy/move assignment operator for a class is
|
| 1810 |
implicitly defined, all non-user-provided copy/move assignment operators
|
| 1811 |
for its direct base classes and its non-static data members shall have
|
| 1812 |
been implicitly defined. An implicitly-declared copy/move assignment
|
|
@@ -1857,19 +1877,20 @@ member function is not accessible (Clause [[class.access]]).
|
|
| 1857 |
Copying/moving one object into another using the copy/move constructor
|
| 1858 |
or the copy/move assignment operator does not change the layout or size
|
| 1859 |
of either object.
|
| 1860 |
|
| 1861 |
When certain criteria are met, an implementation is allowed to omit the
|
| 1862 |
-
copy/move construction of a class object, even if the
|
| 1863 |
-
|
| 1864 |
-
|
| 1865 |
-
copy/move operation as simply two
|
| 1866 |
-
|
| 1867 |
-
the times when the two objects would
|
| 1868 |
-
optimization.[^
|
| 1869 |
-
elision*, is permitted in the
|
| 1870 |
-
combined to eliminate multiple
|
|
|
|
| 1871 |
|
| 1872 |
- in a `return` statement in a function with a class return type, when
|
| 1873 |
the expression is the name of a non-volatile automatic object (other
|
| 1874 |
than a function or catch-clause parameter) with the same
|
| 1875 |
cv-unqualified type as the function return type, the copy/move
|
|
@@ -1886,14 +1907,15 @@ combined to eliminate multiple copies):
|
|
| 1886 |
same cv-unqualified type, the copy/move operation can be omitted by
|
| 1887 |
constructing the temporary object directly into the target of the
|
| 1888 |
omitted copy/move
|
| 1889 |
- when the of an exception handler (Clause [[except]]) declares an
|
| 1890 |
object of the same type (except for cv-qualification) as the exception
|
| 1891 |
-
object ([[except.throw]]), the copy
|
| 1892 |
treating the as an alias for the exception object if the meaning of
|
| 1893 |
the program will be unchanged except for the execution of constructors
|
| 1894 |
-
and destructors for the object declared by the .
|
|
|
|
| 1895 |
|
| 1896 |
``` cpp
|
| 1897 |
class Thing {
|
| 1898 |
public:
|
| 1899 |
Thing();
|
|
@@ -1917,22 +1939,25 @@ function `f()` and the copying of that temporary object into object
|
|
| 1917 |
viewed as directly initializing the global object `t2`, and that
|
| 1918 |
object’s destruction will occur at program exit. Adding a move
|
| 1919 |
constructor to `Thing` has the same effect, but it is the move
|
| 1920 |
construction from the temporary object to `t2` that is elided.
|
| 1921 |
|
| 1922 |
-
When the criteria for elision of a copy operation are met
|
| 1923 |
-
|
| 1924 |
-
|
| 1925 |
-
|
| 1926 |
-
|
| 1927 |
-
|
| 1928 |
-
|
| 1929 |
-
|
| 1930 |
-
|
| 1931 |
-
|
| 1932 |
-
|
| 1933 |
-
|
|
|
|
|
|
|
|
|
|
| 1934 |
|
| 1935 |
``` cpp
|
| 1936 |
class Thing {
|
| 1937 |
public:
|
| 1938 |
Thing();
|
|
@@ -1977,29 +2002,33 @@ as follows:
|
|
| 1977 |
The *constructor characteristics* of a constructor or constructor
|
| 1978 |
template are
|
| 1979 |
|
| 1980 |
- the template parameter list ([[temp.param]]), if any,
|
| 1981 |
- the *parameter-type-list* ([[dcl.fct]]),
|
| 1982 |
-
- the *exception-specification* ([[except.spec]]),
|
| 1983 |
- absence or presence of `explicit` ([[class.conv.ctor]]), and
|
| 1984 |
- absence or presence of `constexpr` ([[dcl.constexpr]]).
|
| 1985 |
|
| 1986 |
For each non-template constructor in the candidate set of inherited
|
| 1987 |
constructors other than a constructor having no parameters or a
|
| 1988 |
copy/move constructor having a single parameter, a constructor is
|
| 1989 |
implicitly declared with the same constructor characteristics unless
|
| 1990 |
there is a user-declared constructor with the same signature in the
|
| 1991 |
-
class where the *using-declaration* appears
|
| 1992 |
-
|
| 1993 |
-
constructor template
|
| 1994 |
-
|
| 1995 |
-
|
| 1996 |
-
|
|
|
|
|
|
|
|
|
|
| 1997 |
|
| 1998 |
A constructor so declared has the same access as the corresponding
|
| 1999 |
constructor in `X`. It is deleted if the corresponding constructor in
|
| 2000 |
-
`X` is deleted ([[dcl.fct.def]]).
|
|
|
|
|
|
|
| 2001 |
|
| 2002 |
Default and copy/move constructors may be implicitly declared as
|
| 2003 |
specified in [[class.ctor]] and [[class.copy]].
|
| 2004 |
|
| 2005 |
``` cpp
|
|
@@ -2142,10 +2171,11 @@ while writing a message to the standard log whenever an object of class
|
|
| 2142 |
[basic.stc.dynamic]: basic.md#basic.stc.dynamic
|
| 2143 |
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 2144 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 2145 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 2146 |
[basic.types]: basic.md#basic.types
|
|
|
|
| 2147 |
[class.abstract]: class.md#class.abstract
|
| 2148 |
[class.access]: class.md#class.access
|
| 2149 |
[class.base.init]: #class.base.init
|
| 2150 |
[class.cdtor]: #class.cdtor
|
| 2151 |
[class.conv]: #class.conv
|
|
@@ -2154,19 +2184,19 @@ while writing a message to the standard log whenever an object of class
|
|
| 2154 |
[class.copy]: #class.copy
|
| 2155 |
[class.ctor]: #class.ctor
|
| 2156 |
[class.dtor]: #class.dtor
|
| 2157 |
[class.expl.init]: #class.expl.init
|
| 2158 |
[class.free]: #class.free
|
|
|
|
| 2159 |
[class.inhctor]: #class.inhctor
|
| 2160 |
[class.init]: #class.init
|
| 2161 |
[class.mem]: class.md#class.mem
|
| 2162 |
[class.member.lookup]: class.md#class.member.lookup
|
| 2163 |
[class.mfct]: class.md#class.mfct
|
| 2164 |
[class.mi]: class.md#class.mi
|
| 2165 |
-
[class.
|
| 2166 |
[class.temporary]: #class.temporary
|
| 2167 |
-
[class.this]: class.md#class.this
|
| 2168 |
[class.union]: class.md#class.union
|
| 2169 |
[class.virtual]: class.md#class.virtual
|
| 2170 |
[conv]: conv.md#conv
|
| 2171 |
[conv.lval]: conv.md#conv.lval
|
| 2172 |
[dcl.array]: dcl.md#dcl.array
|
|
@@ -2181,11 +2211,11 @@ while writing a message to the standard log whenever an object of class
|
|
| 2181 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 2182 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
| 2183 |
[dcl.type.cv]: dcl.md#dcl.type.cv
|
| 2184 |
[diff.special]: compatibility.md#diff.special
|
| 2185 |
[except]: except.md#except
|
| 2186 |
-
[except.
|
| 2187 |
[except.spec]: except.md#except.spec
|
| 2188 |
[except.throw]: except.md#except.throw
|
| 2189 |
[expr]: expr.md#expr
|
| 2190 |
[expr.ass]: expr.md#expr.ass
|
| 2191 |
[expr.call]: expr.md#expr.call
|
|
@@ -2208,52 +2238,52 @@ while writing a message to the standard log whenever an object of class
|
|
| 2208 |
[over.best.ics]: over.md#over.best.ics
|
| 2209 |
[over.ics.ref]: over.md#over.ics.ref
|
| 2210 |
[over.load]: over.md#over.load
|
| 2211 |
[over.match]: over.md#over.match
|
| 2212 |
[over.match.best]: over.md#over.match.best
|
|
|
|
| 2213 |
[special]: #special
|
| 2214 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 2215 |
[stmt.return]: stmt.md#stmt.return
|
|
|
|
|
|
|
|
|
|
| 2216 |
[temp.over.link]: temp.md#temp.over.link
|
| 2217 |
[temp.param]: temp.md#temp.param
|
| 2218 |
[temp.variadic]: temp.md#temp.variadic
|
| 2219 |
|
| 2220 |
-
[^1]:
|
|
|
|
|
|
|
|
|
|
| 2221 |
purposes of overload resolution ([[over.best.ics]],
|
| 2222 |
[[over.ics.ref]]) and therefore initialization ([[dcl.init]]) and
|
| 2223 |
explicit casts ([[expr.static.cast]]). A conversion to `void` does
|
| 2224 |
not invoke any conversion function ([[expr.static.cast]]). Even
|
| 2225 |
though never directly called to perform a conversion, such
|
| 2226 |
conversion functions can be declared and can potentially be reached
|
| 2227 |
through a call to a virtual conversion function in a base class.
|
| 2228 |
|
| 2229 |
-
[^
|
| 2230 |
`operator` `delete` because [[expr.delete]] requires that in this
|
| 2231 |
situation, the static type of the object to be deleted be the same
|
| 2232 |
as its dynamic type.
|
| 2233 |
|
| 2234 |
-
[^3]: If the static type of the object to be deleted is different from
|
| 2235 |
-
the dynamic type and the destructor is not virtual the size might be
|
| 2236 |
-
incorrect, but that case is already undefined; see [[expr.delete]].
|
| 2237 |
-
|
| 2238 |
[^4]: This implies that the reference parameter of the
|
| 2239 |
implicitly-declared copy constructor cannot bind to a `volatile`
|
| 2240 |
lvalue; see [[diff.special]].
|
| 2241 |
|
| 2242 |
-
[^5]:
|
| 2243 |
-
initialization.
|
| 2244 |
-
|
| 2245 |
-
[^6]: Because a template assignment operator or an assignment operator
|
| 2246 |
taking an rvalue reference parameter is never a copy assignment
|
| 2247 |
operator, the presence of such an assignment operator does not
|
| 2248 |
suppress the implicit declaration of a copy assignment operator.
|
| 2249 |
Such assignment operators participate in overload resolution with
|
| 2250 |
other assignment operators, including copy assignment operators,
|
| 2251 |
and, if selected, will be used to assign an object.
|
| 2252 |
|
| 2253 |
-
[^
|
| 2254 |
implicitly-declared copy assignment operator cannot bind to a
|
| 2255 |
`volatile` lvalue; see [[diff.special]].
|
| 2256 |
|
| 2257 |
-
[^
|
| 2258 |
copy/move constructor is not executed, there is still one object
|
| 2259 |
destroyed for each one constructed.
|
|
|
|
| 5 |
assignment operator ([[class.copy]]), and destructor ([[class.dtor]])
|
| 6 |
are *special member functions*. The implementation will implicitly
|
| 7 |
declare these member functions for some class types when the program
|
| 8 |
does not explicitly declare them. The implementation will implicitly
|
| 9 |
define them if they are odr-used ([[basic.def.odr]]). See
|
| 10 |
+
[[class.ctor]], [[class.dtor]] and [[class.copy]]. An
|
| 11 |
+
implicitly-declared special member function is declared at the closing
|
| 12 |
+
`}` of the *class-specifier*. Programs shall not define
|
| 13 |
+
implicitly-declared special member functions.
|
| 14 |
|
| 15 |
Programs may explicitly refer to implicitly-declared special member
|
| 16 |
functions. a program may explicitly call, take the address of or form a
|
| 17 |
pointer to member to an implicitly-declared special member function.
|
| 18 |
|
|
|
|
| 34 |
|
| 35 |
Special member functions obey the usual access rules (Clause
|
| 36 |
[[class.access]]). declaring a constructor `protected` ensures that only
|
| 37 |
derived classes and friends can create objects using it.
|
| 38 |
|
| 39 |
+
For a class, its non-static data members, its non-virtual direct base
|
| 40 |
+
classes, and, if the class is not abstract ([[class.abstract]]), its
|
| 41 |
+
virtual base classes are called its *potentially constructed
|
| 42 |
+
subobjects*.
|
| 43 |
+
|
| 44 |
## Constructors <a id="class.ctor">[[class.ctor]]</a>
|
| 45 |
|
| 46 |
+
Constructors do not have names. A declaration of a constructor uses a
|
| 47 |
+
function declarator ([[dcl.fct]]) of the form
|
| 48 |
|
| 49 |
+
``` bnf
|
| 50 |
+
ptr-declarator '(' parameter-declaration-clause ')' exception-specificationₒₚₜ attribute-specifier-seqₒₚₜ
|
| 51 |
+
```
|
|
|
|
| 52 |
|
| 53 |
+
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 54 |
+
optional *attribute-specifier-seq*, and optional surrounding
|
| 55 |
+
parentheses, and the *id-expression* has one of the following forms:
|
| 56 |
+
|
| 57 |
+
- in a *member-declaration* that belongs to the *member-specification*
|
| 58 |
+
of a class but is not a friend declaration ([[class.friend]]), the
|
| 59 |
+
*id-expression* is the injected-class-name (Clause [[class]]) of the
|
| 60 |
+
immediately-enclosing class;
|
| 61 |
+
- in a *member-declaration* that belongs to the *member-specification*
|
| 62 |
+
of a class template but is not a friend declaration, the
|
| 63 |
+
*id-expression* is a *class-name* that names the current
|
| 64 |
+
instantiation ([[temp.dep.type]]) of the immediately-enclosing class
|
| 65 |
+
template; or
|
| 66 |
+
- in a declaration at namespace scope or in a friend declaration, the
|
| 67 |
+
*id-expression* is a *qualified-id* that names a constructor (
|
| 68 |
+
[[class.qual]]).
|
| 69 |
+
|
| 70 |
+
The *class-name* shall not be a *typedef-name*. In a constructor
|
| 71 |
+
declaration, each *decl-specifier* in the optional *decl-specifier-seq*
|
| 72 |
+
shall be `friend`, `inline`, `explicit`, or `constexpr`.
|
| 73 |
|
| 74 |
``` cpp
|
| 75 |
struct S {
|
| 76 |
S(); // declares the constructor
|
| 77 |
};
|
|
|
|
| 84 |
however an explicit type conversion using the functional notation (
|
| 85 |
[[expr.type.conv]]) will cause a constructor to be called to initialize
|
| 86 |
an object. For initialization of objects of class type see
|
| 87 |
[[class.init]].
|
| 88 |
|
| 89 |
+
A constructor can be invoked for a `const`, `volatile` or `const`
|
| 90 |
+
`volatile` object. `const` and `volatile` semantics ([[dcl.type.cv]])
|
| 91 |
+
are not applied on an object under construction. They come into effect
|
| 92 |
+
when the constructor for the most derived object ([[intro.object]])
|
| 93 |
+
ends.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
A *default* constructor for a class `X` is a constructor of class `X`
|
| 96 |
that can be called without an argument. If there is no user-declared
|
| 97 |
constructor for class `X`, a constructor having no parameters is
|
| 98 |
implicitly declared as defaulted ([[dcl.fct.def]]). An
|
|
|
|
| 109 |
user-provided default constructor,
|
| 110 |
- `X` is a union and all of its variant members are of const-qualified
|
| 111 |
type (or array thereof),
|
| 112 |
- `X` is a non-union class and all members of any anonymous union member
|
| 113 |
are of const-qualified type (or array thereof),
|
| 114 |
+
- any potentially constructed subobject, except for a non-static data
|
| 115 |
+
member with a *brace-or-equal-initializer*, has class type `M` (or
|
| 116 |
+
array thereof) and either `M` has no default constructor or overload
|
| 117 |
+
resolution ([[over.match]]) as applied to `M`’s default constructor
|
| 118 |
+
results in an ambiguity or in a function that is deleted or
|
| 119 |
+
inaccessible from the defaulted default constructor, or
|
| 120 |
+
- any potentially constructed subobject has a type with a destructor
|
| 121 |
+
that is deleted or inaccessible from the defaulted default
|
| 122 |
+
constructor.
|
| 123 |
|
| 124 |
A default constructor is trivial if it is not user-provided and if:
|
| 125 |
|
| 126 |
- its class has no virtual functions ([[class.virtual]]) and no virtual
|
| 127 |
base classes ([[class.mi]]), and
|
|
|
|
| 167 |
|
| 168 |
[[class.base.init]] describes the order in which constructors for base
|
| 169 |
classes and non-static data members are called and describes how
|
| 170 |
arguments can be specified for the calls to these constructors.
|
| 171 |
|
| 172 |
+
A `return` statement in the body of a constructor shall not specify a
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
return value. The address of a constructor shall not be taken.
|
| 174 |
|
| 175 |
A functional notation type conversion ([[expr.type.conv]]) can be used
|
| 176 |
to create new objects of its type. The syntax looks like an explicit
|
| 177 |
call of the constructor.
|
|
|
|
| 216 |
|
| 217 |
Temporaries of class type are created in various contexts: binding a
|
| 218 |
reference to a prvalue ([[dcl.init.ref]]), returning a prvalue (
|
| 219 |
[[stmt.return]]), a conversion that creates a prvalue ([[conv.lval]],
|
| 220 |
[[expr.static.cast]], [[expr.const.cast]], [[expr.cast]]), throwing an
|
| 221 |
+
exception ([[except.throw]]), and in some initializations (
|
| 222 |
+
[[dcl.init]]). The lifetime of exception objects is described in
|
| 223 |
+
[[except.throw]]. Even when the creation of the temporary object is
|
| 224 |
+
unevaluated (Clause [[expr]]) or otherwise avoided ([[class.copy]]),
|
| 225 |
+
all the semantic restrictions shall be respected as if the temporary
|
| 226 |
+
object had been created and later destroyed. This includes
|
| 227 |
+
accessibility ([[class.access]]) and whether it is deleted, for the
|
| 228 |
+
constructor selected and for the destructor. However, in the special
|
| 229 |
+
case of a function call used as the operand of a *decltype-specifier* (
|
| 230 |
+
[[expr.call]]), no temporary is introduced, so the foregoing does not
|
| 231 |
+
apply to the prvalue of any such function call.
|
|
|
|
|
|
|
| 232 |
|
| 233 |
Consider the following code:
|
| 234 |
|
| 235 |
``` cpp
|
| 236 |
class X {
|
|
|
|
| 291 |
default constructor is called to initialize an element of an array. If
|
| 292 |
the constructor has one or more default arguments, the destruction of
|
| 293 |
every temporary created in a default argument is sequenced before the
|
| 294 |
construction of the next array element, if any.
|
| 295 |
|
| 296 |
+
The second context is when a reference is bound to a temporary.[^1] The
|
| 297 |
temporary to which the reference is bound or the temporary that is the
|
| 298 |
complete object of a subobject to which the reference is bound persists
|
| 299 |
for the lifetime of the reference except:
|
| 300 |
|
| 301 |
- A temporary bound to a reference member in a constructor’s
|
| 302 |
+
*ctor-initializer* ([[class.base.init]]) persists until the
|
| 303 |
+
constructor exits.
|
| 304 |
- A temporary bound to a reference parameter in a function call (
|
| 305 |
[[expr.call]]) persists until the completion of the full-expression
|
| 306 |
containing the call.
|
| 307 |
- The lifetime of a temporary bound to the returned value in a function
|
| 308 |
return statement ([[stmt.return]]) is not extended; the temporary is
|
|
|
|
| 351 |
temporary `T1` to hold the result of the expression `S(16)`, a second
|
| 352 |
temporary `T2` to hold the result of the expression `S(23)`, and a third
|
| 353 |
temporary `T3` to hold the result of the addition of these two
|
| 354 |
expressions. The temporary `T3` is then bound to the reference `cr`. It
|
| 355 |
is unspecified whether `T1` or `T2` is created first. On an
|
| 356 |
+
implementation where `T1` is created before `T2`, `T2` shall be
|
| 357 |
+
destroyed before `T1`. The temporaries `T1` and `T2` are bound to the
|
| 358 |
+
reference parameters of `operator+`; these temporaries are destroyed at
|
| 359 |
+
the end of the full-expression containing the call to `operator+`. The
|
| 360 |
+
temporary `T3` bound to the reference `cr` is destroyed at the end of
|
| 361 |
+
`cr`’s lifetime, that is, at the end of the program. In addition, the
|
| 362 |
+
order in which `T3` is destroyed takes into account the destruction
|
| 363 |
+
order of other objects with static storage duration. That is, because
|
| 364 |
+
`obj1` is constructed before `T3`, and `T3` is constructed before
|
| 365 |
+
`obj2`, `obj2` shall be destroyed before `T3`, and `T3` shall be
|
| 366 |
+
destroyed before `obj1`.
|
| 367 |
|
| 368 |
## Conversions <a id="class.conv">[[class.conv]]</a>
|
| 369 |
|
| 370 |
Type conversions of class objects can be specified by constructors and
|
| 371 |
by conversion functions. These conversions are called *user-defined
|
|
|
|
| 429 |
|
| 430 |
``` cpp
|
| 431 |
struct X {
|
| 432 |
X(int);
|
| 433 |
X(const char*, int =0);
|
| 434 |
+
X(int, int);
|
| 435 |
};
|
| 436 |
|
| 437 |
void f(X arg) {
|
| 438 |
X a = 1; // a = X(1)
|
| 439 |
X b = "Jessie"; // b = X("Jessie",0)
|
| 440 |
a = 2; // a = X(2)
|
| 441 |
f(3); // f(X(3))
|
| 442 |
+
f({1, 2}); // f(X(1,2))
|
| 443 |
}
|
| 444 |
```
|
| 445 |
|
| 446 |
An explicit constructor constructs objects just like non-explicit
|
| 447 |
constructors, but does so only where the direct-initialization syntax (
|
|
|
|
| 452 |
|
| 453 |
``` cpp
|
| 454 |
struct Z {
|
| 455 |
explicit Z();
|
| 456 |
explicit Z(int);
|
| 457 |
+
explicit Z(int, int);
|
| 458 |
};
|
| 459 |
|
| 460 |
Z a; // OK: default-initialization performed
|
| 461 |
Z a1 = 1; // error: no implicit conversion
|
| 462 |
Z a3 = Z(1); // OK: direct initialization syntax used
|
| 463 |
Z a2(1); // OK: direct initialization syntax used
|
| 464 |
Z* p = new Z(1); // OK: direct initialization syntax used
|
| 465 |
Z a4 = (Z)1; // OK: explicit cast used
|
| 466 |
Z a5 = static_cast<Z>(1); // OK: explicit cast used
|
| 467 |
+
Z a6 = { 3, 4 }; // error: no implicit conversion
|
| 468 |
```
|
| 469 |
|
| 470 |
A non-explicit copy/move constructor ([[class.copy]]) is a converting
|
| 471 |
constructor. An implicitly-declared copy/move constructor is not an
|
| 472 |
explicit constructor; it may be called for implicit type conversions.
|
|
|
|
| 497 |
function, the type of the conversion function ([[dcl.fct]]) is
|
| 498 |
“function taking no parameter returning *conversion-type-id*”. A
|
| 499 |
conversion function is never used to convert a (possibly cv-qualified)
|
| 500 |
object to the (possibly cv-qualified) same object type (or a reference
|
| 501 |
to it), to a (possibly cv-qualified) base class of that type (or a
|
| 502 |
+
reference to it), or to (possibly cv-qualified) void.[^2]
|
| 503 |
|
| 504 |
``` cpp
|
| 505 |
struct X {
|
| 506 |
operator int();
|
| 507 |
};
|
|
|
|
| 562 |
|
| 563 |
Conversion functions cannot be declared `static`.
|
| 564 |
|
| 565 |
## Destructors <a id="class.dtor">[[class.dtor]]</a>
|
| 566 |
|
| 567 |
+
A declaration of a destructor uses a function declarator ([[dcl.fct]])
|
| 568 |
+
of the form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 569 |
|
| 570 |
+
``` bnf
|
| 571 |
+
ptr-declarator '(' parameter-declaration-clause ')' exception-specificationₒₚₜ attribute-specifier-seqₒₚₜ
|
| 572 |
+
```
|
| 573 |
+
|
| 574 |
+
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 575 |
+
optional *attribute-specifier-seq*, and optional surrounding
|
| 576 |
+
parentheses, and the *id-expression* has one of the following forms:
|
| 577 |
+
|
| 578 |
+
- in a *member-declaration* that belongs to the *member-specification*
|
| 579 |
+
of a class but is not a friend declaration ([[class.friend]]), the
|
| 580 |
+
*id-expression* is `~`*class-name* and the *class-name* is the
|
| 581 |
+
injected-class-name (Clause [[class]]) of the immediately-enclosing
|
| 582 |
+
class;
|
| 583 |
+
- in a *member-declaration* that belongs to the *member-specification*
|
| 584 |
+
of a class template but is not a friend declaration, the
|
| 585 |
+
*id-expression* is `~`*class-name* and the *class-name* names the
|
| 586 |
+
current instantiation ([[temp.dep.type]]) of the
|
| 587 |
+
immediately-enclosing class template; or
|
| 588 |
+
- in a declaration at namespace scope or in a friend declaration, the
|
| 589 |
+
*id-expression* is *nested-name-specifier* `~`*class-name* and the
|
| 590 |
+
*class-name* names the same class as the *nested-name-specifier*.
|
| 591 |
+
|
| 592 |
+
The *class-name* shall not be a *typedef-name*. A destructor shall take
|
| 593 |
+
no arguments ([[dcl.fct]]). In a destructor declaration, each
|
| 594 |
+
*decl-specifier* of the optional *decl-specifier-seq* shall be `friend`,
|
| 595 |
+
`inline`, or `virtual`.
|
| 596 |
+
|
| 597 |
+
A destructor is used to destroy objects of its class type. The address
|
| 598 |
+
of a destructor shall not be taken. A destructor can be invoked for a
|
| 599 |
+
`const`, `volatile` or `const` `volatile` object. `const` and `volatile`
|
| 600 |
+
semantics ([[dcl.type.cv]]) are not applied on an object under
|
| 601 |
+
destruction. They stop being in effect when the destructor for the most
|
| 602 |
+
derived object ([[intro.object]]) starts.
|
| 603 |
|
| 604 |
A declaration of a destructor that does not have an
|
| 605 |
*exception-specification* is implicitly considered to have the same
|
| 606 |
*exception-specification* as an implicit declaration ([[except.spec]]).
|
| 607 |
|
|
|
|
| 611 |
|
| 612 |
A defaulted destructor for a class `X` is defined as deleted if:
|
| 613 |
|
| 614 |
- `X` is a union-like class that has a variant member with a non-trivial
|
| 615 |
destructor,
|
| 616 |
+
- any potentially constructed subobject has class type `M` (or array
|
| 617 |
thereof) and `M` has a deleted destructor or a destructor that is
|
| 618 |
inaccessible from the defaulted destructor,
|
|
|
|
|
|
|
| 619 |
- or, for a virtual destructor, lookup of the non-array deallocation
|
| 620 |
function results in an ambiguity or in a function that is deleted or
|
| 621 |
inaccessible from the defaulted destructor.
|
| 622 |
|
| 623 |
A destructor is trivial if it is not user-provided and if:
|
|
|
|
| 661 |
destructor (whether user- or implicitly-declared) is virtual.
|
| 662 |
|
| 663 |
some language constructs have special semantics when used during
|
| 664 |
destruction; see [[class.cdtor]].
|
| 665 |
|
| 666 |
+
A destructor is invoked implicitly
|
| 667 |
|
| 668 |
+
- for a constructed object with static storage duration (
|
| 669 |
[[basic.stc.static]]) at program termination ([[basic.start.term]]),
|
| 670 |
+
- for a constructed object with thread storage duration (
|
| 671 |
[[basic.stc.thread]]) at thread exit,
|
| 672 |
+
- for a constructed object with automatic storage duration (
|
| 673 |
[[basic.stc.auto]]) when the block in which an object is created
|
| 674 |
exits ([[stmt.dcl]]),
|
| 675 |
+
- for a constructed temporary object when its lifetime ends (
|
| 676 |
+
[[class.temporary]]).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 677 |
|
| 678 |
+
In each case, the context of the invocation is the context of the
|
| 679 |
+
construction of the object. A destructor is also invoked implicitly
|
| 680 |
+
through use of a *delete-expression* ([[expr.delete]]) for a
|
| 681 |
+
constructed object allocated by a *new-expression* ([[expr.new]]); the
|
| 682 |
+
context of the invocation is the *delete-expression*. An array of class
|
| 683 |
+
type contains several subobjects for each of which the destructor is
|
| 684 |
+
invoked. A destructor can also be invoked explicitly. A destructor is
|
| 685 |
+
*potentially invoked* if it is invoked or as specified in [[expr.new]]
|
| 686 |
+
and [[class.base.init]]. A program is ill-formed if a destructor that
|
| 687 |
+
is potentially invoked is deleted or not accessible from the context of
|
| 688 |
+
the invocation.
|
| 689 |
|
| 690 |
At the point of definition of a virtual destructor (including an
|
| 691 |
implicit definition ([[class.copy]])), the non-array deallocation
|
| 692 |
function is looked up in the scope of the destructor’s class (
|
| 693 |
[[class.member.lookup]]), and, if no declaration is found, the function
|
|
|
|
| 699 |
available for the *delete-expression* ([[class.free]]).
|
| 700 |
|
| 701 |
In an explicit destructor call, the destructor name appears as a `~`
|
| 702 |
followed by a *type-name* or *decltype-specifier* that denotes the
|
| 703 |
destructor’s class type. The invocation of a destructor is subject to
|
| 704 |
+
the usual rules for member functions ([[class.mfct]]); that is, if the
|
| 705 |
object is not of the destructor’s class type and not of a class derived
|
| 706 |
+
from the destructor’s class type (including when the destructor is
|
| 707 |
+
invoked via a null pointer value), the program has undefined behavior.
|
| 708 |
+
invoking `delete` on a null pointer does not call the destructor; see
|
| 709 |
+
[[expr.delete]].
|
| 710 |
|
| 711 |
``` cpp
|
| 712 |
struct B {
|
| 713 |
virtual ~B() { }
|
| 714 |
};
|
|
|
|
| 798 |
When an object is deleted with a *delete-expression* ([[expr.delete]]),
|
| 799 |
a *deallocation function* (`operator delete()` for non-array objects or
|
| 800 |
`operator delete[]()` for arrays) is (implicitly) called to reclaim the
|
| 801 |
storage occupied by the object ([[basic.stc.dynamic.deallocation]]).
|
| 802 |
|
| 803 |
+
Class-specific deallocation function lookup is a part of general
|
| 804 |
+
deallocation function lookup ([[expr.delete]]) and occurs as follows.
|
| 805 |
+
If the *delete-expression* is used to deallocate a class object whose
|
| 806 |
static type has a virtual destructor, the deallocation function is the
|
| 807 |
one selected at the point of definition of the dynamic type’s virtual
|
| 808 |
+
destructor ([[class.dtor]]).[^3] Otherwise, if the *delete-expression*
|
| 809 |
is used to deallocate an object of class `T` or array thereof, the
|
| 810 |
static and dynamic types of the object shall be identical and the
|
| 811 |
deallocation function’s name is looked up in the scope of `T`. If this
|
| 812 |
+
lookup fails to find the name, general deallocation function lookup (
|
| 813 |
+
[[expr.delete]]) continues. If the result of the lookup is ambiguous or
|
| 814 |
+
inaccessible, or if the lookup selects a placement deallocation
|
| 815 |
+
function, the program is ill-formed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 816 |
|
| 817 |
Any deallocation function for a class `X` is a static member (even if
|
| 818 |
not explicitly declared `static`).
|
| 819 |
|
| 820 |
``` cpp
|
|
|
|
| 931 |
complex e; // initialize by a call of
|
| 932 |
// complex()
|
| 933 |
complex f = 3; // construct complex(3) using
|
| 934 |
// complex(double)
|
| 935 |
// copy/move it into f
|
| 936 |
+
complex g = { 1, 2 }; // initialize by a call of
|
| 937 |
+
// complex(double, double)
|
|
|
|
| 938 |
```
|
| 939 |
|
| 940 |
overloading of the assignment operator ([[over.ass]]) has no effect on
|
| 941 |
initialization.
|
| 942 |
|
|
|
|
| 987 |
```
|
| 988 |
|
| 989 |
``` bnf
|
| 990 |
mem-initializer-list:
|
| 991 |
mem-initializer '...'ₒₚₜ
|
| 992 |
+
mem-initializer '...'ₒₚₜ ',' mem-initializer-list
|
| 993 |
```
|
| 994 |
|
| 995 |
``` bnf
|
| 996 |
mem-initializer:
|
| 997 |
mem-initializer-id '(' expression-listₒₚₜ ')'
|
|
|
|
| 1089 |
part of the full-expression that performs the initialization. A
|
| 1090 |
*mem-initializer* where the *mem-initializer-id* denotes a virtual base
|
| 1091 |
class is ignored during execution of a constructor of any class that is
|
| 1092 |
not the most derived class.
|
| 1093 |
|
| 1094 |
+
In a non-delegating constructor, if a given potentially constructed
|
| 1095 |
+
subobject is not designated by a *mem-initializer-id* (including the
|
| 1096 |
case where there is no *mem-initializer-list* because the constructor
|
| 1097 |
+
has no *ctor-initializer*), then
|
|
|
|
| 1098 |
|
| 1099 |
- if the entity is a non-static data member that has a
|
| 1100 |
+
*brace-or-equal-initializer* and either
|
| 1101 |
+
- the constructor’s class is a union ([[class.union]]), and no other
|
| 1102 |
+
variant member of that union is designated by a *mem-initializer-id*
|
| 1103 |
+
or
|
| 1104 |
+
- the constructor’s class is not a union, and, if the entity is a
|
| 1105 |
+
member of an anonymous union, no other member of that union is
|
| 1106 |
+
designated by a *mem-initializer-id*,
|
| 1107 |
+
|
| 1108 |
+
the entity is initialized as specified in [[dcl.init]];
|
| 1109 |
+
- otherwise, if the entity is an anonymous union or a variant member (
|
| 1110 |
+
[[class.union]]), no initialization is performed;
|
| 1111 |
- otherwise, the entity is default-initialized ([[dcl.init]]).
|
| 1112 |
|
| 1113 |
An abstract class ([[class.abstract]]) is never a most derived class,
|
| 1114 |
thus its constructors never initialize virtual base classes, therefore
|
| 1115 |
the corresponding *mem-initializer*s may be omitted. An attempt to
|
| 1116 |
initialize more than one non-static data member of a union renders the
|
| 1117 |
+
program ill-formed. After the call to a constructor for class `X` for an
|
| 1118 |
+
object with automatic or dynamic storage duration has completed, if the
|
| 1119 |
+
constructor was not invoked as part of value-initialization and a member
|
| 1120 |
+
of `X` is neither initialized nor given a value during execution of the
|
| 1121 |
+
*compound-statement* of the body of the constructor, the member has an
|
| 1122 |
+
indeterminate value.
|
| 1123 |
|
| 1124 |
``` cpp
|
| 1125 |
struct A {
|
| 1126 |
A();
|
| 1127 |
};
|
|
|
|
| 1154 |
|
| 1155 |
the `A(int)` constructor will simply initialize `i` to the value of
|
| 1156 |
`arg`, and the side effects in `i`’s *brace-or-equal-initializer* will
|
| 1157 |
not take place.
|
| 1158 |
|
| 1159 |
+
In a non-delegating constructor, the destructor for each potentially
|
| 1160 |
+
constructed subobject of class type is potentially invoked (
|
| 1161 |
+
[[class.dtor]]). This provision ensures that destructors can be called
|
| 1162 |
+
for fully-constructed sub-objects in case an exception is thrown (
|
| 1163 |
+
[[except.ctor]]).
|
| 1164 |
+
|
| 1165 |
In a non-delegating constructor, initialization proceeds in the
|
| 1166 |
following order:
|
| 1167 |
|
| 1168 |
- First, and only for the constructor of the most derived class (
|
| 1169 |
[[intro.object]]), virtual base classes are initialized in the order
|
|
|
|
| 1551 |
one is declared *implicitly*. If the class definition declares a move
|
| 1552 |
constructor or move assignment operator, the implicitly declared copy
|
| 1553 |
constructor is defined as deleted; otherwise, it is defined as
|
| 1554 |
defaulted ([[dcl.fct.def]]). The latter case is deprecated if the class
|
| 1555 |
has a user-declared copy assignment operator or a user-declared
|
| 1556 |
+
destructor.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1557 |
|
| 1558 |
The implicitly-declared copy constructor for a class `X` will have the
|
| 1559 |
form
|
| 1560 |
|
| 1561 |
``` cpp
|
| 1562 |
X::X(const X&)
|
| 1563 |
```
|
| 1564 |
|
| 1565 |
+
if each potentially constructed subobject of a class type `M` (or array
|
| 1566 |
+
thereof) has a copy constructor whose first parameter is of type `const`
|
| 1567 |
+
`M&` or `const` `volatile` `M&`.[^4] Otherwise, the implicitly-declared
|
| 1568 |
+
copy constructor will have the form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1569 |
|
| 1570 |
``` cpp
|
| 1571 |
X::X(X&)
|
| 1572 |
```
|
| 1573 |
|
| 1574 |
If the definition of a class `X` does not explicitly declare a move
|
| 1575 |
constructor, one will be implicitly declared as defaulted if and only if
|
| 1576 |
|
| 1577 |
- `X` does not have a user-declared copy constructor,
|
| 1578 |
- `X` does not have a user-declared copy assignment operator,
|
| 1579 |
+
- `X` does not have a user-declared move assignment operator, and
|
| 1580 |
+
- `X` does not have a user-declared destructor.
|
|
|
|
| 1581 |
|
| 1582 |
When the move constructor is not implicitly declared or explicitly
|
| 1583 |
supplied, expressions that otherwise would have invoked the move
|
| 1584 |
constructor may instead invoke a copy constructor.
|
| 1585 |
|
|
|
|
| 1594 |
member of its class. A defaulted copy/move constructor for a class `X`
|
| 1595 |
is defined as deleted ([[dcl.fct.def.delete]]) if `X` has:
|
| 1596 |
|
| 1597 |
- a variant member with a non-trivial corresponding constructor and `X`
|
| 1598 |
is a union-like class,
|
| 1599 |
+
- a potentially constructed subobject type `M` (or array thereof) that
|
| 1600 |
cannot be copied/moved because overload resolution ([[over.match]]),
|
| 1601 |
as applied to `M`’s corresponding constructor, results in an ambiguity
|
| 1602 |
or a function that is deleted or inaccessible from the defaulted
|
| 1603 |
constructor,
|
| 1604 |
+
- any potentially constructed subobject of a type with a destructor that
|
| 1605 |
+
is deleted or inaccessible from the defaulted constructor, or,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1606 |
- for the copy constructor, a non-static data member of rvalue reference
|
| 1607 |
+
type.
|
| 1608 |
+
|
| 1609 |
+
A defaulted move constructor that is defined as deleted is ignored by
|
| 1610 |
+
overload resolution ([[over.match]], [[over.over]]). A deleted move
|
| 1611 |
+
constructor would otherwise interfere with initialization from an rvalue
|
| 1612 |
+
which can use the copy constructor instead.
|
| 1613 |
|
| 1614 |
A copy/move constructor for class `X` is trivial if it is not
|
| 1615 |
+
user-provided, its parameter-type-list is equivalent to the
|
| 1616 |
+
parameter-type-list of an implicit declaration, and if
|
| 1617 |
|
| 1618 |
- class `X` has no virtual functions ([[class.virtual]]) and no virtual
|
| 1619 |
base classes ([[class.mi]]), and
|
| 1620 |
+
- class `X` has no non-static data members of volatile-qualified type,
|
| 1621 |
+
and
|
| 1622 |
- the constructor selected to copy/move each direct base class subobject
|
| 1623 |
is trivial, and
|
| 1624 |
- for each non-static data member of `X` that is of class type (or array
|
| 1625 |
thereof), the constructor selected to copy/move that member is
|
| 1626 |
trivial;
|
| 1627 |
|
| 1628 |
otherwise the copy/move constructor is *non-trivial*.
|
| 1629 |
|
| 1630 |
A copy/move constructor that is defaulted and not defined as deleted is
|
| 1631 |
+
*implicitly defined* if it is odr-used ([[basic.def.odr]]) or when it
|
|
|
|
|
|
|
| 1632 |
is explicitly defaulted after its first declaration. The copy/move
|
| 1633 |
constructor is implicitly defined even if the implementation elided its
|
| 1634 |
odr-use ([[basic.def.odr]], [[class.temporary]]). If the
|
| 1635 |
implicitly-defined constructor would satisfy the requirements of a
|
| 1636 |
`constexpr` constructor ([[dcl.constexpr]]), the implicitly-defined
|
| 1637 |
constructor is `constexpr`.
|
| 1638 |
|
| 1639 |
Before the defaulted copy/move constructor for a class is implicitly
|
| 1640 |
+
defined, all non-user-provided copy/move constructors for its
|
| 1641 |
+
potentially constructed subobjects shall have been implicitly defined.
|
| 1642 |
+
An implicitly-declared copy/move constructor has an
|
| 1643 |
*exception-specification* ([[except.spec]]).
|
| 1644 |
|
| 1645 |
The implicitly-defined copy/move constructor for a non-union class `X`
|
| 1646 |
performs a memberwise copy/move of its bases and members.
|
| 1647 |
*brace-or-equal-initializer*s of non-static data members are ignored.
|
|
|
|
| 1667 |
object representation ([[basic.types]]) of `X`.
|
| 1668 |
|
| 1669 |
A user-declared *copy* assignment operator `X::operator=` is a
|
| 1670 |
non-static non-template member function of class `X` with exactly one
|
| 1671 |
parameter of type `X`, `X&`, `const` `X&`, `volatile` `X&` or `const`
|
| 1672 |
+
`volatile` `X&`.[^5] An overloaded assignment operator must be declared
|
| 1673 |
to have only one parameter; see [[over.ass]]. More than one form of
|
| 1674 |
copy assignment operator may be declared for a class. If a class `X`
|
| 1675 |
only has a copy assignment operator with a parameter of type `X&`, an
|
| 1676 |
expression of type const `X` cannot be assigned to an object of type
|
| 1677 |
`X`.
|
|
|
|
| 1706 |
- each direct base class `B` of `X` has a copy assignment operator whose
|
| 1707 |
parameter is of type `const` `B&`, `const` `volatile` `B&` or `B`, and
|
| 1708 |
- for all the non-static data members of `X` that are of a class type
|
| 1709 |
`M` (or array thereof), each such class type has a copy assignment
|
| 1710 |
operator whose parameter is of type `const` `M&`, `const` `volatile`
|
| 1711 |
+
`M&` or `M`.[^6]
|
| 1712 |
|
| 1713 |
Otherwise, the implicitly-declared copy assignment operator will have
|
| 1714 |
the form
|
| 1715 |
|
| 1716 |
``` cpp
|
|
|
|
| 1728 |
assignment operator, one will be implicitly declared as defaulted if and
|
| 1729 |
only if
|
| 1730 |
|
| 1731 |
- `X` does not have a user-declared copy constructor,
|
| 1732 |
- `X` does not have a user-declared move constructor,
|
| 1733 |
+
- `X` does not have a user-declared copy assignment operator, and
|
| 1734 |
+
- `X` does not have a user-declared destructor.
|
|
|
|
|
|
|
| 1735 |
|
| 1736 |
The class definition
|
| 1737 |
|
| 1738 |
``` cpp
|
| 1739 |
struct S {
|
|
|
|
| 1773 |
- a variant member with a non-trivial corresponding assignment operator
|
| 1774 |
and `X` is a union-like class, or
|
| 1775 |
- a non-static data member of `const` non-class type (or array thereof),
|
| 1776 |
or
|
| 1777 |
- a non-static data member of reference type, or
|
| 1778 |
+
- a potentially constructed subobject of class type `M` (or array
|
| 1779 |
+
thereof) that cannot be copied/moved because overload resolution (
|
| 1780 |
+
[[over.match]]), as applied to `M`’s corresponding assignment
|
| 1781 |
+
operator, results in an ambiguity or a function that is deleted or
|
| 1782 |
+
inaccessible from the defaulted assignment operator.
|
| 1783 |
+
|
| 1784 |
+
A defaulted move assignment operator that is defined as deleted is
|
| 1785 |
+
ignored by overload resolution ([[over.match]], [[over.over]]).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1786 |
|
| 1787 |
Because a copy/move assignment operator is implicitly declared for a
|
| 1788 |
class if not declared by the user, a base class copy/move assignment
|
| 1789 |
operator is always hidden by the corresponding assignment operator of a
|
| 1790 |
derived class ([[over.ass]]). A *using-declaration* (
|
|
|
|
| 1795 |
declaration of the derived class operator; the operator introduced by
|
| 1796 |
the *using-declaration* is hidden by the implicitly-declared operator in
|
| 1797 |
the derived class.
|
| 1798 |
|
| 1799 |
A copy/move assignment operator for class `X` is trivial if it is not
|
| 1800 |
+
user-provided, its parameter-type-list is equivalent to the
|
| 1801 |
+
parameter-type-list of an implicit declaration, and if
|
| 1802 |
|
| 1803 |
- class `X` has no virtual functions ([[class.virtual]]) and no virtual
|
| 1804 |
base classes ([[class.mi]]), and
|
| 1805 |
+
- class `X` has no non-static data members of volatile-qualified type,
|
| 1806 |
+
and
|
| 1807 |
- the assignment operator selected to copy/move each direct base class
|
| 1808 |
subobject is trivial, and
|
| 1809 |
- for each non-static data member of `X` that is of class type (or array
|
| 1810 |
thereof), the assignment operator selected to copy/move that member is
|
| 1811 |
trivial;
|
| 1812 |
|
| 1813 |
otherwise the copy/move assignment operator is *non-trivial*.
|
| 1814 |
|
| 1815 |
+
A copy/move assignment operator for a class `X` that is defaulted and
|
| 1816 |
+
not defined as deleted is *implicitly defined* when it is odr-used (
|
| 1817 |
+
[[basic.def.odr]]) (e.g., when it is selected by overload resolution to
|
| 1818 |
+
assign to an object of its class type) or when it is explicitly
|
| 1819 |
+
defaulted after its first declaration. The implicitly-defined copy/move
|
| 1820 |
+
assignment operator is `constexpr` if
|
| 1821 |
+
|
| 1822 |
+
- `X` is a literal type, and
|
| 1823 |
+
- the assignment operator selected to copy/move each direct base class
|
| 1824 |
+
subobject is a `constexpr` function, and
|
| 1825 |
+
- for each non-static data member of `X` that is of class type (or array
|
| 1826 |
+
thereof), the assignment operator selected to copy/move that member is
|
| 1827 |
+
a `constexpr` function.
|
| 1828 |
|
| 1829 |
Before the defaulted copy/move assignment operator for a class is
|
| 1830 |
implicitly defined, all non-user-provided copy/move assignment operators
|
| 1831 |
for its direct base classes and its non-static data members shall have
|
| 1832 |
been implicitly defined. An implicitly-declared copy/move assignment
|
|
|
|
| 1877 |
Copying/moving one object into another using the copy/move constructor
|
| 1878 |
or the copy/move assignment operator does not change the layout or size
|
| 1879 |
of either object.
|
| 1880 |
|
| 1881 |
When certain criteria are met, an implementation is allowed to omit the
|
| 1882 |
+
copy/move construction of a class object, even if the constructor
|
| 1883 |
+
selected for the copy/move operation and/or the destructor for the
|
| 1884 |
+
object have side effects. In such cases, the implementation treats the
|
| 1885 |
+
source and target of the omitted copy/move operation as simply two
|
| 1886 |
+
different ways of referring to the same object, and the destruction of
|
| 1887 |
+
that object occurs at the later of the times when the two objects would
|
| 1888 |
+
have been destroyed without the optimization.[^7] This elision of
|
| 1889 |
+
copy/move operations, called *copy elision*, is permitted in the
|
| 1890 |
+
following circumstances (which may be combined to eliminate multiple
|
| 1891 |
+
copies):
|
| 1892 |
|
| 1893 |
- in a `return` statement in a function with a class return type, when
|
| 1894 |
the expression is the name of a non-volatile automatic object (other
|
| 1895 |
than a function or catch-clause parameter) with the same
|
| 1896 |
cv-unqualified type as the function return type, the copy/move
|
|
|
|
| 1907 |
same cv-unqualified type, the copy/move operation can be omitted by
|
| 1908 |
constructing the temporary object directly into the target of the
|
| 1909 |
omitted copy/move
|
| 1910 |
- when the of an exception handler (Clause [[except]]) declares an
|
| 1911 |
object of the same type (except for cv-qualification) as the exception
|
| 1912 |
+
object ([[except.throw]]), the copy operation can be omitted by
|
| 1913 |
treating the as an alias for the exception object if the meaning of
|
| 1914 |
the program will be unchanged except for the execution of constructors
|
| 1915 |
+
and destructors for the object declared by the . There cannot be a
|
| 1916 |
+
move from the exception object because it is always an lvalue.
|
| 1917 |
|
| 1918 |
``` cpp
|
| 1919 |
class Thing {
|
| 1920 |
public:
|
| 1921 |
Thing();
|
|
|
|
| 1939 |
viewed as directly initializing the global object `t2`, and that
|
| 1940 |
object’s destruction will occur at program exit. Adding a move
|
| 1941 |
constructor to `Thing` has the same effect, but it is the move
|
| 1942 |
construction from the temporary object to `t2` that is elided.
|
| 1943 |
|
| 1944 |
+
When the criteria for elision of a copy/move operation are met, but not
|
| 1945 |
+
for an , and the object to be copied is designated by an lvalue, or when
|
| 1946 |
+
the *expression* in a `return` statement is a (possibly parenthesized)
|
| 1947 |
+
*id-expression* that names an object with automatic storage duration
|
| 1948 |
+
declared in the body or *parameter-declaration-clause* of the innermost
|
| 1949 |
+
enclosing function or *lambda-expression*, overload resolution to select
|
| 1950 |
+
the constructor for the copy is first performed as if the object were
|
| 1951 |
+
designated by an rvalue. If the first overload resolution fails or was
|
| 1952 |
+
not performed, or if the type of the first parameter of the selected
|
| 1953 |
+
constructor is not an rvalue reference to the object’s type (possibly
|
| 1954 |
+
cv-qualified), overload resolution is performed again, considering the
|
| 1955 |
+
object as an lvalue. This two-stage overload resolution must be
|
| 1956 |
+
performed regardless of whether copy elision will occur. It determines
|
| 1957 |
+
the constructor to be called if elision is not performed, and the
|
| 1958 |
+
selected constructor must be accessible even if the call is elided.
|
| 1959 |
|
| 1960 |
``` cpp
|
| 1961 |
class Thing {
|
| 1962 |
public:
|
| 1963 |
Thing();
|
|
|
|
| 2002 |
The *constructor characteristics* of a constructor or constructor
|
| 2003 |
template are
|
| 2004 |
|
| 2005 |
- the template parameter list ([[temp.param]]), if any,
|
| 2006 |
- the *parameter-type-list* ([[dcl.fct]]),
|
|
|
|
| 2007 |
- absence or presence of `explicit` ([[class.conv.ctor]]), and
|
| 2008 |
- absence or presence of `constexpr` ([[dcl.constexpr]]).
|
| 2009 |
|
| 2010 |
For each non-template constructor in the candidate set of inherited
|
| 2011 |
constructors other than a constructor having no parameters or a
|
| 2012 |
copy/move constructor having a single parameter, a constructor is
|
| 2013 |
implicitly declared with the same constructor characteristics unless
|
| 2014 |
there is a user-declared constructor with the same signature in the
|
| 2015 |
+
complete class where the *using-declaration* appears or the constructor
|
| 2016 |
+
would be a default, copy, or move constructor for that class. Similarly,
|
| 2017 |
+
for each constructor template in the candidate set of inherited
|
| 2018 |
+
constructors, a constructor template is implicitly declared with the
|
| 2019 |
+
same constructor characteristics unless there is an equivalent
|
| 2020 |
+
user-declared constructor template ([[temp.over.link]]) in the complete
|
| 2021 |
+
class where the *using-declaration* appears. Default arguments are not
|
| 2022 |
+
inherited. An *exception-specification* is implied as specified in
|
| 2023 |
+
[[except.spec]].
|
| 2024 |
|
| 2025 |
A constructor so declared has the same access as the corresponding
|
| 2026 |
constructor in `X`. It is deleted if the corresponding constructor in
|
| 2027 |
+
`X` is deleted ([[dcl.fct.def]]). An inheriting constructor shall not
|
| 2028 |
+
be explicitly instantiated ([[temp.explicit]]) or explicitly
|
| 2029 |
+
specialized ([[temp.expl.spec]]).
|
| 2030 |
|
| 2031 |
Default and copy/move constructors may be implicitly declared as
|
| 2032 |
specified in [[class.ctor]] and [[class.copy]].
|
| 2033 |
|
| 2034 |
``` cpp
|
|
|
|
| 2171 |
[basic.stc.dynamic]: basic.md#basic.stc.dynamic
|
| 2172 |
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 2173 |
[basic.stc.static]: basic.md#basic.stc.static
|
| 2174 |
[basic.stc.thread]: basic.md#basic.stc.thread
|
| 2175 |
[basic.types]: basic.md#basic.types
|
| 2176 |
+
[class]: class.md#class
|
| 2177 |
[class.abstract]: class.md#class.abstract
|
| 2178 |
[class.access]: class.md#class.access
|
| 2179 |
[class.base.init]: #class.base.init
|
| 2180 |
[class.cdtor]: #class.cdtor
|
| 2181 |
[class.conv]: #class.conv
|
|
|
|
| 2184 |
[class.copy]: #class.copy
|
| 2185 |
[class.ctor]: #class.ctor
|
| 2186 |
[class.dtor]: #class.dtor
|
| 2187 |
[class.expl.init]: #class.expl.init
|
| 2188 |
[class.free]: #class.free
|
| 2189 |
+
[class.friend]: class.md#class.friend
|
| 2190 |
[class.inhctor]: #class.inhctor
|
| 2191 |
[class.init]: #class.init
|
| 2192 |
[class.mem]: class.md#class.mem
|
| 2193 |
[class.member.lookup]: class.md#class.member.lookup
|
| 2194 |
[class.mfct]: class.md#class.mfct
|
| 2195 |
[class.mi]: class.md#class.mi
|
| 2196 |
+
[class.qual]: basic.md#class.qual
|
| 2197 |
[class.temporary]: #class.temporary
|
|
|
|
| 2198 |
[class.union]: class.md#class.union
|
| 2199 |
[class.virtual]: class.md#class.virtual
|
| 2200 |
[conv]: conv.md#conv
|
| 2201 |
[conv.lval]: conv.md#conv.lval
|
| 2202 |
[dcl.array]: dcl.md#dcl.array
|
|
|
|
| 2211 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 2212 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
| 2213 |
[dcl.type.cv]: dcl.md#dcl.type.cv
|
| 2214 |
[diff.special]: compatibility.md#diff.special
|
| 2215 |
[except]: except.md#except
|
| 2216 |
+
[except.ctor]: except.md#except.ctor
|
| 2217 |
[except.spec]: except.md#except.spec
|
| 2218 |
[except.throw]: except.md#except.throw
|
| 2219 |
[expr]: expr.md#expr
|
| 2220 |
[expr.ass]: expr.md#expr.ass
|
| 2221 |
[expr.call]: expr.md#expr.call
|
|
|
|
| 2238 |
[over.best.ics]: over.md#over.best.ics
|
| 2239 |
[over.ics.ref]: over.md#over.ics.ref
|
| 2240 |
[over.load]: over.md#over.load
|
| 2241 |
[over.match]: over.md#over.match
|
| 2242 |
[over.match.best]: over.md#over.match.best
|
| 2243 |
+
[over.over]: over.md#over.over
|
| 2244 |
[special]: #special
|
| 2245 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 2246 |
[stmt.return]: stmt.md#stmt.return
|
| 2247 |
+
[temp.dep.type]: temp.md#temp.dep.type
|
| 2248 |
+
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 2249 |
+
[temp.explicit]: temp.md#temp.explicit
|
| 2250 |
[temp.over.link]: temp.md#temp.over.link
|
| 2251 |
[temp.param]: temp.md#temp.param
|
| 2252 |
[temp.variadic]: temp.md#temp.variadic
|
| 2253 |
|
| 2254 |
+
[^1]: The same rules apply to initialization of an `initializer_list`
|
| 2255 |
+
object ([[dcl.init.list]]) with its underlying temporary array
|
| 2256 |
+
|
| 2257 |
+
[^2]: These conversions are considered as standard conversions for the
|
| 2258 |
purposes of overload resolution ([[over.best.ics]],
|
| 2259 |
[[over.ics.ref]]) and therefore initialization ([[dcl.init]]) and
|
| 2260 |
explicit casts ([[expr.static.cast]]). A conversion to `void` does
|
| 2261 |
not invoke any conversion function ([[expr.static.cast]]). Even
|
| 2262 |
though never directly called to perform a conversion, such
|
| 2263 |
conversion functions can be declared and can potentially be reached
|
| 2264 |
through a call to a virtual conversion function in a base class.
|
| 2265 |
|
| 2266 |
+
[^3]: A similar provision is not needed for the array version of
|
| 2267 |
`operator` `delete` because [[expr.delete]] requires that in this
|
| 2268 |
situation, the static type of the object to be deleted be the same
|
| 2269 |
as its dynamic type.
|
| 2270 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2271 |
[^4]: This implies that the reference parameter of the
|
| 2272 |
implicitly-declared copy constructor cannot bind to a `volatile`
|
| 2273 |
lvalue; see [[diff.special]].
|
| 2274 |
|
| 2275 |
+
[^5]: Because a template assignment operator or an assignment operator
|
|
|
|
|
|
|
|
|
|
| 2276 |
taking an rvalue reference parameter is never a copy assignment
|
| 2277 |
operator, the presence of such an assignment operator does not
|
| 2278 |
suppress the implicit declaration of a copy assignment operator.
|
| 2279 |
Such assignment operators participate in overload resolution with
|
| 2280 |
other assignment operators, including copy assignment operators,
|
| 2281 |
and, if selected, will be used to assign an object.
|
| 2282 |
|
| 2283 |
+
[^6]: This implies that the reference parameter of the
|
| 2284 |
implicitly-declared copy assignment operator cannot bind to a
|
| 2285 |
`volatile` lvalue; see [[diff.special]].
|
| 2286 |
|
| 2287 |
+
[^7]: Because only one object is destroyed instead of two, and one
|
| 2288 |
copy/move constructor is not executed, there is still one object
|
| 2289 |
destroyed for each one constructed.
|