From Jason Turner

[over.oper]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp6bu1ufaj/{from.md → to.md} +169 -184
tmp/tmp6bu1ufaj/{from.md → to.md} RENAMED
@@ -8,38 +8,53 @@ template*. A specialization of an operator function template is also an
8
  operator function. An operator function is said to *implement* the
9
  operator named in its *operator-function-id*.
10
 
11
  ``` bnf
12
  operator-function-id:
13
- 'operator' operator
14
  ```
15
 
16
- [*Note 1*: The last two operators are function call ([[expr.call]])
17
- and subscripting ([[expr.sub]]). The operators `new[]`, `delete[]`,
18
- `()`, and `[]` are formed from more than one token. — *end note*]
 
 
 
 
 
 
 
 
 
 
19
 
20
  Both the unary and binary forms of
21
 
22
- ``` cpp
23
- + - * &
24
  ```
25
 
26
  can be overloaded.
27
 
 
 
28
  The following operators cannot be overloaded:
29
 
30
- ``` cpp
31
- . .* :: ?:
32
  ```
33
 
34
- nor can the preprocessing symbols `#` and `##` (Clause  [[cpp]]).
 
 
 
35
 
36
  Operator functions are usually not called directly; instead they are
37
  invoked to evaluate the operators they implement ([[over.unary]] –
38
  [[over.inc]]). They can be explicitly called, however, using the
39
  *operator-function-id* as the name of the function in the function call
40
- syntax ([[expr.call]]).
41
 
42
  [*Example 1*:
43
 
44
  ``` cpp
45
  complex z = a.operator+(b); // complex z = a+b;
@@ -47,83 +62,133 @@ void* p = operator new(sizeof(int)*n);
47
  ```
48
 
49
  — *end example*]
50
 
51
  The allocation and deallocation functions, `operator` `new`, `operator`
52
- `new[]`, `operator` `delete` and `operator` `delete[]`, are described
53
  completely in  [[basic.stc.dynamic]]. The attributes and restrictions
54
  found in the rest of this subclause do not apply to them unless
55
  explicitly stated in  [[basic.stc.dynamic]].
56
 
 
 
 
 
57
  An operator function shall either be a non-static member function or be
58
  a non-member function that has at least one parameter whose type is a
59
  class, a reference to a class, an enumeration, or a reference to an
60
  enumeration. It is not possible to change the precedence, grouping, or
61
  number of operands of operators. The meaning of the operators `=`,
62
  (unary) `&`, and `,` (comma), predefined for each type, can be changed
63
- for specific class and enumeration types by defining operator functions
64
- that implement these operators. Operator functions are inherited in the
65
- same manner as other base class functions.
 
 
66
 
67
- The identities among certain predefined operators applied to basic types
68
- (for example, `++a` `a+=1`) need not hold for operator functions. Some
69
- predefined operators, such as `+=`, require an operand to be an lvalue
70
- when applied to basic types; this is not required by operator functions.
71
 
72
- An operator function cannot have default arguments (
73
- [[dcl.fct.default]]), except where explicitly stated below. Operator
74
- functions cannot have more or fewer parameters than the number required
75
- for the corresponding operator, as described in the rest of this
76
- subclause.
 
 
 
 
 
77
 
78
  Operators not mentioned explicitly in subclauses  [[over.ass]] through 
79
  [[over.inc]] act as ordinary unary and binary operators obeying the
80
  rules of  [[over.unary]] or  [[over.binary]].
81
 
82
  ### Unary operators <a id="over.unary">[[over.unary]]</a>
83
 
84
- A prefix unary operator shall be implemented by a non-static member
85
- function ([[class.mfct]]) with no parameters or a non-member function
86
- with one parameter. Thus, for any prefix unary operator `@`, `@x` can be
87
- interpreted as either `x.operator@()` or `operator@(x)`. If both forms
88
- of the operator function have been declared, the rules in 
89
- [[over.match.oper]] determine which, if any, interpretation is used.
90
- See  [[over.inc]] for an explanation of the postfix unary operators `++`
91
- and `\dcr`.
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  The unary and binary forms of the same operator are considered to have
94
  the same name.
