tmp/tmpqv0tizn5/{from.md → to.md}
RENAMED
|
@@ -62,17 +62,19 @@ its class. Such a constructor is called a *converting constructor*.
|
|
| 62 |
|
| 63 |
``` cpp
|
| 64 |
struct X {
|
| 65 |
X(int);
|
| 66 |
X(const char*, int =0);
|
|
|
|
| 67 |
};
|
| 68 |
|
| 69 |
void f(X arg) {
|
| 70 |
X a = 1; // a = X(1)
|
| 71 |
X b = "Jessie"; // b = X("Jessie",0)
|
| 72 |
a = 2; // a = X(2)
|
| 73 |
f(3); // f(X(3))
|
|
|
|
| 74 |
}
|
| 75 |
```
|
| 76 |
|
| 77 |
An explicit constructor constructs objects just like non-explicit
|
| 78 |
constructors, but does so only where the direct-initialization syntax (
|
|
@@ -83,19 +85,21 @@ value-initialization ([[dcl.init]]).
|
|
| 83 |
|
| 84 |
``` cpp
|
| 85 |
struct Z {
|
| 86 |
explicit Z();
|
| 87 |
explicit Z(int);
|
|
|
|
| 88 |
};
|
| 89 |
|
| 90 |
Z a; // OK: default-initialization performed
|
| 91 |
Z a1 = 1; // error: no implicit conversion
|
| 92 |
Z a3 = Z(1); // OK: direct initialization syntax used
|
| 93 |
Z a2(1); // OK: direct initialization syntax used
|
| 94 |
Z* p = new Z(1); // OK: direct initialization syntax used
|
| 95 |
Z a4 = (Z)1; // OK: explicit cast used
|
| 96 |
Z a5 = static_cast<Z>(1); // OK: explicit cast used
|
|
|
|
| 97 |
```
|
| 98 |
|
| 99 |
A non-explicit copy/move constructor ([[class.copy]]) is a converting
|
| 100 |
constructor. An implicitly-declared copy/move constructor is not an
|
| 101 |
explicit constructor; it may be called for implicit type conversions.
|
|
@@ -126,11 +130,11 @@ return type can be specified. If a conversion function is a member
|
|
| 126 |
function, the type of the conversion function ([[dcl.fct]]) is
|
| 127 |
“function taking no parameter returning *conversion-type-id*”. A
|
| 128 |
conversion function is never used to convert a (possibly cv-qualified)
|
| 129 |
object to the (possibly cv-qualified) same object type (or a reference
|
| 130 |
to it), to a (possibly cv-qualified) base class of that type (or a
|
| 131 |
-
reference to it), or to (possibly cv-qualified) void.[^
|
| 132 |
|
| 133 |
``` cpp
|
| 134 |
struct X {
|
| 135 |
operator int();
|
| 136 |
};
|
|
|
|
| 62 |
|
| 63 |
``` cpp
|
| 64 |
struct X {
|
| 65 |
X(int);
|
| 66 |
X(const char*, int =0);
|
| 67 |
+
X(int, int);
|
| 68 |
};
|
| 69 |
|
| 70 |
void f(X arg) {
|
| 71 |
X a = 1; // a = X(1)
|
| 72 |
X b = "Jessie"; // b = X("Jessie",0)
|
| 73 |
a = 2; // a = X(2)
|
| 74 |
f(3); // f(X(3))
|
| 75 |
+
f({1, 2}); // f(X(1,2))
|
| 76 |
}
|
| 77 |
```
|
| 78 |
|
| 79 |
An explicit constructor constructs objects just like non-explicit
|
| 80 |
constructors, but does so only where the direct-initialization syntax (
|
|
|
|
| 85 |
|
| 86 |
``` cpp
|
| 87 |
struct Z {
|
| 88 |
explicit Z();
|
| 89 |
explicit Z(int);
|
| 90 |
+
explicit Z(int, int);
|
| 91 |
};
|
| 92 |
|
| 93 |
Z a; // OK: default-initialization performed
|
| 94 |
Z a1 = 1; // error: no implicit conversion
|
| 95 |
Z a3 = Z(1); // OK: direct initialization syntax used
|
| 96 |
Z a2(1); // OK: direct initialization syntax used
|
| 97 |
Z* p = new Z(1); // OK: direct initialization syntax used
|
| 98 |
Z a4 = (Z)1; // OK: explicit cast used
|
| 99 |
Z a5 = static_cast<Z>(1); // OK: explicit cast used
|
| 100 |
+
Z a6 = { 3, 4 }; // error: no implicit conversion
|
| 101 |
```
|
| 102 |
|
| 103 |
A non-explicit copy/move constructor ([[class.copy]]) is a converting
|
| 104 |
constructor. An implicitly-declared copy/move constructor is not an
|
| 105 |
explicit constructor; it may be called for implicit type conversions.
|
|
|
|
| 130 |
function, the type of the conversion function ([[dcl.fct]]) is
|
| 131 |
“function taking no parameter returning *conversion-type-id*”. A
|
| 132 |
conversion function is never used to convert a (possibly cv-qualified)
|
| 133 |
object to the (possibly cv-qualified) same object type (or a reference
|
| 134 |
to it), to a (possibly cv-qualified) base class of that type (or a
|
| 135 |
+
reference to it), or to (possibly cv-qualified) void.[^2]
|
| 136 |
|
| 137 |
``` cpp
|
| 138 |
struct X {
|
| 139 |
operator int();
|
| 140 |
};
|