tmp/tmpe4325s5r/{from.md → to.md}
RENAMED
|
@@ -1,17 +1,17 @@
|
|
| 1 |
-
## Conversions <a id="class.conv">[[class.conv]]</a>
|
| 2 |
|
| 3 |
Type conversions of class objects can be specified by constructors and
|
| 4 |
by conversion functions. These conversions are called *user-defined
|
| 5 |
-
conversions* and are used for implicit type conversions
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
User-defined conversions are applied only where they are unambiguous (
|
| 10 |
[[class.member.lookup]], [[class.conv.fct]]). Conversions obey the
|
| 11 |
-
access control rules
|
| 12 |
-
|
| 13 |
|
| 14 |
[*Note 1*: See [[over.match]] for a discussion of the use of
|
| 15 |
conversions in function calls as well as examples below. — *end note*]
|
| 16 |
|
| 17 |
At most one user-defined conversion (constructor or conversion function)
|
|
@@ -27,21 +27,21 @@ struct X {
|
|
| 27 |
struct Y {
|
| 28 |
operator X();
|
| 29 |
};
|
| 30 |
|
| 31 |
Y a;
|
| 32 |
-
int b = a; // error
|
| 33 |
int c = X(a); // OK: a.operator X().operator int()
|
| 34 |
```
|
| 35 |
|
| 36 |
— *end example*]
|
| 37 |
|
| 38 |
User-defined conversions are used implicitly only if they are
|
| 39 |
unambiguous. A conversion function in a derived class does not hide a
|
| 40 |
conversion function in a base class unless the two functions convert to
|
| 41 |
-
the same type. Function overload resolution
|
| 42 |
-
|
| 43 |
|
| 44 |
[*Example 2*:
|
| 45 |
|
| 46 |
``` cpp
|
| 47 |
struct X {
|
|
@@ -51,23 +51,22 @@ struct X {
|
|
| 51 |
struct Y : X {
|
| 52 |
operator char();
|
| 53 |
};
|
| 54 |
|
| 55 |
void f(Y& a) {
|
| 56 |
-
if (a) { //
|
| 57 |
}
|
| 58 |
}
|
| 59 |
```
|
| 60 |
|
| 61 |
— *end example*]
|
| 62 |
|
| 63 |
-
### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
| 64 |
|
| 65 |
-
A constructor
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
constructor*.
|
| 69 |
|
| 70 |
[*Example 1*:
|
| 71 |
|
| 72 |
``` cpp
|
| 73 |
struct X {
|
|
@@ -88,15 +87,15 @@ void f(X arg) {
|
|
| 88 |
— *end example*]
|
| 89 |
|
| 90 |
[*Note 1*:
|
| 91 |
|
| 92 |
An explicit constructor constructs objects just like non-explicit
|
| 93 |
-
constructors, but does so only where the direct-initialization syntax
|
| 94 |
-
[[dcl.init]]
|
| 95 |
explicitly used; see also [[over.match.copy]]. A default constructor
|
| 96 |
may be an explicit constructor; such a constructor will be used to
|
| 97 |
-
perform default-initialization or value-initialization
|
| 98 |
|
| 99 |
[*Example 2*:
|
| 100 |
|
| 101 |
``` cpp
|
| 102 |
struct Z {
|
|
@@ -119,25 +118,25 @@ Z a6 = { 3, 4 }; // error: no implicit conversion
|
|
| 119 |
|
| 120 |
— *end example*]
|
| 121 |
|
| 122 |
— *end note*]
|
| 123 |
|
| 124 |
-
A non-explicit copy/move constructor
|
| 125 |
constructor.
|
| 126 |
|
| 127 |
[*Note 2*: An implicitly-declared copy/move constructor is not an
|
| 128 |
explicit constructor; it may be called for implicit type
|
| 129 |
conversions. — *end note*]
|
| 130 |
|
| 131 |
-
### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
|
| 132 |
|
| 133 |
A member function of a class `X` having no parameters with a name of the
|
| 134 |
form
|
| 135 |
|
| 136 |
``` bnf
|
| 137 |
conversion-function-id:
|
| 138 |
-
|
| 139 |
```
|
| 140 |
|
| 141 |
``` bnf
|
| 142 |
conversion-type-id:
|
| 143 |
type-specifier-seq conversion-declaratorₒₚₜ
|
|
@@ -150,16 +149,16 @@ conversion-declarator:
|
|
| 150 |
|
| 151 |
specifies a conversion from `X` to the type specified by the
|
| 152 |
*conversion-type-id*. Such functions are called *conversion functions*.
|
| 153 |
A *decl-specifier* in the *decl-specifier-seq* of a conversion function
|
| 154 |
(if any) shall be neither a *defining-type-specifier* nor `static`. The
|
| 155 |
-
type of the conversion function
|
| 156 |
parameter returning *conversion-type-id*”. A conversion function is
|
| 157 |
never used to convert a (possibly cv-qualified) object to the (possibly
|
| 158 |
cv-qualified) same object type (or a reference to it), to a (possibly
|
| 159 |
cv-qualified) base class of that type (or a reference to it), or to
|
| 160 |
-
|
| 161 |
|
| 162 |
[*Example 1*:
|
| 163 |
|
| 164 |
``` cpp
|
| 165 |
struct X {
|
|
@@ -177,15 +176,14 @@ void f(X a) {
|
|
| 177 |
In all three cases the value assigned will be converted by
|
| 178 |
`X::operator int()`.
|
| 179 |
|
| 180 |
— *end example*]
|
| 181 |
|
| 182 |
-
A conversion function may be explicit
|
| 183 |
-
|
| 184 |
-
direct-initialization
|
| 185 |
-
|
| 186 |
-
initializations.
|
| 187 |
|
| 188 |
[*Example 2*:
|
| 189 |
|
| 190 |
``` cpp
|
| 191 |
class Y { };
|
|
@@ -193,11 +191,11 @@ struct Z {
|
|
| 193 |
explicit operator Y() const;
|
| 194 |
};
|
| 195 |
|
| 196 |
void h(Z z) {
|
| 197 |
Y y1(z); // OK: direct-initialization
|
| 198 |
-
Y y2 = z; //
|
| 199 |
Y y3 = (Y)z; // OK: cast notation
|
| 200 |
}
|
| 201 |
|
| 202 |
void g(X a, X b) {
|
| 203 |
int i = (a) ? 1+a : 0;
|
|
@@ -245,12 +243,12 @@ operator int [[noreturn]] (); // error: noreturn attribute applied to a type
|
|
| 245 |
|
| 246 |
Conversion functions are inherited.
|
| 247 |
|
| 248 |
Conversion functions can be virtual.
|
| 249 |
|
| 250 |
-
A conversion function template shall not have a deduced return type
|
| 251 |
-
[[dcl.spec.auto]]
|
| 252 |
|
| 253 |
[*Example 5*:
|
| 254 |
|
| 255 |
``` cpp
|
| 256 |
struct S {
|
|
|
|
| 1 |
+
### Conversions <a id="class.conv">[[class.conv]]</a>
|
| 2 |
|
| 3 |
Type conversions of class objects can be specified by constructors and
|
| 4 |
by conversion functions. These conversions are called *user-defined
|
| 5 |
+
conversions* and are used for implicit type conversions [[conv]], for
|
| 6 |
+
initialization [[dcl.init]], and for explicit type conversions (
|
| 7 |
+
[[expr.type.conv]], [[expr.cast]], [[expr.static.cast]]).
|
| 8 |
|
| 9 |
User-defined conversions are applied only where they are unambiguous (
|
| 10 |
[[class.member.lookup]], [[class.conv.fct]]). Conversions obey the
|
| 11 |
+
access control rules [[class.access]]. Access control is applied after
|
| 12 |
+
ambiguity resolution [[basic.lookup]].
|
| 13 |
|
| 14 |
[*Note 1*: See [[over.match]] for a discussion of the use of
|
| 15 |
conversions in function calls as well as examples below. — *end note*]
|
| 16 |
|
| 17 |
At most one user-defined conversion (constructor or conversion function)
|
|
|
|
| 27 |
struct Y {
|
| 28 |
operator X();
|
| 29 |
};
|
| 30 |
|
| 31 |
Y a;
|
| 32 |
+
int b = a; // error: no viable conversion (a.operator X().operator int() not considered)
|
| 33 |
int c = X(a); // OK: a.operator X().operator int()
|
| 34 |
```
|
| 35 |
|
| 36 |
— *end example*]
|
| 37 |
|
| 38 |
User-defined conversions are used implicitly only if they are
|
| 39 |
unambiguous. A conversion function in a derived class does not hide a
|
| 40 |
conversion function in a base class unless the two functions convert to
|
| 41 |
+
the same type. Function overload resolution [[over.match.best]] selects
|
| 42 |
+
the best conversion function to perform the conversion.
|
| 43 |
|
| 44 |
[*Example 2*:
|
| 45 |
|
| 46 |
``` cpp
|
| 47 |
struct X {
|
|
|
|
| 51 |
struct Y : X {
|
| 52 |
operator char();
|
| 53 |
};
|
| 54 |
|
| 55 |
void f(Y& a) {
|
| 56 |
+
if (a) { // error: ambiguous between X::operator int() and Y::operator char()
|
| 57 |
}
|
| 58 |
}
|
| 59 |
```
|
| 60 |
|
| 61 |
— *end example*]
|
| 62 |
|
| 63 |
+
#### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
|
| 64 |
|
| 65 |
+
A constructor that is not explicit [[dcl.fct.spec]] specifies a
|
| 66 |
+
conversion from the types of its parameters (if any) to the type of its
|
| 67 |
+
class. Such a constructor is called a *converting constructor*.
|
|
|
|
| 68 |
|
| 69 |
[*Example 1*:
|
| 70 |
|
| 71 |
``` cpp
|
| 72 |
struct X {
|
|
|
|
| 87 |
— *end example*]
|
| 88 |
|
| 89 |
[*Note 1*:
|
| 90 |
|
| 91 |
An explicit constructor constructs objects just like non-explicit
|
| 92 |
+
constructors, but does so only where the direct-initialization syntax
|
| 93 |
+
[[dcl.init]] or where casts ([[expr.static.cast]], [[expr.cast]]) are
|
| 94 |
explicitly used; see also [[over.match.copy]]. A default constructor
|
| 95 |
may be an explicit constructor; such a constructor will be used to
|
| 96 |
+
perform default-initialization or value-initialization [[dcl.init]].
|
| 97 |
|
| 98 |
[*Example 2*:
|
| 99 |
|
| 100 |
``` cpp
|
| 101 |
struct Z {
|
|
|
|
| 118 |
|
| 119 |
— *end example*]
|
| 120 |
|
| 121 |
— *end note*]
|
| 122 |
|
| 123 |
+
A non-explicit copy/move constructor [[class.copy.ctor]] is a converting
|
| 124 |
constructor.
|
| 125 |
|
| 126 |
[*Note 2*: An implicitly-declared copy/move constructor is not an
|
| 127 |
explicit constructor; it may be called for implicit type
|
| 128 |
conversions. — *end note*]
|
| 129 |
|
| 130 |
+
#### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
|
| 131 |
|
| 132 |
A member function of a class `X` having no parameters with a name of the
|
| 133 |
form
|
| 134 |
|
| 135 |
``` bnf
|
| 136 |
conversion-function-id:
|
| 137 |
+
operator conversion-type-id
|
| 138 |
```
|
| 139 |
|
| 140 |
``` bnf
|
| 141 |
conversion-type-id:
|
| 142 |
type-specifier-seq conversion-declaratorₒₚₜ
|
|
|
|
| 149 |
|
| 150 |
specifies a conversion from `X` to the type specified by the
|
| 151 |
*conversion-type-id*. Such functions are called *conversion functions*.
|
| 152 |
A *decl-specifier* in the *decl-specifier-seq* of a conversion function
|
| 153 |
(if any) shall be neither a *defining-type-specifier* nor `static`. The
|
| 154 |
+
type of the conversion function [[dcl.fct]] is “function taking no
|
| 155 |
parameter returning *conversion-type-id*”. A conversion function is
|
| 156 |
never used to convert a (possibly cv-qualified) object to the (possibly
|
| 157 |
cv-qualified) same object type (or a reference to it), to a (possibly
|
| 158 |
cv-qualified) base class of that type (or a reference to it), or to
|
| 159 |
+
cv `void`.[^6]
|
| 160 |
|
| 161 |
[*Example 1*:
|
| 162 |
|
| 163 |
``` cpp
|
| 164 |
struct X {
|
|
|
|
| 176 |
In all three cases the value assigned will be converted by
|
| 177 |
`X::operator int()`.
|
| 178 |
|
| 179 |
— *end example*]
|
| 180 |
|
| 181 |
+
A conversion function may be explicit [[dcl.fct.spec]], in which case it
|
| 182 |
+
is only considered as a user-defined conversion for
|
| 183 |
+
direct-initialization [[dcl.init]]. Otherwise, user-defined conversions
|
| 184 |
+
are not restricted to use in assignments and initializations.
|
|
|
|
| 185 |
|
| 186 |
[*Example 2*:
|
| 187 |
|
| 188 |
``` cpp
|
| 189 |
class Y { };
|
|
|
|
| 191 |
explicit operator Y() const;
|
| 192 |
};
|
| 193 |
|
| 194 |
void h(Z z) {
|
| 195 |
Y y1(z); // OK: direct-initialization
|
| 196 |
+
Y y2 = z; // error: no conversion function candidate for copy-initialization
|
| 197 |
Y y3 = (Y)z; // OK: cast notation
|
| 198 |
}
|
| 199 |
|
| 200 |
void g(X a, X b) {
|
| 201 |
int i = (a) ? 1+a : 0;
|
|
|
|
| 243 |
|
| 244 |
Conversion functions are inherited.
|
| 245 |
|
| 246 |
Conversion functions can be virtual.
|
| 247 |
|
| 248 |
+
A conversion function template shall not have a deduced return type
|
| 249 |
+
[[dcl.spec.auto]].
|
| 250 |
|
| 251 |
[*Example 5*:
|
| 252 |
|
| 253 |
``` cpp
|
| 254 |
struct S {
|