From Jason Turner

[over.oper]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpdqkntq5k/{from.md → to.md} +54 -31
tmp/tmpdqkntq5k/{from.md → to.md} RENAMED
@@ -11,13 +11,13 @@ operator named in its *operator-function-id*.
11
  ``` bnf
12
  operator-function-id:
13
  'operator' operator
14
  ```
15
 
16
- The last two operators are function call ([[expr.call]]) and
17
- subscripting ([[expr.sub]]). The operators `new[]`, `delete[]`, `()`,
18
- and `[]` are formed from more than one token.
19
 
20
  Both the unary and binary forms of
21
 
22
  ``` cpp
23
  + - * &
@@ -37,15 +37,19 @@ 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
  ``` cpp
43
  complex z = a.operator+(b); // complex z = a+b;
44
  void* p = operator new(sizeof(int)*n);
45
  ```
46
 
 
 
47
  The allocation and deallocation functions, `operator` `new`, `operator`
48
  `new[]`, `operator` `delete` and `operator` `delete[]`, are described
49
  completely in  [[basic.stc.dynamic]]. The attributes and restrictions
50
  found in the rest of this subclause do not apply to them unless
51
  explicitly stated in  [[basic.stc.dynamic]].
@@ -85,12 +89,14 @@ of the operator function have been declared, the rules in 
85
  [[over.match.oper]] determine which, if any, interpretation is used.
86
  See  [[over.inc]] for an explanation of the postfix unary operators `++`
87
  and `\dcr`.
88
 
89
  The unary and binary forms of the same operator are considered to have
90
- the same name. Consequently, a unary operator can hide a binary operator
91
- from an enclosing scope, and vice versa.
 
 
92
 
93
  ### Binary operators <a id="over.binary">[[over.binary]]</a>
94
 
95
  A binary operator shall be implemented either by a non-static member
96
  function ([[class.mfct]]) with one parameter or by a non-member
@@ -106,14 +112,20 @@ function with exactly one parameter. Because a copy assignment operator
106
  `operator=` is implicitly declared for a class if not declared by the
107
  user ([[class.copy]]), a base class assignment operator is always
108
  hidden by the copy assignment operator of the derived class.
109
 
110
  Any assignment operator, even the copy and move assignment operators,
111
- can be virtual. For a derived class `D` with a base class `B` for which
112
- a virtual copy/move assignment has been declared, the copy/move
113
- assignment operator in `D` does not override `B`’s virtual copy/move
114
- assignment operator.
 
 
 
 
 
 
115
 
116
  ``` cpp
