tmp/tmp1oxkdcrz/{from.md → to.md}
RENAMED
|
@@ -1,10 +1,7 @@
|
|
| 1 |
#### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
|
| 2 |
|
| 3 |
-
A member function of a class `X` having no parameters with a name of the
|
| 4 |
-
form
|
| 5 |
-
|
| 6 |
``` bnf
|
| 7 |
conversion-function-id:
|
| 8 |
operator conversion-type-id
|
| 9 |
```
|
| 10 |
|
|
@@ -16,20 +13,44 @@ conversion-type-id:
|
|
| 16 |
``` bnf
|
| 17 |
conversion-declarator:
|
| 18 |
ptr-operator conversion-declaratorₒₚₜ
|
| 19 |
```
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
specifies a conversion from `X` to the type specified by the
|
| 22 |
-
*conversion-type-id*
|
| 23 |
-
|
| 24 |
-
(if any) shall be
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
cv
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
[*Example 1*:
|
| 33 |
|
| 34 |
``` cpp
|
| 35 |
struct X {
|
|
@@ -61,13 +82,13 @@ class Y { };
|
|
| 61 |
struct Z {
|
| 62 |
explicit operator Y() const;
|
| 63 |
};
|
| 64 |
|
| 65 |
void h(Z z) {
|
| 66 |
-
Y y1(z); // OK
|
| 67 |
Y y2 = z; // error: no conversion function candidate for copy-initialization
|
| 68 |
-
Y y3 = (Y)z; // OK
|
| 69 |
}
|
| 70 |
|
| 71 |
void g(X a, X b) {
|
| 72 |
int i = (a) ? 1+a : 0;
|
| 73 |
int j = (a&&b) ? a+b : i;
|
|
@@ -110,18 +131,47 @@ operator int [[noreturn]] (); // error: noreturn attribute applied to a type
|
|
| 110 |
|
| 111 |
— *end example*]
|
| 112 |
|
| 113 |
— *end note*]
|
| 114 |
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
Conversion functions can be virtual.
|
| 118 |
|
| 119 |
A conversion function template shall not have a deduced return type
|
| 120 |
[[dcl.spec.auto]].
|
| 121 |
|
| 122 |
-
[*Example
|
| 123 |
|
| 124 |
``` cpp
|
| 125 |
struct S {
|
| 126 |
operator auto() const { return 10; } // OK
|
| 127 |
template<class T>
|
|
|
|
| 1 |
#### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
|
| 2 |
|
|
|
|
|
|
|
|
|
|
| 3 |
``` bnf
|
| 4 |
conversion-function-id:
|
| 5 |
operator conversion-type-id
|
| 6 |
```
|
| 7 |
|
|
|
|
| 13 |
``` bnf
|
| 14 |
conversion-declarator:
|
| 15 |
ptr-operator conversion-declaratorₒₚₜ
|
| 16 |
```
|
| 17 |
|
| 18 |
+
A declaration whose *declarator-id* has an *unqualified-id* that is a
|
| 19 |
+
*conversion-function-id* declares a *conversion function*; its
|
| 20 |
+
*declarator* shall be a function declarator [[dcl.fct]] of the form
|
| 21 |
+
|
| 22 |
+
``` bnf
|
| 23 |
+
ptr-declarator '(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
|
| 24 |
+
ref-qualifier-seqₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 25 |
+
```
|
| 26 |
+
|
| 27 |
+
where the *ptr-declarator* consists solely of an *id-expression*, an
|
| 28 |
+
optional *attribute-specifier-seq*, and optional surrounding
|
| 29 |
+
parentheses, and the *id-expression* has one of the following forms:
|
| 30 |
+
|
| 31 |
+
- in a *member-declaration* that belongs to the *member-specification*
|
| 32 |
+
of a class or class template but is not a friend declaration
|
| 33 |
+
[[class.friend]], the *id-expression* is a *conversion-function-id*;
|
| 34 |
+
- otherwise, the *id-expression* is a *qualified-id* whose
|
| 35 |
+
*unqualified-id* is a *conversion-function-id*.
|
| 36 |
+
|
| 37 |
+
A conversion function shall have no non-object parameters and shall be a
|
| 38 |
+
non-static member function of a class or class template `X`; it
|
| 39 |
specifies a conversion from `X` to the type specified by the
|
| 40 |
+
*conversion-type-id*, interpreted as a *type-id* [[dcl.name]]. A
|
| 41 |
+
*decl-specifier* in the *decl-specifier-seq* of a conversion function
|
| 42 |
+
(if any) shall not be a *defining-type-specifier*.
|
| 43 |
+
|
| 44 |
+
The type of the conversion function is “`noexcept`ₒₚₜ function taking
|
| 45 |
+
no parameter *cv-qualifier-seq*ₒₚₜ *ref-qualifier*ₒₚₜ returning
|
| 46 |
+
*conversion-type-id*”.
|
| 47 |
+
|
| 48 |
+
A conversion function is never used to convert a (possibly cv-qualified)
|
| 49 |
+
object to the (possibly cv-qualified) same object type (or a reference
|
| 50 |
+
to it), to a (possibly cv-qualified) base class of that type (or a
|
| 51 |
+
reference to it), or to cv `void`.[^6]
|
| 52 |
|
| 53 |
[*Example 1*:
|
| 54 |
|
| 55 |
``` cpp
|
| 56 |
struct X {
|
|
|
|
| 82 |
struct Z {
|
| 83 |
explicit operator Y() const;
|
| 84 |
};
|
| 85 |
|
| 86 |
void h(Z z) {
|
| 87 |
+
Y y1(z); // OK, direct-initialization
|
| 88 |
Y y2 = z; // error: no conversion function candidate for copy-initialization
|
| 89 |
+
Y y3 = (Y)z; // OK, cast notation
|
| 90 |
}
|
| 91 |
|
| 92 |
void g(X a, X b) {
|
| 93 |
int i = (a) ? 1+a : 0;
|
| 94 |
int j = (a&&b) ? a+b : i;
|
|
|
|
| 131 |
|
| 132 |
— *end example*]
|
| 133 |
|
| 134 |
— *end note*]
|
| 135 |
|
| 136 |
+
[*Note 2*:
|
| 137 |
+
|
| 138 |
+
A conversion function in a derived class hides only conversion functions
|
| 139 |
+
in base classes that convert to the same type. A conversion function
|
| 140 |
+
template with a dependent return type hides only templates in base
|
| 141 |
+
classes that correspond to it [[class.member.lookup]]; otherwise, it
|
| 142 |
+
hides and is hidden as a non-template function. Function overload
|
| 143 |
+
resolution [[over.match.best]] selects the best conversion function to
|
| 144 |
+
perform the conversion.
|
| 145 |
+
|
| 146 |
+
[*Example 5*:
|
| 147 |
+
|
| 148 |
+
``` cpp
|
| 149 |
+
struct X {
|
| 150 |
+
operator int();
|
| 151 |
+
};
|
| 152 |
+
|
| 153 |
+
struct Y : X {
|
| 154 |
+
operator char();
|
| 155 |
+
};
|
| 156 |
+
|
| 157 |
+
void f(Y& a) {
|
| 158 |
+
if (a) { // error: ambiguous between X::operator int() and Y::operator char()
|
| 159 |
+
}
|
| 160 |
+
}
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
— *end example*]
|
| 164 |
+
|
| 165 |
+
— *end note*]
|
| 166 |
|
| 167 |
Conversion functions can be virtual.
|
| 168 |
|
| 169 |
A conversion function template shall not have a deduced return type
|
| 170 |
[[dcl.spec.auto]].
|
| 171 |
|
| 172 |
+
[*Example 6*:
|
| 173 |
|
| 174 |
``` cpp
|
| 175 |
struct S {
|
| 176 |
operator auto() const { return 10; } // OK
|
| 177 |
template<class T>
|