tmp/tmphhkmcwg3/{from.md → to.md}
RENAMED
|
@@ -1,54 +1,59 @@
|
|
| 1 |
#### Operators in expressions <a id="over.match.oper">[[over.match.oper]]</a>
|
| 2 |
|
| 3 |
If no operand of an operator in an expression has a type that is a class
|
| 4 |
or an enumeration, the operator is assumed to be a built-in operator and
|
| 5 |
-
interpreted according to Clause [[expr]].
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
``` cpp
|
| 13 |
struct String {
|
| 14 |
String (const String&);
|
| 15 |
String (const char*);
|
| 16 |
operator const char* ();
|
| 17 |
};
|
| 18 |
String operator + (const String&, const String&);
|
| 19 |
|
| 20 |
-
void f(
|
| 21 |
-
const char* p= "one" + "two"; // ill-formed because neither
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
// class or enumeration types exist that
|
| 25 |
-
// would perform the operation.
|
| 26 |
}
|
| 27 |
```
|
| 28 |
|
|
|
|
|
|
|
| 29 |
If either operand has a type that is a class or an enumeration, a
|
| 30 |
user-defined operator function might be declared that implements this
|
| 31 |
operator or a user-defined conversion can be necessary to convert the
|
| 32 |
operand to a type that is appropriate for a built-in operator. In this
|
| 33 |
case, overload resolution is used to determine which operator function
|
| 34 |
or built-in operator is to be invoked to implement the operator.
|
| 35 |
Therefore, the operator notation is first transformed to the equivalent
|
| 36 |
function-call notation as summarized in Table [[tab:over.rel.op.func]]
|
| 37 |
(where `@` denotes one of the operators covered in the specified
|
| 38 |
-
subclause).
|
|
|
|
| 39 |
|
| 40 |
**Table: Relationship between operator and function call notation** <a id="tab:over.rel.op.func">[tab:over.rel.op.func]</a>
|
| 41 |
|
| 42 |
| Subclause | Expression | As member function | As non-member function |
|
| 43 |
-
| ------------ | ---------- | ------------------ | ---------------------- |
|
| 44 |
-
| (a)
|
| 45 |
-
| (a, b)
|
| 46 |
-
| [[over.ass]] | a=b
|
| 47 |
-
| [[over.sub]] | a[b]
|
| 48 |
-
| [[over.ref]] | a->
|
| 49 |
-
| (a, 0)
|
| 50 |
|
| 51 |
|
| 52 |
For a unary operator `@` with an operand of a type whose cv-unqualified
|
| 53 |
version is `T1`, and for a binary operator `@` with a left operand of a
|
| 54 |
type whose cv-unqualified version is `T1` and a right operand of a type
|
|
@@ -64,14 +69,14 @@ candidates*, are constructed as follows:
|
|
| 64 |
lookup of `operator@` in the context of the expression according to
|
| 65 |
the usual rules for name lookup in unqualified function calls (
|
| 66 |
[[basic.lookup.argdep]]) except that all member functions are ignored.
|
| 67 |
However, if no operand has a class type, only those non-member
|
| 68 |
functions in the lookup set that have a first parameter of type `T1`
|
| 69 |
-
or “reference to
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
- For the operator `,`, the unary operator `&`, or the operator `->`,
|
| 74 |
the built-in candidates set is empty. For all other operators, the
|
| 75 |
built-in candidates include all of the candidate operator functions
|
| 76 |
defined in [[over.built]] that, compared to the given operator,
|
| 77 |
- have the same operator name, and
|
|
@@ -95,10 +100,12 @@ the member candidates, the non-member candidates, and the built-in
|
|
| 95 |
candidates. The argument list contains all of the operands of the
|
| 96 |
operator. The best function from the set of candidate functions is
|
| 97 |
selected according to [[over.match.viable]] and
|
| 98 |
[[over.match.best]].[^6]
|
| 99 |
|
|
|
|
|
|
|
| 100 |
``` cpp
|
| 101 |
struct A {
|
| 102 |
operator int();
|
| 103 |
};
|
| 104 |
A operator+(const A&, const A&);
|
|
@@ -106,18 +113,22 @@ void m() {
|
|
| 106 |
A a, b;
|
| 107 |
a + b; // operator+(a, b) chosen over int(a) + int(b)
|
| 108 |
}
|
| 109 |
```
|
| 110 |
|
|
|
|
|
|
|
| 111 |
If a built-in candidate is selected by overload resolution, the operands
|
| 112 |
of class type are converted to the types of the corresponding parameters
|
| 113 |
of the selected operation function, except that the second standard
|
| 114 |
conversion sequence of a user-defined conversion sequence (
|
| 115 |
[[over.ics.user]]) is not applied. Then the operator is treated as the
|
| 116 |
corresponding built-in operator and interpreted according to Clause
|
| 117 |
[[expr]].
|
| 118 |
|
|
|
|
|
|
|
| 119 |
``` cpp
|
| 120 |
struct X {
|
| 121 |
operator double();
|
| 122 |
};
|
| 123 |
|
|
@@ -127,20 +138,24 @@ struct Y {
|
|
| 127 |
|
| 128 |
int *a = Y() + 100.0; // error: pointer arithmetic requires integral operand
|
| 129 |
int *b = Y() + X(); // error: pointer arithmetic requires integral operand
|
| 130 |
```
|
| 131 |
|
|
|
|
|
|
|
| 132 |
The second operand of operator `->` is ignored in selecting an
|
| 133 |
`operator->` function, and is not an argument when the `operator->`
|
| 134 |
function is called. When `operator->` returns, the operator `->` is
|
| 135 |
applied to the value returned, with the original second operand.[^7]
|
| 136 |
|
| 137 |
If the operator is the operator `,`, the unary operator `&`, or the
|
| 138 |
operator `->`, and there are no viable functions, then the operator is
|
| 139 |
assumed to be the built-in operator and interpreted according to Clause
|
| 140 |
[[expr]].
|
| 141 |
|
|
|
|
|
|
|
| 142 |
The lookup rules for operators in expressions are different than the
|
| 143 |
lookup rules for operator function names in a function call, as shown in
|
| 144 |
the following example:
|
| 145 |
|
| 146 |
``` cpp
|
|
@@ -158,5 +173,7 @@ void B::f() {
|
|
| 158 |
operator+ (a,a); // error: global operator hidden by member
|
| 159 |
a + a; // OK: calls global operator+
|
| 160 |
}
|
| 161 |
```
|
| 162 |
|
|
|
|
|
|
|
|
|
| 1 |
#### Operators in expressions <a id="over.match.oper">[[over.match.oper]]</a>
|
| 2 |
|
| 3 |
If no operand of an operator in an expression has a type that is a class
|
| 4 |
or an enumeration, the operator is assumed to be a built-in operator and
|
| 5 |
+
interpreted according to Clause [[expr]].
|
| 6 |
+
|
| 7 |
+
[*Note 1*: Because `.`, `.*`, and `::` cannot be overloaded, these
|
| 8 |
+
operators are always built-in operators interpreted according to Clause
|
| 9 |
+
[[expr]]. `?:` cannot be overloaded, but the rules in this subclause are
|
| 10 |
+
used to determine the conversions to be applied to the second and third
|
| 11 |
+
operands when they have class or enumeration type (
|
| 12 |
+
[[expr.cond]]). — *end note*]
|
| 13 |
+
|
| 14 |
+
[*Example 1*:
|
| 15 |
|
| 16 |
``` cpp
|
| 17 |
struct String {
|
| 18 |
String (const String&);
|
| 19 |
String (const char*);
|
| 20 |
operator const char* ();
|
| 21 |
};
|
| 22 |
String operator + (const String&, const String&);
|
| 23 |
|
| 24 |
+
void f() {
|
| 25 |
+
const char* p= "one" + "two"; // ill-formed because neither operand has class or enumeration type
|
| 26 |
+
int I = 1 + 1; // always evaluates to 2 even if class or enumeration types exist
|
| 27 |
+
// that would perform the operation.
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
```
|
| 30 |
|
| 31 |
+
— *end example*]
|
| 32 |
+
|
| 33 |
If either operand has a type that is a class or an enumeration, a
|
| 34 |
user-defined operator function might be declared that implements this
|
| 35 |
operator or a user-defined conversion can be necessary to convert the
|
| 36 |
operand to a type that is appropriate for a built-in operator. In this
|
| 37 |
case, overload resolution is used to determine which operator function
|
| 38 |
or built-in operator is to be invoked to implement the operator.
|
| 39 |
Therefore, the operator notation is first transformed to the equivalent
|
| 40 |
function-call notation as summarized in Table [[tab:over.rel.op.func]]
|
| 41 |
(where `@` denotes one of the operators covered in the specified
|
| 42 |
+
subclause). However, the operands are sequenced in the order prescribed
|
| 43 |
+
for the built-in operator (Clause [[expr]]).
|
| 44 |
|
| 45 |
**Table: Relationship between operator and function call notation** <a id="tab:over.rel.op.func">[tab:over.rel.op.func]</a>
|
| 46 |
|
| 47 |
| Subclause | Expression | As member function | As non-member function |
|
| 48 |
+
| ------------ | ---------- | ------------------- | ---------------------- |
|
| 49 |
+
| (a)} |
|
| 50 |
+
| (a, b)} |
|
| 51 |
+
| [[over.ass]] | `a=b` | `(a).operator= (b)` | |
|
| 52 |
+
| [[over.sub]] | `a[b]` | `(a).operator[](b)` | |
|
| 53 |
+
| [[over.ref]] | `a->` | `(a).operator->( )` | |
|
| 54 |
+
| (a, 0)} |
|
| 55 |
|
| 56 |
|
| 57 |
For a unary operator `@` with an operand of a type whose cv-unqualified
|
| 58 |
version is `T1`, and for a binary operator `@` with a left operand of a
|
| 59 |
type whose cv-unqualified version is `T1` and a right operand of a type
|
|
|
|
| 69 |
lookup of `operator@` in the context of the expression according to
|
| 70 |
the usual rules for name lookup in unqualified function calls (
|
| 71 |
[[basic.lookup.argdep]]) except that all member functions are ignored.
|
| 72 |
However, if no operand has a class type, only those non-member
|
| 73 |
functions in the lookup set that have a first parameter of type `T1`
|
| 74 |
+
or “reference to cv `T1`”, when `T1` is an enumeration type, or (if
|
| 75 |
+
there is a right operand) a second parameter of type `T2` or
|
| 76 |
+
“reference to cv `T2`”, when `T2` is an enumeration type, are
|
| 77 |
+
candidate functions.
|
| 78 |
- For the operator `,`, the unary operator `&`, or the operator `->`,
|
| 79 |
the built-in candidates set is empty. For all other operators, the
|
| 80 |
built-in candidates include all of the candidate operator functions
|
| 81 |
defined in [[over.built]] that, compared to the given operator,
|
| 82 |
- have the same operator name, and
|
|
|
|
| 100 |
candidates. The argument list contains all of the operands of the
|
| 101 |
operator. The best function from the set of candidate functions is
|
| 102 |
selected according to [[over.match.viable]] and
|
| 103 |
[[over.match.best]].[^6]
|
| 104 |
|
| 105 |
+
[*Example 2*:
|
| 106 |
+
|
| 107 |
``` cpp
|
| 108 |
struct A {
|
| 109 |
operator int();
|
| 110 |
};
|
| 111 |
A operator+(const A&, const A&);
|
|
|
|
| 113 |
A a, b;
|
| 114 |
a + b; // operator+(a, b) chosen over int(a) + int(b)
|
| 115 |
}
|
| 116 |
```
|
| 117 |
|
| 118 |
+
— *end example*]
|
| 119 |
+
|
| 120 |
If a built-in candidate is selected by overload resolution, the operands
|
| 121 |
of class type are converted to the types of the corresponding parameters
|
| 122 |
of the selected operation function, except that the second standard
|
| 123 |
conversion sequence of a user-defined conversion sequence (
|
| 124 |
[[over.ics.user]]) is not applied. Then the operator is treated as the
|
| 125 |
corresponding built-in operator and interpreted according to Clause
|
| 126 |
[[expr]].
|
| 127 |
|
| 128 |
+
[*Example 3*:
|
| 129 |
+
|
| 130 |
``` cpp
|
| 131 |
struct X {
|
| 132 |
operator double();
|
| 133 |
};
|
| 134 |
|
|
|
|
| 138 |
|
| 139 |
int *a = Y() + 100.0; // error: pointer arithmetic requires integral operand
|
| 140 |
int *b = Y() + X(); // error: pointer arithmetic requires integral operand
|
| 141 |
```
|
| 142 |
|
| 143 |
+
— *end example*]
|
| 144 |
+
|
| 145 |
The second operand of operator `->` is ignored in selecting an
|
| 146 |
`operator->` function, and is not an argument when the `operator->`
|
| 147 |
function is called. When `operator->` returns, the operator `->` is
|
| 148 |
applied to the value returned, with the original second operand.[^7]
|
| 149 |
|
| 150 |
If the operator is the operator `,`, the unary operator `&`, or the
|
| 151 |
operator `->`, and there are no viable functions, then the operator is
|
| 152 |
assumed to be the built-in operator and interpreted according to Clause
|
| 153 |
[[expr]].
|
| 154 |
|
| 155 |
+
[*Note 2*:
|
| 156 |
+
|
| 157 |
The lookup rules for operators in expressions are different than the
|
| 158 |
lookup rules for operator function names in a function call, as shown in
|
| 159 |
the following example:
|
| 160 |
|
| 161 |
``` cpp
|
|
|
|
| 173 |
operator+ (a,a); // error: global operator hidden by member
|
| 174 |
a + a; // OK: calls global operator+
|
| 175 |
}
|
| 176 |
```
|
| 177 |
|
| 178 |
+
— *end note*]
|
| 179 |
+
|