From Jason Turner

[over.oper]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpf9fsc4e2/{from.md → to.md} +68 -56
tmp/tmpf9fsc4e2/{from.md → to.md} RENAMED
@@ -1,14 +1,16 @@
1
  ## Overloaded operators <a id="over.oper">[[over.oper]]</a>
2
 
3
- A function declaration having one of the following
4
- *operator-function-id*s as its name declares an *operator function*. A
5
- function template declaration having one of the following
6
- *operator-function-id*s as its name declares an *operator function
7
- 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
  ```
@@ -64,26 +66,30 @@ void* p = operator new(sizeof(int)*n);
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
@@ -96,25 +102,25 @@ 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
 
@@ -123,26 +129,26 @@ 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 ')'
@@ -217,69 +223,74 @@ void f() {
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 {
266
  Z operator[](std::initializer_list<int>);
 
267
  };
268
  X x;
269
- x[{1,2,3}] = 7; // OK: meaning x.operator[]({1,2,3\)}
 
270
  int a[10];
271
  a[{1,2,3}] = 7; // error: built-in subscript operator
 
272
  ```
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
 
@@ -291,18 +302,19 @@ the operator function is selected by overload resolution
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,8 +339,8 @@ void f(X a, Y b) {
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
 
 
1
  ## Overloaded operators <a id="over.oper">[[over.oper]]</a>
2
 
3
+ ### General <a id="over.oper.general">[[over.oper.general]]</a>
4
+
5
+ A declaration whose *declarator-id* is an *operator-function-id* shall
6
+ declare a function or function template or an explicit instantiation or
7
+ specialization of a function template. A function so declared is an
8
+ *operator function*. A function template so declared is an *operator
9
+ function template*. A specialization of an operator function template is
10
+ also an operator function. An operator function is said to *implement*
11
+ the operator named in its *operator-function-id*.
12
 
13
  ``` bnf
14
  operator-function-id:
15
  operator operator
16
  ```
 
66
  — *end example*]
67
 
68
  The allocation and deallocation functions, `operator` `new`, `operator`
69
  `new[]`, `operator` `delete`, and `operator` `delete[]`, are described
70
  completely in  [[basic.stc.dynamic]]. The attributes and restrictions
71
+ found in the rest of [[over.oper]] do not apply to them unless
72
  explicitly stated in  [[basic.stc.dynamic]].
73
 
74
  The `co_await` operator is described completely in  [[expr.await]]. The
75
+ attributes and restrictions found in the rest of [[over.oper]] do not
76
  apply to it unless explicitly stated in  [[expr.await]].
77
 
78
+ An operator function shall either
79
+
80
+ - be a member function or
81
+ - be a non-member function that has at least one non-object parameter
82
+ whose type is a class, a reference to a class, an enumeration, or a
83
+ reference to an enumeration.
84
+
85
+ It is not possible to change the precedence, grouping, or number of
86
+ operands of operators. The meaning of the operators `=`, (unary) `&`,
87
+ and `,` (comma), predefined for each type, can be changed for specific
88
+ class types by defining operator functions that implement these
89
+ operators. Likewise, the meaning of the operators (unary) `&` and `,`
90
+ (comma) can be changed for specific enumeration types. Operator
91
  functions are inherited in the same manner as other base class
92
  functions.
93
 
94
  An operator function shall be a prefix unary, binary, function call,
95
  subscripting, class member access, increment, or decrement operator
 
102
  operator functions. — *end note*]
103
 
104
  An operator function cannot have default arguments [[dcl.fct.default]],
105
  except where explicitly stated below. Operator functions cannot have
106
  more or fewer parameters than the number required for the corresponding
107
+ operator, as described in the rest of [[over.oper]].
108
 
109
  Operators not mentioned explicitly in subclauses  [[over.ass]] through 
110
  [[over.inc]] act as ordinary unary and binary operators obeying the
111
  rules of  [[over.unary]] or  [[over.binary]].
112
 
113
  ### Unary operators <a id="over.unary">[[over.unary]]</a>
114
 
115
  A *prefix unary operator function* is a function named `operator@` for a
116
  prefix *unary-operator* `@` [[expr.unary.op]] that is either a
117
+ non-static member function [[class.mfct]] with no non-object parameters
118
+ or a non-member function with one parameter. For a *unary-expression* of
119
+ the form `@ cast-expression`, the operator function is selected by
120
+ overload resolution [[over.match.oper]]. If a member function is
121
+ selected, the expression is interpreted as
122
 
123
  ``` bnf
124
  cast-expression '.' operator '@' '('')'
125
  ```
126
 
 
129
 
130
  ``` bnf
131
  operator '@' '(' cast-expression ')'
132
  ```
