tmp/tmpisd2xwjm/{from.md → to.md}
RENAMED
|
@@ -6,17 +6,19 @@ its class. Such a constructor is called a *converting constructor*.
|
|
| 6 |
|
| 7 |
``` cpp
|
| 8 |
struct X {
|
| 9 |
X(int);
|
| 10 |
X(const char*, int =0);
|
|
|
|
| 11 |
};
|
| 12 |
|
| 13 |
void f(X arg) {
|
| 14 |
X a = 1; // a = X(1)
|
| 15 |
X b = "Jessie"; // b = X("Jessie",0)
|
| 16 |
a = 2; // a = X(2)
|
| 17 |
f(3); // f(X(3))
|
|
|
|
| 18 |
}
|
| 19 |
```
|
| 20 |
|
| 21 |
An explicit constructor constructs objects just like non-explicit
|
| 22 |
constructors, but does so only where the direct-initialization syntax (
|
|
@@ -27,19 +29,21 @@ value-initialization ([[dcl.init]]).
|
|
| 27 |
|
| 28 |
``` cpp
|
| 29 |
struct Z {
|
| 30 |
explicit Z();
|
| 31 |
explicit Z(int);
|
|
|
|
| 32 |
};
|
| 33 |
|
| 34 |
Z a; // OK: default-initialization performed
|
| 35 |
Z a1 = 1; // error: no implicit conversion
|
| 36 |
Z a3 = Z(1); // OK: direct initialization syntax used
|
| 37 |
Z a2(1); // OK: direct initialization syntax used
|
| 38 |
Z* p = new Z(1); // OK: direct initialization syntax used
|
| 39 |
Z a4 = (Z)1; // OK: explicit cast used
|
| 40 |
Z a5 = static_cast<Z>(1); // OK: explicit cast used
|
|
|
|
| 41 |
```
|
| 42 |
|
| 43 |
A non-explicit copy/move constructor ([[class.copy]]) is a converting
|
| 44 |
constructor. An implicitly-declared copy/move constructor is not an
|
| 45 |
explicit constructor; it may be called for implicit type conversions.
|
|
|
|
| 6 |
|
| 7 |
``` cpp
|
| 8 |
struct X {
|
| 9 |
X(int);
|
| 10 |
X(const char*, int =0);
|
| 11 |
+
X(int, int);
|
| 12 |
};
|
| 13 |
|
| 14 |
void f(X arg) {
|
| 15 |
X a = 1; // a = X(1)
|
| 16 |
X b = "Jessie"; // b = X("Jessie",0)
|
| 17 |
a = 2; // a = X(2)
|
| 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 (
|
|
|
|
| 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. An implicitly-declared copy/move constructor is not an
|
| 49 |
explicit constructor; it may be called for implicit type conversions.
|