117
  struct B {
118
  virtual int operator= (int);
119
  virtual B& operator= (const B&);
@@ -129,15 +141,18 @@ B* bptr = &dobj1;
129
  void f() {
130
  bptr->operator=(99); // calls D::operator=(int)
131
  *bptr = 99; // ditto
132
  bptr->operator=(dobj2); // calls D::operator=(const B&)
133
  *bptr = dobj2; // ditto
134
- dobj1 = dobj2; // calls implicitly-declared
135
- // D::operator=(const D&)
136
  }
137
  ```
138
 
 
 
 
 
139
  ### Function call <a id="over.call">[[over.call]]</a>
140
 
141
  `operator()`
142
 
143
  shall be a non-static member function with an arbitrary number of
@@ -162,35 +177,33 @@ mechanism ([[over.match.best]]).
162
 
163
  shall be a non-static member function with exactly one parameter. It
164
  implements the subscripting syntax
165
 
166
  ``` bnf
167
- postfix-expression '[' expression ']'
168
- ```
169
-
170
- or
171
-
172
- ``` bnf
173
- postfix-expression '[' braced-init-list ']'
174
  ```
175
 
176
  Thus, a subscripting expression `x[y]` is interpreted as
177
  `x.operator[](y)` for a class object `x` of type `T` if
178
  `T::operator[](T1)` exists and if the operator is selected as the best
179
  match function by the overload resolution mechanism (
180
  [[over.match.best]]).
181
 
 
 
182
  ``` cpp
183
  struct X {
184
  Z operator[](std::initializer_list<int>);
185
  };
186
  X x;
187
  x[{1,2,3}] = 7; // OK: meaning x.operator[]({1,2,3\)}
188
  int a[10];
189
  a[{1,2,3}] = 7; // error: built-in subscript operator
190
  ```
191
 
 
 
192
  ### Class member access <a id="over.ref">[[over.ref]]</a>
193
 
194
  `operator->`
195
 
196
  shall be a non-static member function taking no parameters. It
@@ -215,11 +228,13 @@ defines the prefix increment operator `++` for objects of that type. If
215
  the function is a non-static member function with one parameter (which
216
  shall be of type `int`) or a non-member function with two parameters
217
  (the second of which shall be of type `int`), it defines the postfix
218
  increment operator `++` for objects of that type. When the postfix
219
  increment is called as a result of using the `++` operator, the `int`
220
- argument will have value zero.[^13]
 
 
221
 
222
  ``` cpp
223
  struct X {
224
  X& operator++(); // prefix ++a
225
  X operator++(int); // postfix a++
@@ -240,10 +255,12 @@ void f(X a, Y b) {
240
  operator++(b); // explicit call: like ++b;
241
  operator++(b, 0); // explicit call: like b++;
242
  }
243
  ```
244
 
 
 
245
  The prefix and postfix decrement operators `-{-}` are handled
246
  analogously.
247
 
248
  ### User-defined literals <a id="over.literal">[[over.literal]]</a>
249
 
@@ -255,13 +272,14 @@ literal-operator-id:
255
 
256
  The *string-literal* or *user-defined-string-literal* in a
257
  *literal-operator-id* shall have no *encoding-prefix* and shall contain
258
  no characters other than the implicit terminating `'\0'`. The
259
  *ud-suffix* of the *user-defined-string-literal* or the *identifier* in
260
- a *literal-operator-id* is called a *literal suffix identifier*. some
261
  literal suffix identifiers are reserved for future standardization; see 
262
- [[usrlit.suffix]].
 
263
 
264
  A declaration whose *declarator-id* is a *literal-operator-id* shall be
265
  a declaration of a namespace-scope function or function template (it
266
  could be a friend function ([[class.friend]])), an explicit
267
  instantiation or specialization of a function template, or a
@@ -298,29 +316,34 @@ have a single *template-parameter* that is a non-type template parameter
298
  pack ([[temp.variadic]]) with element type `char`.
299
 
300
  Literal operators and literal operator templates shall not have C
301
  language linkage.
302
 
303
- Literal operators and literal operator templates are usually invoked
304
- implicitly through user-defined literals ([[lex.ext]]). However, except
305
- for the constraints described above, they are ordinary namespace-scope
306
- functions and function templates. In particular, they are looked up like
307
- ordinary functions and function templates and they follow the same
308
- overload resolution rules. Also, they can be declared `inline` or
309
- `constexpr`, they may have internal or external linkage, they can be
310
- called explicitly, their addresses can be taken, etc.
 
 
 
311
 
312
  ``` cpp
313
  void operator "" _km(long double); // OK
314
  string operator "" _i18n(const char*, std::size_t); // OK
315
  template <char...> double operator "" _\u03C0(); // OK: UCN for lowercase pi
316
  float operator ""_e(const char*); // OK
317
  float operator ""E(const char*); // error: reserved literal suffix~([usrlit.suffix], [lex.ext])
318
- double operator""_Bq(long double); // OK: does not use the reserved name _Bq~([global.names])
319
- double operator"" _Bq(long double); // uses the reserved name _Bq~([global.names])
320
  float operator " " B(const char*); // error: non-empty string-literal
321
  string operator "" 5X(const char*, std::size_t); // error: invalid literal suffix identifier
322
  double operator "" _miles(double); // error: invalid parameter-declaration-clause
323
  template <char...> int operator "" _j(const char*); // error: invalid parameter-declaration-clause
324
  extern "C" void operator "" _m(long double); // error: C language linkage
325
  ```
326
 
 
 
 
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
  + - * &
 
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;
46
  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]].
 
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
 
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 {
130
  virtual int operator= (int);
131
  virtual B& operator= (const B&);
 
141
  void f() {
142
  bptr->operator=(99); // calls D::operator=(int)
143
  *bptr = 99; // ditto
144
  bptr->operator=(dobj2); // calls D::operator=(const B&)
145
  *bptr = dobj2; // ditto
146
+ dobj1 = dobj2; // calls implicitly-declared D::operator=(const D&)
 
147
  }
148
  ```
149
 
150
+ — *end example*]
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
 
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 {
195
  Z operator[](std::initializer_list<int>);
196
  };
197
  X x;
198
  x[{1,2,3}] = 7; // OK: meaning x.operator[]({1,2,3\)}
199
  int a[10];
200
  a[{1,2,3}] = 7; // error: built-in subscript operator
201
  ```
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
 
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 {
239
  X& operator++(); // prefix ++a
240
  X operator++(int); // postfix a++
 
255
  operator++(b); // explicit call: like ++b;
256
  operator++(b, 0); // explicit call: like 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
 
 
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
 
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
+