133
 
134
+ [*Note 1*: The operators `++` and `--` [[expr.pre.incr]] are described
135
+ in  [[over.inc]]. — *end note*]
136
 
137
+ [*Note 2*: The unary and binary forms of the same operator have the
138
+ same name. Consequently, a unary operator can hide a binary operator
 
 
139
  from an enclosing scope, and vice versa. — *end note*]
140
 
141
  ### Binary operators <a id="over.binary">[[over.binary]]</a>
142
 
143
+ #### General <a id="over.binary.general">[[over.binary.general]]</a>
144
+
145
  A *binary operator function* is a function named `operator@` for a
146
  binary operator `@` that is either a non-static member function
147
+ [[class.mfct]] with one non-object parameter or a non-member function
148
+ with two parameters. For an expression `x @ y` with subexpressions x and
149
+ y, the operator function is selected by overload resolution
150
  [[over.match.oper]]. If a member function is selected, the expression is
151
  interpreted as
152
 
153
  ``` bnf
154
  x '.' operator '@' '(' y ')'
 
223
  — *end note*]
224
 
225
  ### Function call <a id="over.call">[[over.call]]</a>
226
 
227
  A *function call operator function* is a function named `operator()`
228
+ that is a member function with an arbitrary number of parameters. It may
229
+ have default arguments. For an expression of the form
230
 
231
  ``` bnf
232
  postfix-expression '(' expression-listₒₚₜ ')'
233
  ```
234
 
235
  where the *postfix-expression* is of class type, the operator function
236
  is selected by overload resolution [[over.call.object]]. If a surrogate
237
+ call function is selected, let e be the result of invoking the
238
+ corresponding conversion operator function on the *postfix-expression*;
239
+
240
+ the expression is interpreted as
241
 
242
  ``` bnf
243
+ e '(' expression-listₒₚₜ ')'
244
  ```
245
 
246
  Otherwise, the expression is interpreted as
247
 
248
  ``` bnf
249
  postfix-expression '.' operator '('')' '(' expression-listₒₚₜ ')'
250
  ```
251
 
252
  ### Subscripting <a id="over.sub">[[over.sub]]</a>
253
 
254
+ A *subscripting operator function* is a member function named
255
+ `operator[]` with an arbitrary number of parameters. It may have default
256
+ arguments. For an expression of the form
257
 
258
  ``` bnf
259
+ postfix-expression '[' expression-listₒₚₜ ']'
260
  ```
261
 
262
  the operator function is selected by overload resolution
263
  [[over.match.oper]]. If a member function is selected, the expression is
264
  interpreted as
265
 
266
  ``` bnf
267
+ postfix-expression . operator '['']' '(' expression-listₒₚₜ ')'
268
  ```
269
 
270
  [*Example 1*:
271
 
272
  ``` cpp
273
  struct X {
274
  Z operator[](std::initializer_list<int>);
275
+ Z operator[](auto...);
276
  };
277
  X x;
278
+ x[{1,2,3}] = 7; // OK, meaning x.operator[]({1,2,3\)}
279
+ x[1,2,3] = 7; // OK, meaning x.operator[](1,2,3)
280
  int a[10];
281
  a[{1,2,3}] = 7; // error: built-in subscript operator
282
+ a[1,2,3] = 7; // error: built-in subscript operator
283
  ```
284
 
285
  — *end example*]
286
 
287
  ### Class member access <a id="over.ref">[[over.ref]]</a>
288
 
289
  A *class member access operator function* is a function named
290
+ `operator->` that is a non-static member function taking no non-object
291
+ parameters. For an expression of the form
292
 
293
  ``` bnf
294
  postfix-expression '->' templateₒₚₜ id-expression
295
  ```
296
 
 
302
  ```
303
 
304
  ### Increment and decrement <a id="over.inc">[[over.inc]]</a>
305
 
306
  An *increment operator function* is a function named `operator++`. If
307
+ this function is a non-static member function with no non-object
308
+ parameters, or a non-member function with one parameter, it defines the
309
+ prefix increment operator `++` for objects of that type. If the function
310
+ is a non-static member function with one non-object parameter (which
311
+ shall be of type `int`) or a non-member function with two parameters
312
+ (the second of which shall be of type `int`), it defines the postfix
313
+ increment operator `++` for objects of that type. When the postfix
314
+ increment is called as a result of using the `++` operator, the `int`
315
+ argument will have value zero.[^9]
316
 
317
  [*Example 1*:
318
 
319
  ``` cpp
320
  struct X {
 
339
  }
340
  ```
341
 
342
  — *end example*]
343
 
344
+ A *decrement operator function* is a function named `operator--` and is
345
+ handled analogously to an increment operator function.
346