tmp/tmpyz_mno9y/{from.md → to.md}
RENAMED
|
@@ -47,12 +47,12 @@ complex f = 3; // initialized by calling complex(double) with a
|
|
| 47 |
complex g = { 1, 2 }; // initialized by calling complex(double, double) with arguments 1 and 2
|
| 48 |
```
|
| 49 |
|
| 50 |
— *end example*]
|
| 51 |
|
| 52 |
-
[*Note 1*: Overloading of the assignment operator [[over.
|
| 53 |
-
effect on initialization. — *end note*]
|
| 54 |
|
| 55 |
An object of class type can also be initialized by a *braced-init-list*.
|
| 56 |
List-initialization semantics apply; see [[dcl.init]] and
|
| 57 |
[[dcl.init.list]].
|
| 58 |
|
|
@@ -83,13 +83,13 @@ Here, `x.i` is initialized with 99, `x.f` is initialized with 88.8, and
|
|
| 83 |
[*Note 2*: Braces can be elided in the *initializer-list* for any
|
| 84 |
aggregate, even if the aggregate has members of a class type with
|
| 85 |
user-defined type conversions; see [[dcl.init.aggr]]. — *end note*]
|
| 86 |
|
| 87 |
[*Note 3*: If `T` is a class type with no default constructor, any
|
| 88 |
-
declaration of an object of type `T` (or array thereof) is
|
| 89 |
-
no *initializer* is explicitly specified (see
|
| 90 |
-
[[dcl.init]]). — *end note*]
|
| 91 |
|
| 92 |
[*Note 4*: The order in which objects with static or thread storage
|
| 93 |
duration are initialized is described in [[basic.start.dynamic]] and
|
| 94 |
[[stmt.dcl]]. — *end note*]
|
| 95 |
|
|
@@ -130,11 +130,11 @@ name as a direct or virtual base class of the class, a
|
|
| 130 |
*mem-initializer-id* naming the member or base class and composed of a
|
| 131 |
single identifier refers to the class member. A *mem-initializer-id* for
|
| 132 |
the hidden base class can be specified using a qualified
|
| 133 |
name. — *end note*]
|
| 134 |
|
| 135 |
-
Unless the *mem-initializer-id* names the constructor’s class, a
|
| 136 |
non-static data member of the constructor’s class, or a direct or
|
| 137 |
virtual base of that class, the *mem-initializer* is ill-formed.
|
| 138 |
|
| 139 |
A *mem-initializer-list* can initialize a base class using any
|
| 140 |
*class-or-decltype* that denotes that base class type.
|
|
@@ -272,11 +272,11 @@ renders the program ill-formed.
|
|
| 272 |
[*Note 4*: After the call to a constructor for class `X` for an object
|
| 273 |
with automatic or dynamic storage duration has completed, if the
|
| 274 |
constructor was not invoked as part of value-initialization and a member
|
| 275 |
of `X` is neither initialized nor given a value during execution of the
|
| 276 |
*compound-statement* of the body of the constructor, the member has an
|
| 277 |
-
indeterminate value. — *end note*]
|
| 278 |
|
| 279 |
[*Example 6*:
|
| 280 |
|
| 281 |
``` cpp
|
| 282 |
struct A {
|
|
@@ -289,11 +289,11 @@ struct B {
|
|
| 289 |
|
| 290 |
struct C {
|
| 291 |
C() { } // initializes members as follows:
|
| 292 |
A a; // OK, calls A::A()
|
| 293 |
const B b; // error: B has no default constructor
|
| 294 |
-
int i; // OK, i has indeterminate value
|
| 295 |
int j = 5; // OK, j has the value 5
|
| 296 |
};
|
| 297 |
```
|
| 298 |
|
| 299 |
— *end example*]
|
|
@@ -427,17 +427,23 @@ the constructor parameter `i`, and initializes `X::j` with the value of
|
|
| 427 |
`X::i`; this takes place each time an object of class `X` is created.
|
| 428 |
|
| 429 |
— *end example*]
|
| 430 |
|
| 431 |
Member functions (including virtual member functions, [[class.virtual]])
|
| 432 |
-
can be called for an object under construction
|
| 433 |
-
|
| 434 |
-
[[expr.typeid]] or of a `dynamic_cast`
|
| 435 |
-
if these operations are performed
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
|
| 440 |
[*Example 11*:
|
| 441 |
|
| 442 |
``` cpp
|
| 443 |
class A {
|
|
@@ -494,15 +500,19 @@ public:
|
|
| 494 |
When a constructor for type `B` is invoked to initialize an object of a
|
| 495 |
different type `D` (that is, when the constructor was inherited
|
| 496 |
[[namespace.udecl]]), initialization proceeds as if a defaulted default
|
| 497 |
constructor were used to initialize the `D` object and each base class
|
| 498 |
subobject from which the constructor was inherited, except that the `B`
|
| 499 |
-
subobject is initialized by the
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
object.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
|
| 505 |
[*Example 1*:
|
| 506 |
|
| 507 |
``` cpp
|
| 508 |
struct B1 {
|
|
@@ -551,14 +561,33 @@ Class template `Log` wraps any class and forwards all of its
|
|
| 551 |
constructors, while writing a message to the standard log whenever an
|
| 552 |
object of class `Log` is destroyed.
|
| 553 |
|
| 554 |
— *end example*]
|
| 555 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
If the constructor was inherited from multiple base class subobjects of
|
| 557 |
type `B`, the program is ill-formed.
|
| 558 |
|
| 559 |
-
[*Example
|
| 560 |
|
| 561 |
``` cpp
|
| 562 |
struct A { A(int); };
|
| 563 |
struct B : A { using A::A; };
|
| 564 |
|
|
@@ -620,11 +649,11 @@ int* p2 = &bobj.y.i; // undefined behavior: refers to member'
|
|
| 620 |
|
| 621 |
A* pa = &bobj; // undefined behavior: upcast to a base class type
|
| 622 |
B bobj; // definition of bobj
|
| 623 |
|
| 624 |
extern X xobj;
|
| 625 |
-
int* p3 = &xobj.i; // OK,
|
| 626 |
X xobj;
|
| 627 |
```
|
| 628 |
|
| 629 |
For another example,
|
| 630 |
|
|
@@ -639,14 +668,14 @@ struct Y {
|
|
| 639 |
};
|
| 640 |
```
|
| 641 |
|
| 642 |
— *end example*]
|
| 643 |
|
| 644 |
-
During the construction of an object, if the value of
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
|
| 649 |
[*Example 2*:
|
| 650 |
|
| 651 |
``` cpp
|
| 652 |
struct C;
|
|
@@ -712,11 +741,13 @@ struct E : C, D, X {
|
|
| 712 |
|
| 713 |
Member functions, including virtual functions [[class.virtual]], can be
|
| 714 |
called during construction or destruction [[class.base.init]]. When a
|
| 715 |
virtual function is called directly or indirectly from a constructor or
|
| 716 |
from a destructor, including during the construction or destruction of
|
| 717 |
-
the class’s non-static data members,
|
|
|
|
|
|
|
| 718 |
applies is the object (call it `x`) under construction or destruction,
|
| 719 |
the function called is the final overrider in the constructor’s or
|
| 720 |
destructor’s class and not one overriding it in a more-derived class. If
|
| 721 |
the virtual function call uses an explicit class member access
|
| 722 |
[[expr.ref]] and the object expression refers to the complete object of
|
|
@@ -810,59 +841,62 @@ B::B(V* v, A* a) {
|
|
| 810 |
— *end example*]
|
| 811 |
|
| 812 |
### Copy/move elision <a id="class.copy.elision">[[class.copy.elision]]</a>
|
| 813 |
|
| 814 |
When certain criteria are met, an implementation is allowed to omit the
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
object have side effects. In such cases, the
|
| 818 |
-
source and target of the omitted
|
| 819 |
-
different ways of referring to the same
|
| 820 |
-
of the selected constructor is an rvalue
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
when the two objects would have been
|
| 824 |
-
optimization.
|
| 825 |
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
|
|
|
|
|
|
|
|
|
| 838 |
- in a *throw-expression* [[expr.throw]], when the operand is the name
|
| 839 |
-
of a non-volatile object with automatic storage duration (other than
|
| 840 |
-
function
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
|
|
|
|
|
|
| 844 |
- in a coroutine [[dcl.fct.def.coroutine]], a copy of a coroutine
|
| 845 |
parameter can be omitted and references to that copy replaced with
|
| 846 |
references to the corresponding parameter if the meaning of the
|
| 847 |
program will be unchanged except for the execution of a constructor
|
| 848 |
-
and destructor for the parameter copy object
|
| 849 |
-
- when the *exception-declaration* of
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
the object
|
| 856 |
-
cannot be a move from the exception object because it is always an
|
| 857 |
-
lvalue. — *end note*]
|
| 858 |
|
| 859 |
Copy elision is not permitted where an expression is evaluated in a
|
| 860 |
context requiring a constant expression [[expr.const]] and in constant
|
| 861 |
initialization [[basic.start.static]].
|
| 862 |
|
| 863 |
-
[*Note
|
| 864 |
expression is evaluated in another context. — *end note*]
|
| 865 |
|
| 866 |
[*Example 1*:
|
| 867 |
|
| 868 |
``` cpp
|
|
|
|
| 47 |
complex g = { 1, 2 }; // initialized by calling complex(double, double) with arguments 1 and 2
|
| 48 |
```
|
| 49 |
|
| 50 |
— *end example*]
|
| 51 |
|
| 52 |
+
[*Note 1*: Overloading of the assignment operator [[over.assign]] has
|
| 53 |
+
no effect on initialization. — *end note*]
|
| 54 |
|
| 55 |
An object of class type can also be initialized by a *braced-init-list*.
|
| 56 |
List-initialization semantics apply; see [[dcl.init]] and
|
| 57 |
[[dcl.init.list]].
|
| 58 |
|
|
|
|
| 83 |
[*Note 2*: Braces can be elided in the *initializer-list* for any
|
| 84 |
aggregate, even if the aggregate has members of a class type with
|
| 85 |
user-defined type conversions; see [[dcl.init.aggr]]. — *end note*]
|
| 86 |
|
| 87 |
[*Note 3*: If `T` is a class type with no default constructor, any
|
| 88 |
+
initializing declaration of an object of type `T` (or array thereof) is
|
| 89 |
+
ill-formed if no *initializer* is explicitly specified (see
|
| 90 |
+
[[class.init]] and [[dcl.init]]). — *end note*]
|
| 91 |
|
| 92 |
[*Note 4*: The order in which objects with static or thread storage
|
| 93 |
duration are initialized is described in [[basic.start.dynamic]] and
|
| 94 |
[[stmt.dcl]]. — *end note*]
|
| 95 |
|
|
|
|
| 130 |
*mem-initializer-id* naming the member or base class and composed of a
|
| 131 |
single identifier refers to the class member. A *mem-initializer-id* for
|
| 132 |
the hidden base class can be specified using a qualified
|
| 133 |
name. — *end note*]
|
| 134 |
|
| 135 |
+
Unless the *mem-initializer-id* names the constructor’s class, a direct
|
| 136 |
non-static data member of the constructor’s class, or a direct or
|
| 137 |
virtual base of that class, the *mem-initializer* is ill-formed.
|
| 138 |
|
| 139 |
A *mem-initializer-list* can initialize a base class using any
|
| 140 |
*class-or-decltype* that denotes that base class type.
|
|
|
|
| 272 |
[*Note 4*: After the call to a constructor for class `X` for an object
|
| 273 |
with automatic or dynamic storage duration has completed, if the
|
| 274 |
constructor was not invoked as part of value-initialization and a member
|
| 275 |
of `X` is neither initialized nor given a value during execution of the
|
| 276 |
*compound-statement* of the body of the constructor, the member has an
|
| 277 |
+
indeterminate or erroneous value [[basic.indet]]. — *end note*]
|
| 278 |
|
| 279 |
[*Example 6*:
|
| 280 |
|
| 281 |
``` cpp
|
| 282 |
struct A {
|
|
|
|
| 289 |
|
| 290 |
struct C {
|
| 291 |
C() { } // initializes members as follows:
|
| 292 |
A a; // OK, calls A::A()
|
| 293 |
const B b; // error: B has no default constructor
|
| 294 |
+
int i; // OK, i has indeterminate or erroneous value
|
| 295 |
int j = 5; // OK, j has the value 5
|
| 296 |
};
|
| 297 |
```
|
| 298 |
|
| 299 |
— *end example*]
|
|
|
|
| 427 |
`X::i`; this takes place each time an object of class `X` is created.
|
| 428 |
|
| 429 |
— *end example*]
|
| 430 |
|
| 431 |
Member functions (including virtual member functions, [[class.virtual]])
|
| 432 |
+
can be called for an object under construction or destruction.
|
| 433 |
+
Similarly, an object under construction or destruction can be the
|
| 434 |
+
operand of the `typeid` operator [[expr.typeid]] or of a `dynamic_cast`
|
| 435 |
+
[[expr.dynamic.cast]]. However, if these operations are performed during
|
| 436 |
+
evaluation of
|
| 437 |
+
|
| 438 |
+
- a *ctor-initializer* (or in a function called directly or indirectly
|
| 439 |
+
from a *ctor-initializer*) before all the *mem-initializer*s for base
|
| 440 |
+
classes have completed,
|
| 441 |
+
- a precondition assertion of a constructor, or
|
| 442 |
+
- a postcondition assertion of a destructor [[dcl.contract.func]],
|
| 443 |
+
|
| 444 |
+
the program has undefined behavior.
|
| 445 |
|
| 446 |
[*Example 11*:
|
| 447 |
|
| 448 |
``` cpp
|
| 449 |
class A {
|
|
|
|
| 500 |
When a constructor for type `B` is invoked to initialize an object of a
|
| 501 |
different type `D` (that is, when the constructor was inherited
|
| 502 |
[[namespace.udecl]]), initialization proceeds as if a defaulted default
|
| 503 |
constructor were used to initialize the `D` object and each base class
|
| 504 |
subobject from which the constructor was inherited, except that the `B`
|
| 505 |
+
subobject is initialized by the inherited constructor if the base class
|
| 506 |
+
subobject were to be initialized as part of the `D` object
|
| 507 |
+
[[class.base.init]]. The invocation of the inherited constructor,
|
| 508 |
+
including the evaluation of any arguments, is omitted if the `B`
|
| 509 |
+
subobject is not to be initialized as part of the `D` object. The
|
| 510 |
+
complete initialization is considered to be a single function call; in
|
| 511 |
+
particular, unless omitted, the initialization of the inherited
|
| 512 |
+
constructor’s parameters is sequenced before the initialization of any
|
| 513 |
+
part of the `D` object.
|
| 514 |
|
| 515 |
[*Example 1*:
|
| 516 |
|
| 517 |
``` cpp
|
| 518 |
struct B1 {
|
|
|
|
| 561 |
constructors, while writing a message to the standard log whenever an
|
| 562 |
object of class `Log` is destroyed.
|
| 563 |
|
| 564 |
— *end example*]
|
| 565 |
|
| 566 |
+
[*Example 2*:
|
| 567 |
+
|
| 568 |
+
``` cpp
|
| 569 |
+
struct V { V() = default; V(int); };
|
| 570 |
+
struct Q { Q(); };
|
| 571 |
+
struct A : virtual V, Q {
|
| 572 |
+
using V::V;
|
| 573 |
+
A() = delete;
|
| 574 |
+
};
|
| 575 |
+
int bar() { return 42; }
|
| 576 |
+
struct B : A {
|
| 577 |
+
B() : A(bar()) {} // OK
|
| 578 |
+
};
|
| 579 |
+
struct C : B {};
|
| 580 |
+
void foo() { C c; } // bar is not invoked, because the V subobject is not initialized as part of B
|
| 581 |
+
```
|
| 582 |
+
|
| 583 |
+
— *end example*]
|
| 584 |
+
|
| 585 |
If the constructor was inherited from multiple base class subobjects of
|
| 586 |
type `B`, the program is ill-formed.
|
| 587 |
|
| 588 |
+
[*Example 3*:
|
| 589 |
|
| 590 |
``` cpp
|
| 591 |
struct A { A(int); };
|
| 592 |
struct B : A { using A::A; };
|
| 593 |
|
|
|
|
| 649 |
|
| 650 |
A* pa = &bobj; // undefined behavior: upcast to a base class type
|
| 651 |
B bobj; // definition of bobj
|
| 652 |
|
| 653 |
extern X xobj;
|
| 654 |
+
int* p3 = &xobj.i; // OK, all constructors of X are trivial
|
| 655 |
X xobj;
|
| 656 |
```
|
| 657 |
|
| 658 |
For another example,
|
| 659 |
|
|
|
|
| 668 |
};
|
| 669 |
```
|
| 670 |
|
| 671 |
— *end example*]
|
| 672 |
|
| 673 |
+
During the construction of an object, if the value of any of its
|
| 674 |
+
subobjects or any element of its object representation is accessed
|
| 675 |
+
through a glvalue that is not obtained, directly or indirectly, from the
|
| 676 |
+
constructor’s `this` pointer, the value thus obtained is unspecified.
|
| 677 |
|
| 678 |
[*Example 2*:
|
| 679 |
|
| 680 |
``` cpp
|
| 681 |
struct C;
|
|
|
|
| 741 |
|
| 742 |
Member functions, including virtual functions [[class.virtual]], can be
|
| 743 |
called during construction or destruction [[class.base.init]]. When a
|
| 744 |
virtual function is called directly or indirectly from a constructor or
|
| 745 |
from a destructor, including during the construction or destruction of
|
| 746 |
+
the class’s non-static data members, or during the evaluation of a
|
| 747 |
+
postcondition assertion of a constructor or a precondition assertion of
|
| 748 |
+
a destructor [[dcl.contract.func]], and the object to which the call
|
| 749 |
applies is the object (call it `x`) under construction or destruction,
|
| 750 |
the function called is the final overrider in the constructor’s or
|
| 751 |
destructor’s class and not one overriding it in a more-derived class. If
|
| 752 |
the virtual function call uses an explicit class member access
|
| 753 |
[[expr.ref]] and the object expression refers to the complete object of
|
|
|
|
| 841 |
— *end example*]
|
| 842 |
|
| 843 |
### Copy/move elision <a id="class.copy.elision">[[class.copy.elision]]</a>
|
| 844 |
|
| 845 |
When certain criteria are met, an implementation is allowed to omit the
|
| 846 |
+
creation of a class object from a source object of the same type
|
| 847 |
+
(ignoring cv-qualification), even if the selected constructor and/or the
|
| 848 |
+
destructor for the object have side effects. In such cases, the
|
| 849 |
+
implementation treats the source and target of the omitted
|
| 850 |
+
initialization as simply two different ways of referring to the same
|
| 851 |
+
object. If the first parameter of the selected constructor is an rvalue
|
| 852 |
+
reference to the object’s type, the destruction of that object occurs
|
| 853 |
+
when the target would have been destroyed; otherwise, the destruction
|
| 854 |
+
occurs at the later of the times when the two objects would have been
|
| 855 |
+
destroyed without the optimization.
|
| 856 |
|
| 857 |
+
[*Note 1*: Because only one object is destroyed instead of two, and the
|
| 858 |
+
creation of one object is omitted, there is still one object destroyed
|
| 859 |
+
for each one constructed. — *end note*]
|
| 860 |
|
| 861 |
+
This elision of object creation, called *copy elision*, is permitted in
|
| 862 |
+
the following circumstances (which may be combined to eliminate multiple
|
| 863 |
+
copies):
|
| 864 |
+
|
| 865 |
+
- in a `return` statement [[stmt.return]] in a function with a class
|
| 866 |
+
return type, when the *expression* is the name of a non-volatile
|
| 867 |
+
object o with automatic storage duration (other than a function
|
| 868 |
+
parameter or a variable introduced by the *exception-declaration* of a
|
| 869 |
+
*handler* [[except.handle]]), the copy-initialization of the result
|
| 870 |
+
object can be omitted by constructing o directly into the function
|
| 871 |
+
call’s result object;
|
| 872 |
- in a *throw-expression* [[expr.throw]], when the operand is the name
|
| 873 |
+
of a non-volatile object o with automatic storage duration (other than
|
| 874 |
+
a function parameter or a variable introduced by the
|
| 875 |
+
*exception-declaration* of a *handler*) that belongs to a scope that
|
| 876 |
+
does not contain the innermost enclosing *compound-statement*
|
| 877 |
+
associated with a *try-block* (if there is one), the
|
| 878 |
+
copy-initialization of the exception object can be omitted by
|
| 879 |
+
constructing o directly into the exception object;
|
| 880 |
- in a coroutine [[dcl.fct.def.coroutine]], a copy of a coroutine
|
| 881 |
parameter can be omitted and references to that copy replaced with
|
| 882 |
references to the corresponding parameter if the meaning of the
|
| 883 |
program will be unchanged except for the execution of a constructor
|
| 884 |
+
and destructor for the parameter copy object;
|
| 885 |
+
- when the *exception-declaration* of a *handler* [[except.handle]]
|
| 886 |
+
declares an object o, the copy-initialization of o can be omitted by
|
| 887 |
+
treating the *exception-declaration* as an alias for the exception
|
| 888 |
+
object if the meaning of the program will be unchanged except for the
|
| 889 |
+
execution of constructors and destructors for the object declared by
|
| 890 |
+
the *exception-declaration*. \[*Note 2*: There cannot be a move from
|
| 891 |
+
the exception object because it is always an lvalue. — *end note*]
|
|
|
|
|
|
|
| 892 |
|
| 893 |
Copy elision is not permitted where an expression is evaluated in a
|
| 894 |
context requiring a constant expression [[expr.const]] and in constant
|
| 895 |
initialization [[basic.start.static]].
|
| 896 |
|
| 897 |
+
[*Note 3*: It is possible that copy elision is performed if the same
|
| 898 |
expression is evaluated in another context. — *end note*]
|
| 899 |
|
| 900 |
[*Example 1*:
|
| 901 |
|
| 902 |
``` cpp
|