tmp/tmpedvk55mj/{from.md → to.md}
RENAMED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
| 2 |
|
| 3 |
A constructor declared without the *function-specifier* `explicit`
|
| 4 |
-
specifies a conversion from the types of its parameters
|
| 5 |
-
its class. Such a constructor is called a *converting
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
``` cpp
|
| 8 |
struct X {
|
| 9 |
X(int);
|
| 10 |
X(const char*, int =0);
|
|
@@ -18,33 +21,48 @@ void f(X arg) {
|
|
| 18 |
f(3); // f(X(3))
|
| 19 |
f({1, 2}); // f(X(1,2))
|
| 20 |
}
|
| 21 |
```
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
An explicit constructor constructs objects just like non-explicit
|
| 24 |
constructors, but does so only where the direct-initialization syntax (
|
| 25 |
[[dcl.init]]) or where casts ([[expr.static.cast]], [[expr.cast]]) are
|
| 26 |
-
explicitly used. A default constructor
|
| 27 |
-
such a constructor will be used to
|
| 28 |
-
value-initialization ([[dcl.init]]).
|
|
|
|
|
|
|
| 29 |
|
| 30 |
``` cpp
|
| 31 |
struct Z {
|
| 32 |
explicit Z();
|
| 33 |
explicit Z(int);
|
| 34 |
explicit Z(int, int);
|
| 35 |
};
|
| 36 |
|
| 37 |
Z a; // OK: default-initialization performed
|
|
|
|
|
|
|
| 38 |
Z a1 = 1; // error: no implicit conversion
|
| 39 |
Z a3 = Z(1); // OK: direct initialization syntax used
|
| 40 |
Z a2(1); // OK: direct initialization syntax used
|
| 41 |
Z* p = new Z(1); // OK: direct initialization syntax used
|
| 42 |
Z a4 = (Z)1; // OK: explicit cast used
|
| 43 |
Z a5 = static_cast<Z>(1); // OK: explicit cast used
|
| 44 |
Z a6 = { 3, 4 }; // error: no implicit conversion
|
| 45 |
```
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
A non-explicit copy/move constructor ([[class.copy]]) is a converting
|
| 48 |
-
constructor.
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
|
|
|
| 1 |
### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
| 2 |
|
| 3 |
A constructor declared without the *function-specifier* `explicit`
|
| 4 |
+
specifies a conversion from the types of its parameters (if any) to the
|
| 5 |
+
type of its class. Such a constructor is called a *converting
|
| 6 |
+
constructor*.
|
| 7 |
+
|
| 8 |
+
[*Example 1*:
|
| 9 |
|
| 10 |
``` cpp
|
| 11 |
struct X {
|
| 12 |
X(int);
|
| 13 |
X(const char*, int =0);
|
|
|
|
| 21 |
f(3); // f(X(3))
|
| 22 |
f({1, 2}); // f(X(1,2))
|
| 23 |
}
|
| 24 |
```
|
| 25 |
|
| 26 |
+
— *end example*]
|
| 27 |
+
|
| 28 |
+
[*Note 1*:
|
| 29 |
+
|
| 30 |
An explicit constructor constructs objects just like non-explicit
|
| 31 |
constructors, but does so only where the direct-initialization syntax (
|
| 32 |
[[dcl.init]]) or where casts ([[expr.static.cast]], [[expr.cast]]) are
|
| 33 |
+
explicitly used; see also [[over.match.copy]]. A default constructor
|
| 34 |
+
may be an explicit constructor; such a constructor will be used to
|
| 35 |
+
perform default-initialization or value-initialization ([[dcl.init]]).
|
| 36 |
+
|
| 37 |
+
[*Example 2*:
|
| 38 |
|
| 39 |
``` cpp
|
| 40 |
struct Z {
|
| 41 |
explicit Z();
|
| 42 |
explicit Z(int);
|
| 43 |
explicit Z(int, int);
|
| 44 |
};
|
| 45 |
|
| 46 |
Z a; // OK: default-initialization performed
|
| 47 |
+
Z b{}; // OK: direct initialization syntax used
|
| 48 |
+
Z c = {}; // error: copy-list-initialization
|
| 49 |
Z a1 = 1; // error: no implicit conversion
|
| 50 |
Z a3 = Z(1); // OK: direct initialization syntax used
|
| 51 |
Z a2(1); // OK: direct initialization syntax used
|
| 52 |
Z* p = new Z(1); // OK: direct initialization syntax used
|
| 53 |
Z a4 = (Z)1; // OK: explicit cast used
|
| 54 |
Z a5 = static_cast<Z>(1); // OK: explicit cast used
|
| 55 |
Z a6 = { 3, 4 }; // error: no implicit conversion
|
| 56 |
```
|
| 57 |
|
| 58 |
+
— *end example*]
|
| 59 |
+
|
| 60 |
+
— *end note*]
|
| 61 |
+
|
| 62 |
A non-explicit copy/move constructor ([[class.copy]]) is a converting
|
| 63 |
+
constructor.
|
| 64 |
+
|
| 65 |
+
[*Note 2*: An implicitly-declared copy/move constructor is not an
|
| 66 |
+
explicit constructor; it may be called for implicit type
|
| 67 |
+
conversions. — *end note*]
|
| 68 |
|