95
 
96
- [*Note 1*: Consequently, a unary operator can hide a binary operator
97
  from an enclosing scope, and vice versa. — *end note*]
98
 
99
  ### Binary operators <a id="over.binary">[[over.binary]]</a>
100
 
101
- A binary operator shall be implemented either by a non-static member
102
- function ([[class.mfct]]) with one parameter or by a non-member
103
- function with two parameters. Thus, for any binary operator `@`, `x@y`
104
- can be interpreted as either `x.operator@(y)` or `operator@(x,y)`. If
105
- both forms of the operator function have been declared, the rules in 
106
- [[over.match.oper]] determine which, if any, interpretation is used.
107
-
108
- ### Assignment <a id="over.ass">[[over.ass]]</a>
109
-
110
- An assignment operator shall be implemented by a non-static member
111
- function with exactly one parameter. Because a copy assignment operator
112
- `operator=` is implicitly declared for a class if not declared by the
113
- user ([[class.copy]]), a base class assignment operator is always
114
- hidden by the copy assignment operator of the derived class.
115
-
116
- Any assignment operator, even the copy and move assignment operators,
117
- can be virtual.
118
-
119
- [*Note 1*:
120
-
121
- For a derived class `D` with a base class `B` for which a virtual
122
- copy/move assignment has been declared, the copy/move assignment
123
- operator in `D` does not override `B`’s virtual copy/move assignment
124
- operator.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  [*Example 1*:
127
 
128
  ``` cpp
129
  struct B {
@@ -151,44 +216,50 @@ void f() {
151
 
152
  — *end note*]
153
 
154
  ### Function call <a id="over.call">[[over.call]]</a>
155
 
156
- `operator()`
157
-
158
- shall be a non-static member function with an arbitrary number of
159
- parameters. It can have default arguments. It implements the function
160
- call syntax
161
 
162
  ``` bnf
163
  postfix-expression '(' expression-listₒₚₜ ')'
164
  ```
165
 
166
- where the *postfix-expression* evaluates to a class object and the
167
- possibly empty *expression-list* matches the parameter list of an
168
- `operator()` member function of the class. Thus, a call `x(arg1,...)` is
169
- interpreted as `x.operator()(arg1, ...)` for a class object `x` of type
170
- `T` if `T::operator()(T1,` `T2,` `T3)` exists and if the operator is
171
- selected as the best match function by the overload resolution
172
- mechanism ([[over.match.best]]).
 
 
 
 
 
 
 
173
 
174
  ### Subscripting <a id="over.sub">[[over.sub]]</a>
175
 
176
- `operator[]`
177
-
178
- shall be a non-static member function with exactly one parameter. It
179
- implements the subscripting syntax
180
 
181
  ``` bnf
182
  postfix-expression '[' expr-or-braced-init-list ']'
183
  ```
184
 
185
- Thus, a subscripting expression `x[y]` is interpreted as
186
- `x.operator[](y)` for a class object `x` of type `T` if
187
- `T::operator[](T1)` exists and if the operator is selected as the best
188
- match function by the overload resolution mechanism (
189
- [[over.match.best]]).
 
 
190
 
191
  [*Example 1*:
192
 
193
  ``` cpp
194
  struct X {
@@ -202,37 +273,36 @@ a[{1,2,3}] = 7; // error: built-in subscript operator
202
 
203
  — *end example*]
204
 
205
  ### Class member access <a id="over.ref">[[over.ref]]</a>
206
 
207
- `operator->`
208
-
209
- shall be a non-static member function taking no parameters. It
210
- implements the class member access syntax that uses `->`.
211
 
212
  ``` bnf
213
- postfix-expression '->' 'template'ₒₚₜ id-expression
214
- postfix-expression '->' pseudo-destructor-name
215
  ```
216
 
217
- An expression `x->m` is interpreted as `(x.operator->())->m` for a class
218
- object `x` of type `T` if `T::operator->()` exists and if the operator
219
- is selected as the best match function by the overload resolution
220
- mechanism ([[over.match]]).
 
 
221
 
222
  ### Increment and decrement <a id="over.inc">[[over.inc]]</a>
223
 
224
- The user-defined function called `operator++` implements the prefix and
225
- postfix `++` operator. If this function is a non-static member function
226
- with no parameters, or a non-member function with one parameter, it
227
- defines the prefix increment operator `++` for objects of that type. If
228
- the function is a non-static member function with one parameter (which
229
- shall be of type `int`) or a non-member function with two parameters
230
- (the second of which shall be of type `int`), it defines the postfix
231
- increment operator `++` for objects of that type. When the postfix
232
- increment is called as a result of using the `++` operator, the `int`
233
- argument will have value zero.[^12]
234
 
235
  [*Example 1*:
236
 
237
  ``` cpp
238
  struct X {
@@ -257,93 +327,8 @@ void f(X a, Y b) {
257
  }
258
  ```
259
 
260
  — *end example*]
261
 
262
- The prefix and postfix decrement operators `-{-}` are handled
263
- analogously.
264
-
265
- ### User-defined literals <a id="over.literal">[[over.literal]]</a>
266
-
267
- ``` bnf
268
- literal-operator-id:
269
- 'operator' string-literal identifier
270
- 'operator' user-defined-string-literal
271
- ```
272
-
273
- The *string-literal* or *user-defined-string-literal* in a
274
- *literal-operator-id* shall have no *encoding-prefix* and shall contain
275
- no characters other than the implicit terminating `'\0'`. The
276
- *ud-suffix* of the *user-defined-string-literal* or the *identifier* in
277
- a *literal-operator-id* is called a *literal suffix identifier*. Some
278
- literal suffix identifiers are reserved for future standardization; see 
279
- [[usrlit.suffix]]. A declaration whose *literal-operator-id* uses such a
280
- literal suffix identifier is ill-formed, no diagnostic required.
281
-
282
- A declaration whose *declarator-id* is a *literal-operator-id* shall be
283
- a declaration of a namespace-scope function or function template (it
284
- could be a friend function ([[class.friend]])), an explicit
285
- instantiation or specialization of a function template, or a
286
- *using-declaration* ([[namespace.udecl]]). A function declared with a
287
- *literal-operator-id* is a *literal operator*. A function template
288
- declared with a *literal-operator-id* is a *literal operator template*.
289
-
290
- The declaration of a literal operator shall have a
291
- *parameter-declaration-clause* equivalent to one of the following:
292
-
293
- ``` cpp
294
- const char*
295
- unsigned long long int
296
- long double
297
- char
298
- wchar_t
299
- char16_t
300
- char32_t
301
- const char*, std::size_t
302
- const wchar_t*, std::size_t
303
- const char16_t*, std::size_t
304
- const char32_t*, std::size_t
305
- ```
306
-
307
- If a parameter has a default argument ([[dcl.fct.default]]), the
308
- program is ill-formed.
309
-
310
- A *raw literal operator* is a literal operator with a single parameter
311
- whose type is `const char*`.
312
-
313
- The declaration of a literal operator template shall have an empty
314
- *parameter-declaration-clause* and its *template-parameter-list* shall
315
- have a single *template-parameter* that is a non-type template parameter
316
- pack ([[temp.variadic]]) with element type `char`.
317
-
318
- Literal operators and literal operator templates shall not have C
319
- language linkage.
320
-
321
- [*Note 1*: Literal operators and literal operator templates are usually
322
- invoked implicitly through user-defined literals ([[lex.ext]]).
323
- However, except for the constraints described above, they are ordinary
324
- namespace-scope functions and function templates. In particular, they
325
- are looked up like ordinary functions and function templates and they
326
- follow the same overload resolution rules. Also, they can be declared
327
- `inline` or `constexpr`, they may have internal or external linkage,
328
- they can be called explicitly, their addresses can be taken,
329
- etc. — *end note*]
330
-
331
- [*Example 1*:
332
-
333
- ``` cpp
334
- void operator "" _km(long double); // OK
335
- string operator "" _i18n(const char*, std::size_t); // OK
336
- template <char...> double operator "" _\u03C0(); // OK: UCN for lowercase pi
337
- float operator ""_e(const char*); // OK
338
- float operator ""E(const char*); // error: reserved literal suffix~([usrlit.suffix], [lex.ext])
339
- double operator""_Bq(long double); // OK: does not use the reserved identifier _Bq~([lex.name])
340
- double operator"" _Bq(long double); // uses the reserved identifier _Bq~([lex.name])
341
- float operator " " B(const char*); // error: non-empty string-literal
342
- string operator "" 5X(const char*, std::size_t); // error: invalid literal suffix identifier
343
- double operator "" _miles(double); // error: invalid parameter-declaration-clause
344
- template <char...> int operator "" _j(const char*); // error: invalid parameter-declaration-clause
345
- extern "C" void operator "" _m(long double); // error: C language linkage
346
- ```
347
-
348
- — *end example*]
349
 
 
8
  operator function. An operator function is said to *implement* the
9
  operator named in its *operator-function-id*.
10
 
11
  ``` bnf
12
  operator-function-id:
13
+ operator operator
14
  ```
15
 
16
+ ``` bnf
17
+ %% Ed. note: character protrusion would misalign various operators.
18
+ operator: one of
19
+ 'new delete new[] delete[] co_await ( ) [ ] -> ->*'
20
+ '~ ! + - * / % ^ &'
21
+ '| = += -= *= /= %= ^= &='
22
+ '|= == != < > <= >= <=> &&'
23
+ '|| << >> <<= >>= ++ -- ,'
24
+ ```
25
+
26
+ [*Note 1*: The operators `new[]`, `delete[]`, `()`, and `[]` are formed
27
+ from more than one token. The latter two operators are function call
28
+ [[expr.call]] and subscripting [[expr.sub]]. — *end note*]
29
 
30
  Both the unary and binary forms of
31
 
32
+ ``` bnf
33
+ '+ - * &'
34
  ```
35
 
36
  can be overloaded.
37
 
38
+ [*Note 2*:
39
+
40
  The following operators cannot be overloaded:
41
 
42
+ ``` bnf
43
+ '. .* :: ?:'
44
  ```
45
 
46
+ nor can the preprocessing symbols `#` [[cpp.stringize]] and `##`
47
+ [[cpp.concat]].
48
+
49
+ — *end note*]
50
 
51
  Operator functions are usually not called directly; instead they are
52
  invoked to evaluate the operators they implement ([[over.unary]] –
53
  [[over.inc]]). They can be explicitly called, however, using the
54
  *operator-function-id* as the name of the function in the function call
55
+ syntax [[expr.call]].
56
 
57
  [*Example 1*:
58
 
59
  ``` cpp
60
  complex z = a.operator+(b); // complex z = a+b;
 
62
  ```
63
 
64
  — *end example*]
65
 
66
  The allocation and deallocation functions, `operator` `new`, `operator`
67
+ `new[]`, `operator` `delete`, and `operator` `delete[]`, are described
68
  completely in  [[basic.stc.dynamic]]. The attributes and restrictions
69
  found in the rest of this subclause do not apply to them unless
70
  explicitly stated in  [[basic.stc.dynamic]].
71
 
72
+ The `co_await` operator is described completely in  [[expr.await]]. The
73
+ attributes and restrictions found in the rest of this subclause do not
74
+ apply to it unless explicitly stated in  [[expr.await]].
75
+
76
  An operator function shall either be a non-static member function or be
77
  a non-member function that has at least one parameter whose type is a
78
  class, a reference to a class, an enumeration, or a reference to an
79
  enumeration. It is not possible to change the precedence, grouping, or
80
  number of operands of operators. The meaning of the operators `=`,
81
  (unary) `&`, and `,` (comma), predefined for each type, can be changed
82
+ for specific class types by defining operator functions that implement
83
+ these operators. Likewise, the meaning of the operators (unary) `&` and
84
+ `,` (comma) can be changed for specific enumeration types. Operator
85
+ functions are inherited in the same manner as other base class
86
+ functions.
87
 
88
+ An operator function shall be a prefix unary, binary, function call,
89
+ subscripting, class member access, increment, or decrement operator
90
+ function.
 
91
 
92
+ [*Note 3*: The identities among certain predefined operators applied to
93
+ basic types (for example, `++a` `a+=1`) need not hold for operator
94
+ functions. Some predefined operators, such as `+=`, require an operand
95
+ to be an lvalue when applied to basic types; this is not required by
96
+ operator functions. — *end note*]
97
+
98
+ An operator function cannot have default arguments [[dcl.fct.default]],
99
+ except where explicitly stated below. Operator functions cannot have
100
+ more or fewer parameters than the number required for the corresponding
101
+ operator, as described in the rest of this subclause.
102
 
103
  Operators not mentioned explicitly in subclauses  [[over.ass]] through 
104
  [[over.inc]] act as ordinary unary and binary operators obeying the
105
  rules of  [[over.unary]] or  [[over.binary]].
106
 
107
  ### Unary operators <a id="over.unary">[[over.unary]]</a>
108
 
109
+ A *prefix unary operator function* is a function named `operator@` for a
110
+ prefix *unary-operator* `@` [[expr.unary.op]] that is either a
111
+ non-static member function [[class.mfct]] with no parameters or a
112
+ non-member function with one parameter. For a *unary-expression* of the
113
+ form `@ cast-expression`, the operator function is selected by overload
114
+ resolution [[over.match.oper]]. If a member function is selected, the
115
+ expression is interpreted as
116
+
117
+ ``` bnf
118
+ cast-expression '.' operator '@' '('')'
119
+ ```
120
+
121
+ Otherwise, if a non-member function is selected, the expression is
122
+ interpreted as
123
+
124
+ ``` bnf
125
+ operator '@' '(' cast-expression ')'
126
+ ```
127
+
128
+ [*Note 1*: The operators `++` and `\dcr` [[expr.pre.incr]] are
129
+ described in  [[over.inc]]. — *end note*]
130
 
131
  The unary and binary forms of the same operator are considered to have
132
  the same name.
133
 
134
+ [*Note 2*: Consequently, a unary operator can hide a binary operator
135
  from an enclosing scope, and vice versa. — *end note*]
136
 
137
  ### Binary operators <a id="over.binary">[[over.binary]]</a>
138
 
139
+ A *binary operator function* is a function named `operator@` for a
140
+ binary operator `@` that is either a non-static member function
141
+ [[class.mfct]] with one parameter or a non-member function with two
142
+ parameters. For an expression `x @ y` with subexpressions x and y, the
143
+ operator function is selected by overload resolution
144
+ [[over.match.oper]]. If a member function is selected, the expression is
145
+ interpreted as
146
+
147
+ ``` bnf
148
+ x '.' operator '@' '(' y ')'
149
+ ```
150
+
151
+ Otherwise, if a non-member function is selected, the expression is
152
+ interpreted as
153
+
154
+ ``` bnf
155
+ operator '@' '(' x ',' y ')'
156
+ ```
157
+
158
+ An *equality operator function* is an operator function for an equality
159
+ operator [[expr.eq]]. A *relational operator function* is an operator
160
+ function for a relational operator [[expr.rel]]. A
161
+ *three-way comparison operator function* is an operator function for the
162
+ three-way comparison operator [[expr.spaceship]]. A
163
+ *comparison operator function* is an equality operator function, a
164
+ relational operator function, or a three-way comparison operator
165
+ function.
166
+
167
+ #### Simple assignment <a id="over.ass">[[over.ass]]</a>
168
+
169
+ A *simple assignment operator function* is a binary operator function
170
+ named `operator=`. A simple assignment operator function shall be a
171
+ non-static member function.
172
+
173
+ [*Note 1*: Because only standard conversion sequences are considered
174
+ when converting to the left operand of an assignment operation
175
+ [[over.best.ics]], an expression `x = y` with a subexpression x of class
176
+ type is always interpreted as `x.operator=(y)`. — *end note*]
177
+
178
+ [*Note 2*: Since a copy assignment operator is implicitly declared for
179
+ a class if not declared by the user [[class.copy.assign]], a base class
180
+ assignment operator function is always hidden by the copy assignment
181
+ operator function of the derived class. — *end note*]
182
+
183
+ [*Note 3*:
184
+
185
+ Any assignment operator function, even the copy and move assignment
186
+ operators, can be virtual. For a derived class `D` with a base class `B`
187
+ for which a virtual copy/move assignment has been declared, the
188
+ copy/move assignment operator in `D` does not override `B`’s virtual
189
+ copy/move assignment operator.
190
 
191
  [*Example 1*:
192
 
193
  ``` cpp
194
  struct B {
 
216
 
217
  — *end note*]
218
 
219
  ### Function call <a id="over.call">[[over.call]]</a>
220
 
221
+ A *function call operator function* is a function named `operator()`
222
+ that is a non-static member function with an arbitrary number of
223
+ parameters. It may have default arguments. For an expression of the form
 
 
224
 
225
  ``` bnf
226
  postfix-expression '(' expression-listₒₚₜ ')'
227
  ```
228
 
229
+ where the *postfix-expression* is of class type, the operator function
230
+ is selected by overload resolution [[over.call.object]]. If a surrogate
231
+ call function for a conversion function named `operator`
232
+ *conversion-type-id* is selected, the expression is interpreted as
233
+
234
+ ``` bnf
235
+ postfix-expression '.' operator conversion-type-id '('')' '(' expression-listₒₚₜ ')'
236
+ ```
237
+
238
+ Otherwise, the expression is interpreted as
239
+
240
+ ``` bnf
241
+ postfix-expression '.' operator '('')' '(' expression-listₒₚₜ ')'
242
+ ```
243
 
244
  ### Subscripting <a id="over.sub">[[over.sub]]</a>
245
 
246
+ A *subscripting operator function* is a function named `operator[]` that
247
+ is a non-static member function with exactly one parameter. For an
248
+ expression of the form
 
249
 
250
  ``` bnf
251
  postfix-expression '[' expr-or-braced-init-list ']'
252
  ```
253
 
254
+ the operator function is selected by overload resolution
255
+ [[over.match.oper]]. If a member function is selected, the expression is
256
+ interpreted as
257
+
258
+ ``` bnf
259
+ postfix-expression . operator '['']' '(' expr-or-braced-init-list ')'
260
+ ```
261
 
262
  [*Example 1*:
263
 
264
  ``` cpp
265
  struct X {
 
273
 
274
  — *end example*]
275
 
276
  ### Class member access <a id="over.ref">[[over.ref]]</a>
277
 
278
+ A *class member access operator function* is a function named
279
+ `operator->` that is a non-static member function taking no parameters.
280
+ For an expression of the form
 
281
 
282
  ``` bnf
283
+ postfix-expression '->' templateₒₚₜ id-expression
 
284
  ```
285
 
286
+ the operator function is selected by overload resolution
287
+ [[over.match.oper]], and the expression is interpreted as
288
+
289
+ ``` bnf
290
+ '(' postfix-expression . operator '->' '('')' ')' '->' templateₒₚₜ id-expression
291
+ ```
292
 
293
  ### Increment and decrement <a id="over.inc">[[over.inc]]</a>
294
 
295
+ An *increment operator function* is a function named `operator++`. If
296
+ this function is a non-static member function with no parameters, or a
297
+ non-member function with one parameter, it defines the prefix increment
298
+ operator `++` for objects of that type. If the function is a non-static
299
+ member function with one parameter (which shall be of type `int`) or a
300
+ non-member function with two parameters (the second of which shall be of
301
+ type `int`), it defines the postfix increment operator `++` for objects
302
+ of that type. When the postfix increment is called as a result of using
303
+ the `++` operator, the `int` argument will have value zero.[^11]
 
304
 
305
  [*Example 1*:
306
 
307
  ``` cpp
308
  struct X {
 
327
  }
328
  ```
329
 
330
  — *end example*]
331
 
332
+ A *decrement operator function* is a function named `operator\dcr` and
333
+ is handled analogously to an increment operator function.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
334