- tmp/tmpfl1cqdi3/{from.md → to.md} +787 -443
tmp/tmpfl1cqdi3/{from.md → to.md}
RENAMED
|
@@ -12,48 +12,57 @@ function declaration is being referenced is determined by comparing the
|
|
| 12 |
types of the arguments at the point of use with the types of the
|
| 13 |
parameters in the overloaded declarations that are visible at the point
|
| 14 |
of use. This function selection process is called *overload resolution*
|
| 15 |
and is defined in [[over.match]].
|
| 16 |
|
|
|
|
|
|
|
| 17 |
``` cpp
|
| 18 |
double abs(double);
|
| 19 |
int abs(int);
|
| 20 |
|
| 21 |
abs(1); // calls abs(int);
|
| 22 |
abs(1.0); // calls abs(double);
|
| 23 |
```
|
| 24 |
|
|
|
|
|
|
|
| 25 |
## Overloadable declarations <a id="over.load">[[over.load]]</a>
|
| 26 |
|
| 27 |
Not all function declarations can be overloaded. Those that cannot be
|
| 28 |
overloaded are specified here. A program is ill-formed if it contains
|
| 29 |
-
two such non-overloadable declarations in the same scope.
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
Certain function declarations cannot be overloaded:
|
| 37 |
|
| 38 |
-
- Function declarations that differ only in the return type
|
|
|
|
| 39 |
overloaded.
|
| 40 |
- Member function declarations with the same name and the same
|
| 41 |
-
|
| 42 |
-
`static` member function declaration ([[class.static]]).
|
| 43 |
-
member function template declarations with the same name,
|
| 44 |
-
|
| 45 |
-
overloaded if any of them is a `static` member function
|
| 46 |
-
declaration. The types of the implicit object parameters
|
| 47 |
-
for the member functions for the purpose of overload
|
| 48 |
-
[[over.match.funcs]]) are not considered when comparing
|
| 49 |
parameter-type-lists for enforcement of this rule. In contrast, if
|
| 50 |
there is no `static` member function declaration among a set of member
|
| 51 |
function declarations with the same name and the same
|
| 52 |
parameter-type-list, then these member function declarations can be
|
| 53 |
overloaded if they differ in the type of their implicit object
|
| 54 |
-
parameter.
|
|
|
|
|
|
|
| 55 |
``` cpp
|
| 56 |
class X {
|
| 57 |
static void f();
|
| 58 |
void f(); // ill-formed
|
| 59 |
void f() const; // ill-formed
|
|
@@ -61,15 +70,18 @@ Certain function declarations cannot be overloaded:
|
|
| 61 |
void g();
|
| 62 |
void g() const; // OK: no static g
|
| 63 |
void g() const volatile; // OK: no static g
|
| 64 |
};
|
| 65 |
```
|
|
|
|
|
|
|
| 66 |
- Member function declarations with the same name and the same
|
| 67 |
-
|
| 68 |
-
with the same name, the same
|
| 69 |
-
template parameter lists cannot be overloaded if any of them, but
|
| 70 |
-
all, have a *ref-qualifier* ([[dcl.fct]]).
|
|
|
|
| 71 |
``` cpp
|
| 72 |
class Y {
|
| 73 |
void h() &;
|
| 74 |
void h() const &; // OK
|
| 75 |
void h() &&; // OK, all declarations have a ref-qualifier
|
|
@@ -77,39 +89,49 @@ Certain function declarations cannot be overloaded:
|
|
| 77 |
void i() const; // ill-formed, prior declaration of i
|
| 78 |
// has a ref-qualifier
|
| 79 |
};
|
| 80 |
```
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
As specified in [[dcl.fct]], function declarations that have equivalent
|
| 83 |
parameter declarations declare the same function and therefore cannot be
|
| 84 |
overloaded:
|
| 85 |
|
| 86 |
- Parameter declarations that differ only in the use of equivalent
|
| 87 |
typedef “types” are equivalent. A `typedef` is not a separate type,
|
| 88 |
but only a synonym for another type ([[dcl.typedef]]).
|
|
|
|
| 89 |
``` cpp
|
| 90 |
typedef int Int;
|
| 91 |
|
| 92 |
void f(int i);
|
| 93 |
void f(Int i); // OK: redeclaration of f(int)
|
| 94 |
-
void f(int i) {
|
| 95 |
-
void f(Int i) {
|
| 96 |
```
|
| 97 |
|
|
|
|
| 98 |
Enumerations, on the other hand, are distinct types and can be used to
|
| 99 |
distinguish overloaded function declarations.
|
|
|
|
| 100 |
``` cpp
|
| 101 |
enum E { a };
|
| 102 |
|
| 103 |
-
void f(int i) {
|
| 104 |
-
void f(E i) {
|
| 105 |
```
|
|
|
|
|
|
|
| 106 |
- Parameter declarations that differ only in a pointer `*` versus an
|
| 107 |
array `[]` are equivalent. That is, the array declaration is adjusted
|
| 108 |
to become a pointer declaration ([[dcl.fct]]). Only the second and
|
| 109 |
subsequent array dimensions are significant in parameter types (
|
| 110 |
[[dcl.array]]).
|
|
|
|
| 111 |
``` cpp
|
| 112 |
int f(char*);
|
| 113 |
int f(char[]); // same as f(char*);
|
| 114 |
int f(char[7]); // same as f(char*);
|
| 115 |
int f(char[9]); // same as f(char*);
|
|
@@ -117,43 +139,52 @@ overloaded:
|
|
| 117 |
int g(char(*)[10]);
|
| 118 |
int g(char[5][10]); // same as g(char(*)[10]);
|
| 119 |
int g(char[7][10]); // same as g(char(*)[10]);
|
| 120 |
int g(char(*)[20]); // different from g(char(*)[10]);
|
| 121 |
```
|
|
|
|
|
|
|
| 122 |
- Parameter declarations that differ only in that one is a function type
|
| 123 |
and the other is a pointer to the same function type are equivalent.
|
| 124 |
That is, the function type is adjusted to become a pointer to function
|
| 125 |
type ([[dcl.fct]]).
|
|
|
|
| 126 |
``` cpp
|
| 127 |
void h(int());
|
| 128 |
void h(int (*)()); // redeclaration of h(int())
|
| 129 |
void h(int x()) { } // definition of h(int())
|
| 130 |
void h(int (*x)()) { } // ill-formed: redefinition of h(int())
|
| 131 |
```
|
|
|
|
|
|
|
| 132 |
- Parameter declarations that differ only in the presence or absence of
|
| 133 |
`const` and/or `volatile` are equivalent. That is, the `const` and
|
| 134 |
`volatile` type-specifiers for each parameter type are ignored when
|
| 135 |
determining which function is being declared, defined, or called.
|
|
|
|
| 136 |
``` cpp
|
| 137 |
typedef const int cInt;
|
| 138 |
|
| 139 |
int f (int);
|
| 140 |
int f (const int); // redeclaration of f(int)
|
| 141 |
-
int f (int) {
|
| 142 |
-
int f (cInt) {
|
| 143 |
```
|
| 144 |
|
|
|
|
| 145 |
Only the `const` and `volatile` type-specifiers at the outermost level
|
| 146 |
of the parameter type specification are ignored in this fashion;
|
| 147 |
`const` and `volatile` type-specifiers buried within a parameter type
|
| 148 |
specification are significant and can be used to distinguish
|
| 149 |
overloaded function declarations.[^1] In particular, for any type `T`,
|
| 150 |
-
“pointer to `T`
|
| 151 |
`T`” are considered distinct parameter types, as are “reference to
|
| 152 |
-
`T`
|
| 153 |
- Two parameter declarations that differ only in their default arguments
|
| 154 |
-
are equivalent.
|
|
|
|
|
|
|
| 155 |
``` cpp
|
| 156 |
void f (int i, int j);
|
| 157 |
void f (int i, int j = 99); // OK: redeclaration of f(int, int)
|
| 158 |
void f (int i = 88, int j); // OK: redeclaration of f(int, int)
|
| 159 |
void f (); // OK: overloaded declaration of f
|
|
@@ -163,17 +194,23 @@ overloaded:
|
|
| 163 |
f (1); // OK: call f(int, int)
|
| 164 |
f (); // Error: f(int, int) or f()?
|
| 165 |
}
|
| 166 |
```
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
## Declaration matching <a id="over.dcl">[[over.dcl]]</a>
|
| 169 |
|
| 170 |
Two function declarations of the same name refer to the same function if
|
| 171 |
they are in the same scope and have equivalent parameter declarations (
|
| 172 |
[[over.load]]). A function member of a derived class is *not* in the
|
| 173 |
same scope as a function member of the same name in a base class.
|
| 174 |
|
|
|
|
|
|
|
| 175 |
``` cpp
|
| 176 |
struct B {
|
| 177 |
int f(int);
|
| 178 |
};
|
| 179 |
|
|
@@ -191,13 +228,17 @@ void h(D* pd) {
|
|
| 191 |
pd->B::f(1); // OK
|
| 192 |
pd->f("Ben"); // OK, calls D::f
|
| 193 |
}
|
| 194 |
```
|
| 195 |
|
|
|
|
|
|
|
| 196 |
A locally declared function is not in the same scope as a function in a
|
| 197 |
containing scope.
|
| 198 |
|
|
|
|
|
|
|
| 199 |
``` cpp
|
| 200 |
void f(const char*);
|
| 201 |
void g() {
|
| 202 |
extern void f(int);
|
| 203 |
f("asdf"); // error: f(int) hides f(const char*)
|
|
@@ -211,13 +252,17 @@ void caller () {
|
|
| 211 |
callee(88, 99); // error: only callee(int) in scope
|
| 212 |
}
|
| 213 |
}
|
| 214 |
```
|
| 215 |
|
|
|
|
|
|
|
| 216 |
Different versions of an overloaded member function can be given
|
| 217 |
different access rules.
|
| 218 |
|
|
|
|
|
|
|
| 219 |
``` cpp
|
| 220 |
class buffer {
|
| 221 |
private:
|
| 222 |
char* p;
|
| 223 |
int size;
|
|
@@ -226,23 +271,27 @@ protected:
|
|
| 226 |
public:
|
| 227 |
buffer(int s) { p = new char[size = s]; }
|
| 228 |
};
|
| 229 |
```
|
| 230 |
|
|
|
|
|
|
|
| 231 |
## Overload resolution <a id="over.match">[[over.match]]</a>
|
| 232 |
|
| 233 |
Overload resolution is a mechanism for selecting the best function to
|
| 234 |
call given a list of expressions that are to be the arguments of the
|
| 235 |
call and a set of *candidate functions* that can be called based on the
|
| 236 |
context of the call. The selection criteria for the best function are
|
| 237 |
the number of arguments, how well the arguments match the
|
| 238 |
parameter-type-list of the candidate function, how well (for non-static
|
| 239 |
member functions) the object matches the implicit object parameter, and
|
| 240 |
-
certain other properties of the candidate function.
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
| 244 |
|
| 245 |
Overload resolution selects the function to call in seven distinct
|
| 246 |
contexts within the language:
|
| 247 |
|
| 248 |
- invocation of a function named in the function call syntax (
|
|
@@ -251,16 +300,16 @@ contexts within the language:
|
|
| 251 |
conversion function, a reference-to-pointer-to-function conversion
|
| 252 |
function, or a reference-to-function conversion function on a class
|
| 253 |
object named in the function call syntax ([[over.call.object]]);
|
| 254 |
- invocation of the operator referenced in an expression (
|
| 255 |
[[over.match.oper]]);
|
| 256 |
-
- invocation of a constructor for direct-initialization (
|
| 257 |
-
of a class object ([[over.match.ctor]]);
|
| 258 |
- invocation of a user-defined conversion for copy-initialization (
|
| 259 |
[[dcl.init]]) of a class object ([[over.match.copy]]);
|
| 260 |
- invocation of a conversion function for initialization of an object of
|
| 261 |
-
a
|
| 262 |
[[over.match.conv]]); and
|
| 263 |
- invocation of a conversion function for conversion to a glvalue or
|
| 264 |
class prvalue to which a reference ([[dcl.init.ref]]) will be
|
| 265 |
directly bound ([[over.match.ref]]).
|
| 266 |
|
|
@@ -310,55 +359,56 @@ object parameter, if present, is always the first parameter and the
|
|
| 310 |
implied object argument, if present, is always the first argument.
|
| 311 |
|
| 312 |
For non-static member functions, the type of the implicit object
|
| 313 |
parameter is
|
| 314 |
|
| 315 |
-
- “lvalue reference to
|
| 316 |
*ref-qualifier* or with the `&` *ref-qualifier*
|
| 317 |
-
- “rvalue reference to
|
| 318 |
*ref-qualifier*
|
| 319 |
|
| 320 |
-
where `X` is the class of which the function is a member and
|
| 321 |
-
cv-qualification on the member function declaration.
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
class
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
During overload resolution, the implied object argument is
|
| 337 |
indistinguishable from other arguments. The implicit object parameter,
|
| 338 |
-
however, retains its identity since
|
| 339 |
-
|
|
|
|
| 340 |
|
| 341 |
-
-
|
| 342 |
-
implicit object parameter; and
|
| 343 |
-
- no user-defined conversions can be applied to achieve a type match
|
| 344 |
-
with it.
|
| 345 |
-
|
| 346 |
-
For non-static member functions declared without a *ref-qualifier*, an
|
| 347 |
-
additional rule applies:
|
| 348 |
-
|
| 349 |
-
- even if the implicit object parameter is not `const`-qualified, an
|
| 350 |
rvalue can be bound to the parameter as long as in all other respects
|
| 351 |
the argument can be converted to the type of the implicit object
|
| 352 |
-
parameter. The fact that such an argument is an rvalue
|
| 353 |
-
the ranking of implicit conversion sequences (
|
|
|
|
| 354 |
|
| 355 |
Because other than in list-initialization only one user-defined
|
| 356 |
conversion is allowed in an implicit conversion sequence, special rules
|
| 357 |
apply when selecting the best user-defined conversion (
|
| 358 |
[[over.match.best]], [[over.best.ics]]).
|
| 359 |
|
|
|
|
|
|
|
| 360 |
``` cpp
|
| 361 |
class T {
|
| 362 |
public:
|
| 363 |
T();
|
| 364 |
};
|
|
@@ -368,10 +418,12 @@ public:
|
|
| 368 |
C(int);
|
| 369 |
};
|
| 370 |
T a = 1; // ill-formed: T(C(1)) not tried
|
| 371 |
```
|
| 372 |
|
|
|
|
|
|
|
| 373 |
In each case where a candidate is a function template, candidate
|
| 374 |
function template specializations are generated using template argument
|
| 375 |
deduction ([[temp.over]], [[temp.deduct]]). Those candidates are then
|
| 376 |
handled as candidate functions in the usual way.[^2] A given name can
|
| 377 |
refer to one or more function templates and also to a set of overloaded
|
|
@@ -399,12 +451,13 @@ class type, overload resolution is applied as specified in
|
|
| 399 |
|
| 400 |
If the *postfix-expression* denotes the address of a set of overloaded
|
| 401 |
functions and/or function templates, overload resolution is applied
|
| 402 |
using that set as described above. If the function selected by overload
|
| 403 |
resolution is a non-static member function, the program is ill-formed.
|
| 404 |
-
|
| 405 |
-
|
|
|
|
| 406 |
|
| 407 |
##### Call to named function <a id="over.call.func">[[over.call.func]]</a>
|
| 408 |
|
| 409 |
Of interest in [[over.call.func]] are only those function calls in
|
| 410 |
which the *postfix-expression* ultimately contains a name that denotes
|
|
@@ -427,12 +480,12 @@ In qualified function calls, the name to be resolved is an
|
|
| 427 |
construct `A->B` is generally equivalent to `(*A).B`, the rest of
|
| 428 |
Clause [[over]] assumes, without loss of generality, that all member
|
| 429 |
function calls have been normalized to the form that uses an object and
|
| 430 |
the `.` operator. Furthermore, Clause [[over]] assumes that the
|
| 431 |
*postfix-expression* that is the left operand of the `.` operator has
|
| 432 |
-
type “
|
| 433 |
-
|
| 434 |
following the rules for looking up names in classes (
|
| 435 |
[[class.member.lookup]]). The function declarations found by that lookup
|
| 436 |
constitute the set of candidate functions. The argument list is the
|
| 437 |
*expression-list* in the call augmented by the addition of the left
|
| 438 |
operand of the `.` operator in the normalized member function call as
|
|
@@ -458,32 +511,33 @@ and overload resolution selects one of the non-static member functions
|
|
| 458 |
of `T`, the call is ill-formed.
|
| 459 |
|
| 460 |
##### Call to object of class type <a id="over.call.object">[[over.call.object]]</a>
|
| 461 |
|
| 462 |
If the *primary-expression* `E` in the function call syntax evaluates to
|
| 463 |
-
a class object of type “
|
| 464 |
includes at least the function call operators of `T`. The function call
|
| 465 |
operators of `T` are obtained by ordinary lookup of the name
|
| 466 |
`operator()` in the context of `(E).operator()`.
|
| 467 |
|
| 468 |
In addition, for each non-explicit conversion function declared in `T`
|
| 469 |
of the form
|
| 470 |
|
| 471 |
``` bnf
|
| 472 |
-
'operator' conversion-type-id '( )' cv-qualifier ref-qualifierₒₚₜ
|
| 473 |
```
|
| 474 |
|
| 475 |
where *cv-qualifier* is the same cv-qualification as, or a greater
|
| 476 |
-
cv-qualification than,
|
| 477 |
-
type “pointer to function of (`
|
| 478 |
-
“reference to pointer to function of (`
|
| 479 |
-
the type “reference to function of (`
|
| 480 |
*surrogate call function* with the unique name *call-function* and
|
| 481 |
having the form
|
| 482 |
|
| 483 |
``` bnf
|
| 484 |
-
'R' call-function '(' conversion-type-id
|
|
|
|
| 485 |
```
|
| 486 |
|
| 487 |
is also considered as a candidate function. Similarly, surrogate call
|
| 488 |
functions are added to the set of candidate functions for each
|
| 489 |
non-explicit conversion function declared in a base class of `T`
|
|
@@ -497,82 +551,92 @@ then be invoked with the arguments of the call. If the conversion
|
|
| 497 |
function cannot be called (e.g., because of an ambiguity), the program
|
| 498 |
is ill-formed.
|
| 499 |
|
| 500 |
The argument list submitted to overload resolution consists of the
|
| 501 |
argument expressions present in the function call syntax preceded by the
|
| 502 |
-
implied object argument `(E)`.
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 511 |
|
| 512 |
``` cpp
|
| 513 |
int f1(int);
|
| 514 |
int f2(float);
|
| 515 |
typedef int (*fp1)(int);
|
| 516 |
typedef int (*fp2)(float);
|
| 517 |
struct A {
|
| 518 |
operator fp1() { return f1; }
|
| 519 |
operator fp2() { return f2; }
|
| 520 |
} a;
|
| 521 |
-
int i = a(1);
|
| 522 |
-
// conversion function
|
| 523 |
```
|
| 524 |
|
|
|
|
|
|
|
| 525 |
#### Operators in expressions <a id="over.match.oper">[[over.match.oper]]</a>
|
| 526 |
|
| 527 |
If no operand of an operator in an expression has a type that is a class
|
| 528 |
or an enumeration, the operator is assumed to be a built-in operator and
|
| 529 |
-
interpreted according to Clause [[expr]].
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
|
| 536 |
``` cpp
|
| 537 |
struct String {
|
| 538 |
String (const String&);
|
| 539 |
String (const char*);
|
| 540 |
operator const char* ();
|
| 541 |
};
|
| 542 |
String operator + (const String&, const String&);
|
| 543 |
|
| 544 |
-
void f(
|
| 545 |
-
const char* p= "one" + "two"; // ill-formed because neither
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
// class or enumeration types exist that
|
| 549 |
-
// would perform the operation.
|
| 550 |
}
|
| 551 |
```
|
| 552 |
|
|
|
|
|
|
|
| 553 |
If either operand has a type that is a class or an enumeration, a
|
| 554 |
user-defined operator function might be declared that implements this
|
| 555 |
operator or a user-defined conversion can be necessary to convert the
|
| 556 |
operand to a type that is appropriate for a built-in operator. In this
|
| 557 |
case, overload resolution is used to determine which operator function
|
| 558 |
or built-in operator is to be invoked to implement the operator.
|
| 559 |
Therefore, the operator notation is first transformed to the equivalent
|
| 560 |
function-call notation as summarized in Table [[tab:over.rel.op.func]]
|
| 561 |
(where `@` denotes one of the operators covered in the specified
|
| 562 |
-
subclause).
|
|
|
|
| 563 |
|
| 564 |
**Table: Relationship between operator and function call notation** <a id="tab:over.rel.op.func">[tab:over.rel.op.func]</a>
|
| 565 |
|
| 566 |
| Subclause | Expression | As member function | As non-member function |
|
| 567 |
-
| ------------ | ---------- | ------------------ | ---------------------- |
|
| 568 |
-
| (a)
|
| 569 |
-
| (a, b)
|
| 570 |
-
| [[over.ass]] | a=b
|
| 571 |
-
| [[over.sub]] | a[b]
|
| 572 |
-
| [[over.ref]] | a->
|
| 573 |
-
| (a, 0)
|
| 574 |
|
| 575 |
|
| 576 |
For a unary operator `@` with an operand of a type whose cv-unqualified
|
| 577 |
version is `T1`, and for a binary operator `@` with a left operand of a
|
| 578 |
type whose cv-unqualified version is `T1` and a right operand of a type
|
|
@@ -588,14 +652,14 @@ candidates*, are constructed as follows:
|
|
| 588 |
lookup of `operator@` in the context of the expression according to
|
| 589 |
the usual rules for name lookup in unqualified function calls (
|
| 590 |
[[basic.lookup.argdep]]) except that all member functions are ignored.
|
| 591 |
However, if no operand has a class type, only those non-member
|
| 592 |
functions in the lookup set that have a first parameter of type `T1`
|
| 593 |
-
or “reference to
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
- For the operator `,`, the unary operator `&`, or the operator `->`,
|
| 598 |
the built-in candidates set is empty. For all other operators, the
|
| 599 |
built-in candidates include all of the candidate operator functions
|
| 600 |
defined in [[over.built]] that, compared to the given operator,
|
| 601 |
- have the same operator name, and
|
|
@@ -619,10 +683,12 @@ the member candidates, the non-member candidates, and the built-in
|
|
| 619 |
candidates. The argument list contains all of the operands of the
|
| 620 |
operator. The best function from the set of candidate functions is
|
| 621 |
selected according to [[over.match.viable]] and
|
| 622 |
[[over.match.best]].[^6]
|
| 623 |
|
|
|
|
|
|
|
| 624 |
``` cpp
|
| 625 |
struct A {
|
| 626 |
operator int();
|
| 627 |
};
|
| 628 |
A operator+(const A&, const A&);
|
|
@@ -630,18 +696,22 @@ void m() {
|
|
| 630 |
A a, b;
|
| 631 |
a + b; // operator+(a, b) chosen over int(a) + int(b)
|
| 632 |
}
|
| 633 |
```
|
| 634 |
|
|
|
|
|
|
|
| 635 |
If a built-in candidate is selected by overload resolution, the operands
|
| 636 |
of class type are converted to the types of the corresponding parameters
|
| 637 |
of the selected operation function, except that the second standard
|
| 638 |
conversion sequence of a user-defined conversion sequence (
|
| 639 |
[[over.ics.user]]) is not applied. Then the operator is treated as the
|
| 640 |
corresponding built-in operator and interpreted according to Clause
|
| 641 |
[[expr]].
|
| 642 |
|
|
|
|
|
|
|
| 643 |
``` cpp
|
| 644 |
struct X {
|
| 645 |
operator double();
|
| 646 |
};
|
| 647 |
|
|
@@ -651,20 +721,24 @@ struct Y {
|
|
| 651 |
|
| 652 |
int *a = Y() + 100.0; // error: pointer arithmetic requires integral operand
|
| 653 |
int *b = Y() + X(); // error: pointer arithmetic requires integral operand
|
| 654 |
```
|
| 655 |
|
|
|
|
|
|
|
| 656 |
The second operand of operator `->` is ignored in selecting an
|
| 657 |
`operator->` function, and is not an argument when the `operator->`
|
| 658 |
function is called. When `operator->` returns, the operator `->` is
|
| 659 |
applied to the value returned, with the original second operand.[^7]
|
| 660 |
|
| 661 |
If the operator is the operator `,`, the unary operator `&`, or the
|
| 662 |
operator `->`, and there are no viable functions, then the operator is
|
| 663 |
assumed to be the built-in operator and interpreted according to Clause
|
| 664 |
[[expr]].
|
| 665 |
|
|
|
|
|
|
|
| 666 |
The lookup rules for operators in expressions are different than the
|
| 667 |
lookup rules for operator function names in a function call, as shown in
|
| 668 |
the following example:
|
| 669 |
|
| 670 |
``` cpp
|
|
@@ -682,64 +756,72 @@ void B::f() {
|
|
| 682 |
operator+ (a,a); // error: global operator hidden by member
|
| 683 |
a + a; // OK: calls global operator+
|
| 684 |
}
|
| 685 |
```
|
| 686 |
|
|
|
|
|
|
|
| 687 |
#### Initialization by constructor <a id="over.match.ctor">[[over.match.ctor]]</a>
|
| 688 |
|
| 689 |
-
When objects of class type are direct-initialized ([[dcl.init]]),
|
| 690 |
copy-initialized from an expression of the same or a derived class
|
| 691 |
-
type ([[dcl.init]]),
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
the candidate functions are all the
|
|
|
|
|
|
|
| 695 |
[[class.conv.ctor]]) of that class. The argument list is the
|
| 696 |
*expression-list* or *assignment-expression* of the *initializer*.
|
| 697 |
|
| 698 |
#### Copy-initialization of class by user-defined conversion <a id="over.match.copy">[[over.match.copy]]</a>
|
| 699 |
|
| 700 |
Under the conditions specified in [[dcl.init]], as part of a
|
| 701 |
copy-initialization of an object of class type, a user-defined
|
| 702 |
conversion can be invoked to convert an initializer expression to the
|
| 703 |
type of the object being initialized. Overload resolution is used to
|
| 704 |
-
select the user-defined conversion to be invoked.
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
|
|
|
|
|
|
| 710 |
|
| 711 |
- The converting constructors ([[class.conv.ctor]]) of `T` are
|
| 712 |
candidate functions.
|
| 713 |
-
- When the type of the initializer expression is a class type “
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
possibly cv-qualified `T`
|
| 718 |
-
argument in the context of direct-initialization of an object
|
| 719 |
-
“*cv2* `T`”, explicit conversion functions are also
|
| 720 |
-
that are not hidden within `S` and yield a type
|
| 721 |
-
version is the same type as `T` or is a derived
|
| 722 |
-
candidate functions. Conversion functions that
|
| 723 |
-
`X`” return lvalues or xvalues, depending on the
|
| 724 |
-
type `X` and are therefore considered to yield
|
| 725 |
-
selecting candidate functions.
|
| 726 |
|
| 727 |
In both cases, the argument list has one argument, which is the
|
| 728 |
-
initializer expression.
|
| 729 |
-
|
| 730 |
-
|
|
|
|
|
|
|
| 731 |
|
| 732 |
#### Initialization by conversion function <a id="over.match.conv">[[over.match.conv]]</a>
|
| 733 |
|
| 734 |
Under the conditions specified in [[dcl.init]], as part of an
|
| 735 |
-
initialization of an object of
|
| 736 |
be invoked to convert an initializer expression of class type to the
|
| 737 |
type of the object being initialized. Overload resolution is used to
|
| 738 |
select the conversion function to be invoked. Assuming that “*cv1* `T`”
|
| 739 |
-
is the type of the object being initialized, and “
|
| 740 |
-
|
| 741 |
functions are selected as follows:
|
| 742 |
|
| 743 |
- The conversion functions of `S` and its base classes are considered.
|
| 744 |
Those non-explicit conversion functions that are not hidden within `S`
|
| 745 |
and yield type `T` or a type that can be converted to type `T` via a
|
|
@@ -754,46 +836,50 @@ functions are selected as follows:
|
|
| 754 |
return lvalues or xvalues, depending on the type of reference, of type
|
| 755 |
“*cv2* `X`” and are therefore considered to yield `X` for this process
|
| 756 |
of selecting candidate functions.
|
| 757 |
|
| 758 |
The argument list has one argument, which is the initializer expression.
|
| 759 |
-
|
| 760 |
-
the
|
|
|
|
| 761 |
|
| 762 |
#### Initialization by conversion function for direct reference binding <a id="over.match.ref">[[over.match.ref]]</a>
|
| 763 |
|
| 764 |
Under the conditions specified in [[dcl.init.ref]], a reference can be
|
| 765 |
bound directly to a glvalue or class prvalue that is the result of
|
| 766 |
applying a conversion function to an initializer expression. Overload
|
| 767 |
resolution is used to select the conversion function to be invoked.
|
| 768 |
-
Assuming that “*cv1* `T`” is the
|
| 769 |
-
initialized, and “
|
| 770 |
-
with `S` a class type, the candidate functions are selected
|
|
|
|
| 771 |
|
| 772 |
- The conversion functions of `S` and its base classes are considered.
|
| 773 |
Those non-explicit conversion functions that are not hidden within `S`
|
| 774 |
and yield type “lvalue reference to *cv2* `T2`” (when initializing an
|
| 775 |
-
lvalue reference or an rvalue reference to function) or “ `T2`”
|
| 776 |
-
“rvalue reference to `T2`” (when initializing an rvalue
|
| 777 |
-
an lvalue reference to function), where “*cv1* `T`” is
|
| 778 |
reference-compatible ([[dcl.init.ref]]) with “*cv2* `T2`”, are
|
| 779 |
candidate functions. For direct-initialization, those explicit
|
| 780 |
conversion functions that are not hidden within `S` and yield type
|
| 781 |
“lvalue reference to *cv2* `T2`” or “*cv2* `T2`” or “rvalue reference
|
| 782 |
-
to *cv2* `T2`
|
| 783 |
can be converted to type `T` with a qualification conversion (
|
| 784 |
[[conv.qual]]), are also candidate functions.
|
| 785 |
|
| 786 |
The argument list has one argument, which is the initializer expression.
|
| 787 |
-
|
| 788 |
-
the
|
|
|
|
| 789 |
|
| 790 |
#### Initialization by list-initialization <a id="over.match.list">[[over.match.list]]</a>
|
| 791 |
|
| 792 |
-
When objects of non-aggregate class type `T` are list-initialized
|
| 793 |
-
[[dcl.init.list]]
|
| 794 |
-
|
|
|
|
| 795 |
|
| 796 |
- Initially, the candidate functions are the initializer-list
|
| 797 |
constructors ([[dcl.init.list]]) of the class `T` and the argument
|
| 798 |
list consists of the initializer list as a single argument.
|
| 799 |
- If no viable initializer-list constructor is found, overload
|
|
@@ -802,14 +888,89 @@ phases:
|
|
| 802 |
the elements of the initializer list.
|
| 803 |
|
| 804 |
If the initializer list has no elements and `T` has a default
|
| 805 |
constructor, the first phase is omitted. In copy-list-initialization, if
|
| 806 |
an `explicit` constructor is chosen, the initialization is ill-formed.
|
| 807 |
-
|
|
|
|
| 808 |
[[over.match.copy]]), where only converting constructors are considered
|
| 809 |
for copy-initialization. This restriction only applies if this
|
| 810 |
-
initialization is part of the final result of overload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 811 |
|
| 812 |
### Viable functions <a id="over.match.viable">[[over.match.viable]]</a>
|
| 813 |
|
| 814 |
From the set of candidate functions constructed for a given context (
|
| 815 |
[[over.match.funcs]]), a set of viable functions is chosen, from which
|
|
@@ -845,14 +1006,14 @@ function (see [[over.ics.ref]]).
|
|
| 845 |
|
| 846 |
### Best viable function <a id="over.match.best">[[over.match.best]]</a>
|
| 847 |
|
| 848 |
Define ICS*i*(`F`) as follows:
|
| 849 |
|
| 850 |
-
-
|
| 851 |
ICS*1*(`F`) is neither better nor worse than ICS*1*(`G`) for any
|
| 852 |
function `G`, and, symmetrically, ICS*1*(`G`) is neither better nor
|
| 853 |
-
worse than ICS*1*(`F`)[^9]
|
| 854 |
- let ICS*i*(`F`) denote the implicit conversion sequence that converts
|
| 855 |
the *i*-th argument in the list to the type of the *i*-th parameter of
|
| 856 |
viable function `F`. [[over.best.ics]] defines the implicit conversion
|
| 857 |
sequences and [[over.ics.rank]] defines what it means for one implicit
|
| 858 |
conversion sequence to be a better conversion sequence or worse
|
|
@@ -868,30 +1029,32 @@ and then
|
|
| 868 |
- the context is an initialization by user-defined conversion (see
|
| 869 |
[[dcl.init]], [[over.match.conv]], and [[over.match.ref]]) and the
|
| 870 |
standard conversion sequence from the return type of `F1` to the
|
| 871 |
destination type (i.e., the type of the entity being initialized) is a
|
| 872 |
better conversion sequence than the standard conversion sequence from
|
| 873 |
-
the return type of `F2` to the destination type
|
|
|
|
| 874 |
``` cpp
|
| 875 |
struct A {
|
| 876 |
A();
|
| 877 |
operator int();
|
| 878 |
operator double();
|
| 879 |
} a;
|
| 880 |
-
int i = a;
|
| 881 |
-
|
| 882 |
-
// a conversion to int
|
| 883 |
float x = a; // ambiguous: both possibilities require conversions,
|
| 884 |
// and neither is better than the other
|
| 885 |
```
|
| 886 |
|
|
|
|
| 887 |
or, if not that,
|
| 888 |
- the context is an initialization by conversion function for direct
|
| 889 |
reference binding ([[over.match.ref]]) of a reference to function
|
| 890 |
type, the return type of `F1` is the same kind of reference (i.e.
|
| 891 |
lvalue or rvalue) as the reference being initialized, and the return
|
| 892 |
type of `F2` is not
|
|
|
|
| 893 |
``` cpp
|
| 894 |
template <class T> struct A {
|
| 895 |
operator T&(); // #1
|
| 896 |
operator T&&(); // #2
|
| 897 |
};
|
|
@@ -899,50 +1062,87 @@ and then
|
|
| 899 |
A<Fn> a;
|
| 900 |
Fn& lf = a; // calls #1
|
| 901 |
Fn&& rf = a; // calls #2
|
| 902 |
```
|
| 903 |
|
|
|
|
| 904 |
or, if not that,
|
| 905 |
- `F1` is not a function template specialization and `F2` is a function
|
| 906 |
template specialization, or, if not that,
|
| 907 |
- `F1` and `F2` are function template specializations, and the function
|
| 908 |
template for `F1` is more specialized than the template for `F2`
|
| 909 |
according to the partial ordering rules described in
|
| 910 |
-
[[temp.func.order]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 911 |
|
| 912 |
If there is exactly one viable function that is a better function than
|
| 913 |
all other viable functions, then it is the one selected by overload
|
| 914 |
-
resolution; otherwise the call is ill-formed[^10]
|
|
|
|
|
|
|
| 915 |
|
| 916 |
``` cpp
|
| 917 |
void Fcn(const int*, short);
|
| 918 |
void Fcn(int*, int);
|
| 919 |
|
| 920 |
int i;
|
| 921 |
short s = 0;
|
| 922 |
|
| 923 |
void f() {
|
| 924 |
-
Fcn(&i, s);
|
| 925 |
-
// &i → int* is better than &i → const int*
|
| 926 |
// but s → short is also better than s → int
|
| 927 |
|
| 928 |
-
Fcn(&i, 1L);
|
| 929 |
-
// &i → int* is better than &i → const int*
|
| 930 |
// and 1L → short and 1L → int are indistinguishable
|
| 931 |
|
| 932 |
-
Fcn(&i,'c');
|
| 933 |
-
// &i → int* is better than &i → const int*
|
| 934 |
// and c → int is better than c → short
|
| 935 |
}
|
| 936 |
```
|
| 937 |
|
|
|
|
|
|
|
| 938 |
If the best viable function resolves to a function for which multiple
|
| 939 |
declarations were found, and if at least two of these declarations — or
|
| 940 |
the declarations they refer to in the case of *using-declaration*s —
|
| 941 |
specify a default argument that made the function viable, the program is
|
| 942 |
ill-formed.
|
| 943 |
|
|
|
|
|
|
|
| 944 |
``` cpp
|
| 945 |
namespace A {
|
| 946 |
extern "C" void f(int = 5);
|
| 947 |
}
|
| 948 |
namespace B {
|
|
@@ -956,10 +1156,12 @@ void use() {
|
|
| 956 |
f(3); // OK, default argument was not used for viability
|
| 957 |
f(); // Error: found default argument twice
|
| 958 |
}
|
| 959 |
```
|
| 960 |
|
|
|
|
|
|
|
| 961 |
#### Implicit conversion sequences <a id="over.best.ics">[[over.best.ics]]</a>
|
| 962 |
|
| 963 |
An *implicit conversion sequence* is a sequence of conversions used to
|
| 964 |
convert an argument in a function call to the type of the corresponding
|
| 965 |
parameter of the function being called. The sequence of conversions is
|
|
@@ -968,16 +1170,16 @@ governed by the rules for initialization of an object or reference by a
|
|
| 968 |
single expression ([[dcl.init]], [[dcl.init.ref]]).
|
| 969 |
|
| 970 |
Implicit conversion sequences are concerned only with the type,
|
| 971 |
cv-qualification, and value category of the argument and how these are
|
| 972 |
converted to match the corresponding properties of the parameter. Other
|
| 973 |
-
properties, such as the lifetime, storage class, alignment,
|
| 974 |
-
accessibility of the argument
|
| 975 |
-
|
| 976 |
-
|
| 977 |
-
argument
|
| 978 |
-
analysis.
|
| 979 |
|
| 980 |
A well-formed implicit conversion sequence is one of the following
|
| 981 |
forms:
|
| 982 |
|
| 983 |
- a *standard conversion sequence* ([[over.ics.scs]]),
|
|
@@ -995,17 +1197,21 @@ by
|
|
| 995 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
| 996 |
step of a class copy-initialization,
|
| 997 |
- [[over.match.copy]], [[over.match.conv]], or [[over.match.ref]] (in
|
| 998 |
all cases), or
|
| 999 |
- the second phase of [[over.match.list]] when the initializer list has
|
| 1000 |
-
exactly one element
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
|
| 1004 |
-
user-defined conversion sequences are not considered.
|
| 1005 |
-
|
| 1006 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1007 |
|
| 1008 |
``` cpp
|
| 1009 |
struct Y { Y(int); };
|
| 1010 |
struct A { operator int(); };
|
| 1011 |
Y y1 = A(); // error: A::operator int() is not a candidate
|
|
@@ -1014,83 +1220,120 @@ struct Y { Y(int); };
|
|
| 1014 |
struct B { operator X(); };
|
| 1015 |
B b;
|
| 1016 |
X x({b}); // error: B::operator X() is not a candidate
|
| 1017 |
```
|
| 1018 |
|
|
|
|
|
|
|
| 1019 |
For the case where the parameter type is a reference, see
|
| 1020 |
[[over.ics.ref]].
|
| 1021 |
|
| 1022 |
When the parameter type is not a reference, the implicit conversion
|
| 1023 |
sequence models a copy-initialization of the parameter from the argument
|
| 1024 |
expression. The implicit conversion sequence is the one required to
|
| 1025 |
convert the argument expression to a prvalue of the type of the
|
| 1026 |
-
parameter.
|
|
|
|
|
|
|
| 1027 |
conversion defined for the purposes of Clause [[over]]; the actual
|
| 1028 |
initialization is defined in terms of constructors and is not a
|
| 1029 |
-
conversion.
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
|
| 1033 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1034 |
When the parameter has a class type and the argument expression has the
|
| 1035 |
same type, the implicit conversion sequence is an identity conversion.
|
| 1036 |
When the parameter has a class type and the argument expression has a
|
| 1037 |
derived class type, the implicit conversion sequence is a
|
| 1038 |
derived-to-base Conversion from the derived class to the base class.
|
| 1039 |
-
|
| 1040 |
-
|
| 1041 |
-
|
|
|
|
|
|
|
|
|
|
| 1042 |
|
| 1043 |
In all contexts, when converting to the implicit object parameter or
|
| 1044 |
when converting to the left operand of an assignment operation only
|
| 1045 |
-
standard conversion sequences
|
| 1046 |
-
result are allowed.
|
| 1047 |
|
| 1048 |
If no conversions are required to match an argument to a parameter type,
|
| 1049 |
the implicit conversion sequence is the standard conversion sequence
|
| 1050 |
consisting of the identity conversion ([[over.ics.scs]]).
|
| 1051 |
|
| 1052 |
If no sequence of conversions can be found to convert an argument to a
|
| 1053 |
-
parameter type
|
| 1054 |
-
conversion sequence cannot be formed.
|
| 1055 |
|
| 1056 |
If several different sequences of conversions exist that each convert
|
| 1057 |
the argument to the parameter type, the implicit conversion sequence
|
| 1058 |
associated with the parameter is defined to be the unique conversion
|
| 1059 |
sequence designated the *ambiguous conversion sequence*. For the purpose
|
| 1060 |
of ranking implicit conversion sequences as described in
|
| 1061 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 1062 |
-
user-defined sequence that is indistinguishable from any
|
| 1063 |
-
user-defined conversion sequence
|
| 1064 |
-
|
| 1065 |
-
|
| 1066 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1067 |
|
| 1068 |
The three forms of implicit conversion sequences mentioned above are
|
| 1069 |
defined in the following subclauses.
|
| 1070 |
|
| 1071 |
##### Standard conversion sequences <a id="over.ics.scs">[[over.ics.scs]]</a>
|
| 1072 |
|
| 1073 |
Table [[tab:over.conversions]] summarizes the conversions defined in
|
| 1074 |
Clause [[conv]] and partitions them into four disjoint categories:
|
| 1075 |
Lvalue Transformation, Qualification Adjustment, Promotion, and
|
| 1076 |
-
Conversion.
|
|
|
|
|
|
|
| 1077 |
category, cv-qualification, and data representation: the Lvalue
|
| 1078 |
Transformations do not change the cv-qualification or data
|
| 1079 |
representation of the type; the Qualification Adjustments do not change
|
| 1080 |
the value category or data representation of the type; and the
|
| 1081 |
Promotions and Conversions do not change the value category or
|
| 1082 |
-
cv-qualification of the type.
|
| 1083 |
|
| 1084 |
-
As described in Clause [[conv]], a standard conversion
|
| 1085 |
-
either the Identity conversion by itself (that is, no
|
| 1086 |
-
consists of one to three conversions from the other four
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
sequence, the conversions are applied in the canonical order: **Lvalue
|
| 1090 |
Transformation**, **Promotion** or **Conversion**, **Qualification
|
| 1091 |
-
Adjustment**.
|
| 1092 |
|
| 1093 |
Each conversion in Table [[tab:over.conversions]] also has an
|
| 1094 |
associated rank (Exact Match, Promotion, or Conversion). These are used
|
| 1095 |
to rank standard conversion sequences ([[over.ics.rank]]). The rank of
|
| 1096 |
a conversion sequence is determined by considering the rank of each
|
|
@@ -1151,70 +1394,88 @@ When a parameter of reference type binds directly ([[dcl.init.ref]]) to
|
|
| 1151 |
an argument expression, the implicit conversion sequence is the identity
|
| 1152 |
conversion, unless the argument expression has a type that is a derived
|
| 1153 |
class of the parameter type, in which case the implicit conversion
|
| 1154 |
sequence is a derived-to-base Conversion ([[over.best.ics]]).
|
| 1155 |
|
|
|
|
|
|
|
| 1156 |
``` cpp
|
| 1157 |
struct A {};
|
| 1158 |
struct B : public A {} b;
|
| 1159 |
int f(A&);
|
| 1160 |
int f(B&);
|
| 1161 |
-
int i = f(b);
|
| 1162 |
-
// f(A&), a conversion
|
| 1163 |
```
|
| 1164 |
|
|
|
|
|
|
|
| 1165 |
If the parameter binds directly to the result of applying a conversion
|
| 1166 |
function to the argument expression, the implicit conversion sequence is
|
| 1167 |
a user-defined conversion sequence ([[over.ics.user]]), with the second
|
| 1168 |
standard conversion sequence either an identity conversion or, if the
|
| 1169 |
conversion function returns an entity of a type that is a derived class
|
| 1170 |
of the parameter type, a derived-to-base Conversion.
|
| 1171 |
|
| 1172 |
When a parameter of reference type is not bound directly to an argument
|
| 1173 |
expression, the conversion sequence is the one required to convert the
|
| 1174 |
-
argument expression to the
|
| 1175 |
-
|
| 1176 |
-
|
| 1177 |
-
|
| 1178 |
-
|
| 1179 |
-
conversion.
|
| 1180 |
|
| 1181 |
Except for an implicit object parameter, for which see
|
| 1182 |
[[over.match.funcs]], a standard conversion sequence cannot be formed if
|
| 1183 |
it requires binding an lvalue reference other than a reference to a
|
| 1184 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 1185 |
-
an lvalue other than a function lvalue.
|
| 1186 |
-
|
| 1187 |
-
|
| 1188 |
-
|
| 1189 |
-
|
|
|
|
|
|
|
| 1190 |
|
| 1191 |
Other restrictions on binding a reference to a particular argument that
|
| 1192 |
are not based on the types of the reference and the argument do not
|
| 1193 |
-
affect the formation of a standard conversion sequence, however.
|
| 1194 |
-
|
| 1195 |
-
|
| 1196 |
-
|
| 1197 |
-
|
| 1198 |
-
|
| 1199 |
-
|
| 1200 |
-
|
|
|
|
|
|
|
| 1201 |
|
| 1202 |
##### List-initialization sequence <a id="over.ics.list">[[over.ics.list]]</a>
|
| 1203 |
|
| 1204 |
When an argument is an initializer list ([[dcl.init.list]]), it is not
|
| 1205 |
an expression and special rules apply for converting it to a parameter
|
| 1206 |
type.
|
| 1207 |
|
| 1208 |
-
If the parameter type is `
|
| 1209 |
-
|
| 1210 |
-
conversion sequence is the
|
| 1211 |
-
|
| 1212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1213 |
conversion even in the context of a call to an initializer-list
|
| 1214 |
constructor.
|
| 1215 |
|
|
|
|
|
|
|
| 1216 |
``` cpp
|
| 1217 |
void f(std::initializer_list<int>);
|
| 1218 |
f( {} ); // OK: f(initializer_list<int>) identity conversion
|
| 1219 |
f( {1,2,3} ); // OK: f(initializer_list<int>) identity conversion
|
| 1220 |
f( {'a','b'} ); // OK: f(initializer_list<int>) integral promotion
|
|
@@ -1233,27 +1494,38 @@ g({ "foo", "bar" }); // OK, uses #3
|
|
| 1233 |
typedef int IA[3];
|
| 1234 |
void h(const IA&);
|
| 1235 |
h({ 1, 2, 3 }); // OK: identity conversion
|
| 1236 |
```
|
| 1237 |
|
| 1238 |
-
|
| 1239 |
-
|
| 1240 |
-
|
| 1241 |
-
|
| 1242 |
-
|
| 1243 |
-
|
|
|
|
| 1244 |
|
| 1245 |
Otherwise, if the parameter is a non-aggregate class `X` and overload
|
| 1246 |
-
resolution per [[over.match.list]] chooses a single best constructor
|
| 1247 |
-
`X` to perform the initialization of an object of type `X` from
|
| 1248 |
-
argument initializer list
|
| 1249 |
-
|
| 1250 |
-
|
| 1251 |
-
|
| 1252 |
-
|
| 1253 |
-
|
| 1254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1255 |
|
| 1256 |
``` cpp
|
| 1257 |
struct A {
|
| 1258 |
A(std::initializer_list<int>);
|
| 1259 |
};
|
|
@@ -1281,16 +1553,20 @@ struct D {
|
|
| 1281 |
};
|
| 1282 |
void i(D);
|
| 1283 |
i({ {1,2}, {"bar"} }); // OK: i(D(A(std::initializer_list<int>{1,2\), C(std::string("bar"))))}
|
| 1284 |
```
|
| 1285 |
|
|
|
|
|
|
|
| 1286 |
Otherwise, if the parameter has an aggregate type which can be
|
| 1287 |
initialized from the initializer list according to the rules for
|
| 1288 |
aggregate initialization ([[dcl.init.aggr]]), the implicit conversion
|
| 1289 |
sequence is a user-defined conversion sequence with the second standard
|
| 1290 |
conversion sequence an identity conversion.
|
| 1291 |
|
|
|
|
|
|
|
| 1292 |
``` cpp
|
| 1293 |
struct A {
|
| 1294 |
int m1;
|
| 1295 |
double m2;
|
| 1296 |
};
|
|
@@ -1298,13 +1574,18 @@ struct A {
|
|
| 1298 |
void f(A);
|
| 1299 |
f( {'a', 'b'} ); // OK: f(A(int,double)) user-defined conversion
|
| 1300 |
f( {1.0} ); // error: narrowing
|
| 1301 |
```
|
| 1302 |
|
| 1303 |
-
|
| 1304 |
-
|
| 1305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1306 |
|
| 1307 |
``` cpp
|
| 1308 |
struct A {
|
| 1309 |
int m1;
|
| 1310 |
double m2;
|
|
@@ -1316,33 +1597,41 @@ f( {1.0} ); // error: narrowing
|
|
| 1316 |
|
| 1317 |
void g(const double &);
|
| 1318 |
g({1}); // same conversion as int to double
|
| 1319 |
```
|
| 1320 |
|
|
|
|
|
|
|
| 1321 |
Otherwise, if the parameter type is not a class:
|
| 1322 |
|
| 1323 |
-
- if the initializer list has one element
|
| 1324 |
-
|
| 1325 |
-
type;
|
|
|
|
| 1326 |
``` cpp
|
| 1327 |
void f(int);
|
| 1328 |
f( {'a'} ); // OK: same conversion as char to int
|
| 1329 |
f( {1.0} ); // error: narrowing
|
| 1330 |
```
|
|
|
|
|
|
|
| 1331 |
- if the initializer list has no elements, the implicit conversion
|
| 1332 |
sequence is the identity conversion.
|
|
|
|
| 1333 |
``` cpp
|
| 1334 |
void f(int);
|
| 1335 |
f( { } ); // OK: identity conversion
|
| 1336 |
```
|
| 1337 |
|
|
|
|
|
|
|
| 1338 |
In all cases other than those enumerated above, no conversion is
|
| 1339 |
possible.
|
| 1340 |
|
| 1341 |
#### Ranking implicit conversion sequences <a id="over.ics.rank">[[over.ics.rank]]</a>
|
| 1342 |
|
| 1343 |
-
|
| 1344 |
sequences based on the relationships *better conversion sequence* and
|
| 1345 |
*better conversion*. If an implicit conversion sequence S1 is defined by
|
| 1346 |
these rules to be a better conversion sequence than S2, then it is also
|
| 1347 |
the case that S2 is a *worse conversion sequence* than S1. If conversion
|
| 1348 |
sequence S1 is neither better than nor worse than conversion sequence
|
|
@@ -1359,10 +1648,31 @@ defined in [[over.best.ics]])
|
|
| 1359 |
[[over.ics.ellipsis]]).
|
| 1360 |
|
| 1361 |
Two implicit conversion sequences of the same form are indistinguishable
|
| 1362 |
conversion sequences unless one of the following rules applies:
|
| 1363 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1364 |
- Standard conversion sequence `S1` is a better conversion sequence than
|
| 1365 |
standard conversion sequence `S2` if
|
| 1366 |
- `S1` is a proper subsequence of `S2` (comparing the conversion
|
| 1367 |
sequences in the canonical form defined by [[over.ics.scs]],
|
| 1368 |
excluding any Lvalue Transformation; the identity conversion
|
|
@@ -1372,11 +1682,12 @@ conversion sequences unless one of the following rules applies:
|
|
| 1372 |
have the same rank and are distinguishable by the rules in the
|
| 1373 |
paragraph below, or, if not that,
|
| 1374 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and neither
|
| 1375 |
refers to an implicit object parameter of a non-static member
|
| 1376 |
function declared without a *ref-qualifier*, and `S1` binds an
|
| 1377 |
-
rvalue reference to an rvalue and `S2` binds an lvalue reference
|
|
|
|
| 1378 |
``` cpp
|
| 1379 |
int i;
|
| 1380 |
int f1();
|
| 1381 |
int&& f2();
|
| 1382 |
int g(const int&);
|
|
@@ -1398,41 +1709,47 @@ conversion sequences unless one of the following rules applies:
|
|
| 1398 |
a << 'c'; // calls A::operator<<(int)
|
| 1399 |
A().p(); // calls A::p()&&
|
| 1400 |
a.p(); // calls A::p()&
|
| 1401 |
```
|
| 1402 |
|
|
|
|
| 1403 |
or, if not that,
|
| 1404 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and `S1`
|
| 1405 |
binds an lvalue reference to a function lvalue and `S2` binds an
|
| 1406 |
-
rvalue reference to a function lvalue
|
|
|
|
| 1407 |
``` cpp
|
| 1408 |
int f(void(&)()); // #1
|
| 1409 |
int f(void(&&)()); // #2
|
| 1410 |
void g();
|
| 1411 |
int i1 = f(g); // calls #1
|
| 1412 |
```
|
| 1413 |
|
|
|
|
| 1414 |
or, if not that,
|
| 1415 |
- `S1`
|
| 1416 |
and `S2` differ only in their qualification conversion and yield
|
| 1417 |
similar types `T1` and `T2` ([[conv.qual]]), respectively, and the
|
| 1418 |
cv-qualification signature of type `T1` is a proper subset of the
|
| 1419 |
-
cv-qualification signature of type `T2`
|
|
|
|
| 1420 |
``` cpp
|
| 1421 |
int f(const volatile int *);
|
| 1422 |
int f(const int *);
|
| 1423 |
int i;
|
| 1424 |
int j = f(&i); // calls f(const int*)
|
| 1425 |
```
|
| 1426 |
|
|
|
|
| 1427 |
or, if not that,
|
| 1428 |
- `S1`
|
| 1429 |
and `S2` are reference bindings ([[dcl.init.ref]]), and the types
|
| 1430 |
to which the references refer are the same type except for top-level
|
| 1431 |
cv-qualifiers, and the type to which the reference initialized by
|
| 1432 |
`S2` refers is more cv-qualified than the type to which the
|
| 1433 |
reference initialized by `S1` refers.
|
|
|
|
| 1434 |
``` cpp
|
| 1435 |
int f(const int &);
|
| 1436 |
int f(int &);
|
| 1437 |
int g(const int &);
|
| 1438 |
int g(int);
|
|
@@ -1448,31 +1765,30 @@ conversion sequences unless one of the following rules applies:
|
|
| 1448 |
void g(const X& a, X b) {
|
| 1449 |
a.f(); // calls X::f() const
|
| 1450 |
b.f(); // calls X::f()
|
| 1451 |
}
|
| 1452 |
```
|
|
|
|
|
|
|
| 1453 |
- User-defined conversion sequence `U1` is a better conversion sequence
|
| 1454 |
than another user-defined conversion sequence `U2` if they contain the
|
| 1455 |
same user-defined conversion function or constructor or they
|
| 1456 |
initialize the same class in an aggregate initialization and in either
|
| 1457 |
case the second standard conversion sequence of `U1` is better than
|
| 1458 |
the second standard conversion sequence of `U2`.
|
|
|
|
| 1459 |
``` cpp
|
| 1460 |
struct A {
|
| 1461 |
operator short();
|
| 1462 |
} a;
|
| 1463 |
int f(int);
|
| 1464 |
int f(float);
|
| 1465 |
int i = f(a); // calls f(int), because short → int is
|
| 1466 |
// better than short → float.
|
| 1467 |
```
|
| 1468 |
-
|
| 1469 |
-
|
| 1470 |
-
- `L1` converts to `std::initializer_list<X>` for some `X` and `L2`
|
| 1471 |
-
does not, or, if not that,
|
| 1472 |
-
- `L1` converts to type “array of `N1` `T`”, `L2` converts to type
|
| 1473 |
-
“array of `N2` `T`”, and `N1` is smaller than `N2`.
|
| 1474 |
|
| 1475 |
Standard conversion sequences are ordered by their ranks: an Exact Match
|
| 1476 |
is a better conversion than a Promotion, which is a better conversion
|
| 1477 |
than a Conversion. Two conversion sequences with the same rank are
|
| 1478 |
indistinguishable unless one of the following rules applies:
|
|
@@ -1488,19 +1804,22 @@ indistinguishable unless one of the following rules applies:
|
|
| 1488 |
of `B*` to `void*`.
|
| 1489 |
- If class `B` is derived directly or indirectly from class `A` and
|
| 1490 |
class `C` is derived directly or indirectly from `B`,
|
| 1491 |
- conversion of `C*` to `B*` is better than conversion of `C*` to
|
| 1492 |
`A*`,
|
|
|
|
| 1493 |
``` cpp
|
| 1494 |
struct A {};
|
| 1495 |
struct B : public A {};
|
| 1496 |
struct C : public B {};
|
| 1497 |
C* pc;
|
| 1498 |
int f(A*);
|
| 1499 |
int f(B*);
|
| 1500 |
int i = f(pc); // calls f(B*)
|
| 1501 |
```
|
|
|
|
|
|
|
| 1502 |
- binding of an expression of type `C` to a reference to type `B` is
|
| 1503 |
better than binding an expression of type `C` to a reference to type
|
| 1504 |
`A`,
|
| 1505 |
- conversion of `A::*` to `B::*` is better than conversion of `A::*`
|
| 1506 |
to `C::*`,
|
|
@@ -1512,26 +1831,31 @@ indistinguishable unless one of the following rules applies:
|
|
| 1512 |
`A`,
|
| 1513 |
- conversion of `B::*` to `C::*` is better than conversion of `A::*`
|
| 1514 |
to `C::*`, and
|
| 1515 |
- conversion of `B` to `A` is better than conversion of `C` to `A`.
|
| 1516 |
|
| 1517 |
-
Compared conversion sequences will have different source
|
| 1518 |
-
the context of comparing the second standard conversion
|
| 1519 |
-
initialization by user-defined conversion (see
|
| 1520 |
-
in all other contexts, the source types will be
|
| 1521 |
-
target types will be different.
|
| 1522 |
|
| 1523 |
## Address of overloaded function <a id="over.over">[[over.over]]</a>
|
| 1524 |
|
| 1525 |
A use of an overloaded function name without arguments is resolved in
|
| 1526 |
certain contexts to a function, a pointer to function or a pointer to
|
| 1527 |
member function for a specific function from the overload set. A
|
| 1528 |
function template name is considered to name a set of overloaded
|
| 1529 |
-
functions in such contexts.
|
| 1530 |
-
|
| 1531 |
-
|
| 1532 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1533 |
|
| 1534 |
- an object or reference being initialized ([[dcl.init]],
|
| 1535 |
[[dcl.init.ref]], [[dcl.init.list]]),
|
| 1536 |
- the left side of an assignment ([[expr.ass]]),
|
| 1537 |
- a parameter of a function ([[expr.call]]),
|
|
@@ -1542,28 +1866,32 @@ when matching a pointer-to-member-function type. The target can be
|
|
| 1542 |
[[expr.static.cast]], [[expr.cast]]), or
|
| 1543 |
- a non-type *template-parameter* ([[temp.arg.nontype]]).
|
| 1544 |
|
| 1545 |
The overloaded function name can be preceded by the `&` operator. An
|
| 1546 |
overloaded function name shall not be used without arguments in contexts
|
| 1547 |
-
other than those listed.
|
| 1548 |
-
|
|
|
|
|
|
|
| 1549 |
|
| 1550 |
If the name is a function template, template argument deduction is
|
| 1551 |
done ([[temp.deduct.funcaddr]]), and if the argument deduction
|
| 1552 |
succeeds, the resulting template argument list is used to generate a
|
| 1553 |
single function template specialization, which is added to the set of
|
| 1554 |
-
overloaded functions considered.
|
| 1555 |
-
if deduction fails and the function template name is followed by an
|
| 1556 |
-
explicit template argument list, the *template-id* is then examined to
|
| 1557 |
-
see whether it identifies a single function template specialization. If
|
| 1558 |
-
it does, the *template-id* is considered to be an lvalue for that
|
| 1559 |
-
function template specialization. The target type is not used in that
|
| 1560 |
-
determination.
|
| 1561 |
|
| 1562 |
-
|
| 1563 |
-
|
| 1564 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1565 |
non-static member function is selected, the reference to the overloaded
|
| 1566 |
function name is required to have the form of a pointer to member as
|
| 1567 |
described in [[expr.unary.op]].
|
| 1568 |
|
| 1569 |
If more than one function is selected, any function template
|
|
@@ -1573,10 +1901,12 @@ function template specialization `F1` is eliminated if the set contains
|
|
| 1573 |
a second function template specialization whose function template is
|
| 1574 |
more specialized than the function template of `F1` according to the
|
| 1575 |
partial ordering rules of [[temp.func.order]]. After such eliminations,
|
| 1576 |
if any, there shall remain exactly one selected function.
|
| 1577 |
|
|
|
|
|
|
|
| 1578 |
``` cpp
|
| 1579 |
int f(double);
|
| 1580 |
int f(int);
|
| 1581 |
int (*pfd)(double) = &f; // selects f(double)
|
| 1582 |
int (*pfi)(int) = &f; // selects f(int)
|
|
@@ -1605,26 +1935,30 @@ int (X::*p4)(long) = &X::f; // error: mismatch
|
|
| 1605 |
int (X::*p5)(int) = &(X::f); // error: wrong syntax for
|
| 1606 |
// pointer to member
|
| 1607 |
int (*p6)(long) = &(X::f); // OK
|
| 1608 |
```
|
| 1609 |
|
| 1610 |
-
|
| 1611 |
-
possibilities must be considered to resolve `f(&g)`, or the equivalent
|
| 1612 |
-
expression `f(g)`.
|
| 1613 |
|
| 1614 |
-
|
| 1615 |
-
|
| 1616 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1617 |
|
| 1618 |
``` cpp
|
| 1619 |
D* f();
|
| 1620 |
B* (*p1)() = &f; // error
|
| 1621 |
|
| 1622 |
void g(D*);
|
| 1623 |
void (*p2)(B*) = &g; // error
|
| 1624 |
```
|
| 1625 |
|
|
|
|
|
|
|
| 1626 |
## Overloaded operators <a id="over.oper">[[over.oper]]</a>
|
| 1627 |
|
| 1628 |
A function declaration having one of the following
|
| 1629 |
*operator-function-id*s as its name declares an *operator function*. A
|
| 1630 |
function template declaration having one of the following
|
|
@@ -1636,13 +1970,13 @@ operator named in its *operator-function-id*.
|
|
| 1636 |
``` bnf
|
| 1637 |
operator-function-id:
|
| 1638 |
'operator' operator
|
| 1639 |
```
|
| 1640 |
|
| 1641 |
-
The last two operators are function call ([[expr.call]])
|
| 1642 |
-
subscripting ([[expr.sub]]). The operators `new[]`, `delete[]`,
|
| 1643 |
-
and `[]` are formed from more than one token.
|
| 1644 |
|
| 1645 |
Both the unary and binary forms of
|
| 1646 |
|
| 1647 |
``` cpp
|
| 1648 |
+ - * &
|
|
@@ -1662,15 +1996,19 @@ Operator functions are usually not called directly; instead they are
|
|
| 1662 |
invoked to evaluate the operators they implement ([[over.unary]] –
|
| 1663 |
[[over.inc]]). They can be explicitly called, however, using the
|
| 1664 |
*operator-function-id* as the name of the function in the function call
|
| 1665 |
syntax ([[expr.call]]).
|
| 1666 |
|
|
|
|
|
|
|
| 1667 |
``` cpp
|
| 1668 |
complex z = a.operator+(b); // complex z = a+b;
|
| 1669 |
void* p = operator new(sizeof(int)*n);
|
| 1670 |
```
|
| 1671 |
|
|
|
|
|
|
|
| 1672 |
The allocation and deallocation functions, `operator` `new`, `operator`
|
| 1673 |
`new[]`, `operator` `delete` and `operator` `delete[]`, are described
|
| 1674 |
completely in [[basic.stc.dynamic]]. The attributes and restrictions
|
| 1675 |
found in the rest of this subclause do not apply to them unless
|
| 1676 |
explicitly stated in [[basic.stc.dynamic]].
|
|
@@ -1710,12 +2048,14 @@ of the operator function have been declared, the rules in
|
|
| 1710 |
[[over.match.oper]] determine which, if any, interpretation is used.
|
| 1711 |
See [[over.inc]] for an explanation of the postfix unary operators `++`
|
| 1712 |
and `\dcr`.
|
| 1713 |
|
| 1714 |
The unary and binary forms of the same operator are considered to have
|
| 1715 |
-
the same name.
|
| 1716 |
-
|
|
|
|
|
|
|
| 1717 |
|
| 1718 |
### Binary operators <a id="over.binary">[[over.binary]]</a>
|
| 1719 |
|
| 1720 |
A binary operator shall be implemented either by a non-static member
|
| 1721 |
function ([[class.mfct]]) with one parameter or by a non-member
|
|
@@ -1731,14 +2071,20 @@ function with exactly one parameter. Because a copy assignment operator
|
|
| 1731 |
`operator=` is implicitly declared for a class if not declared by the
|
| 1732 |
user ([[class.copy]]), a base class assignment operator is always
|
| 1733 |
hidden by the copy assignment operator of the derived class.
|
| 1734 |
|
| 1735 |
Any assignment operator, even the copy and move assignment operators,
|
| 1736 |
-
can be virtual.
|
| 1737 |
-
|
| 1738 |
-
|
| 1739 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1740 |
|
| 1741 |
``` cpp
|
| 1742 |
struct B {
|
| 1743 |
virtual int operator= (int);
|
| 1744 |
virtual B& operator= (const B&);
|
|
@@ -1754,15 +2100,18 @@ B* bptr = &dobj1;
|
|
| 1754 |
void f() {
|
| 1755 |
bptr->operator=(99); // calls D::operator=(int)
|
| 1756 |
*bptr = 99; // ditto
|
| 1757 |
bptr->operator=(dobj2); // calls D::operator=(const B&)
|
| 1758 |
*bptr = dobj2; // ditto
|
| 1759 |
-
dobj1 = dobj2; // calls implicitly-declared
|
| 1760 |
-
// D::operator=(const D&)
|
| 1761 |
}
|
| 1762 |
```
|
| 1763 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1764 |
### Function call <a id="over.call">[[over.call]]</a>
|
| 1765 |
|
| 1766 |
`operator()`
|
| 1767 |
|
| 1768 |
shall be a non-static member function with an arbitrary number of
|
|
@@ -1787,35 +2136,33 @@ mechanism ([[over.match.best]]).
|
|
| 1787 |
|
| 1788 |
shall be a non-static member function with exactly one parameter. It
|
| 1789 |
implements the subscripting syntax
|
| 1790 |
|
| 1791 |
``` bnf
|
| 1792 |
-
postfix-expression '['
|
| 1793 |
-
```
|
| 1794 |
-
|
| 1795 |
-
or
|
| 1796 |
-
|
| 1797 |
-
``` bnf
|
| 1798 |
-
postfix-expression '[' braced-init-list ']'
|
| 1799 |
```
|
| 1800 |
|
| 1801 |
Thus, a subscripting expression `x[y]` is interpreted as
|
| 1802 |
`x.operator[](y)` for a class object `x` of type `T` if
|
| 1803 |
`T::operator[](T1)` exists and if the operator is selected as the best
|
| 1804 |
match function by the overload resolution mechanism (
|
| 1805 |
[[over.match.best]]).
|
| 1806 |
|
|
|
|
|
|
|
| 1807 |
``` cpp
|
| 1808 |
struct X {
|
| 1809 |
Z operator[](std::initializer_list<int>);
|
| 1810 |
};
|
| 1811 |
X x;
|
| 1812 |
x[{1,2,3}] = 7; // OK: meaning x.operator[]({1,2,3\)}
|
| 1813 |
int a[10];
|
| 1814 |
a[{1,2,3}] = 7; // error: built-in subscript operator
|
| 1815 |
```
|
| 1816 |
|
|
|
|
|
|
|
| 1817 |
### Class member access <a id="over.ref">[[over.ref]]</a>
|
| 1818 |
|
| 1819 |
`operator->`
|
| 1820 |
|
| 1821 |
shall be a non-static member function taking no parameters. It
|
|
@@ -1840,11 +2187,13 @@ defines the prefix increment operator `++` for objects of that type. If
|
|
| 1840 |
the function is a non-static member function with one parameter (which
|
| 1841 |
shall be of type `int`) or a non-member function with two parameters
|
| 1842 |
(the second of which shall be of type `int`), it defines the postfix
|
| 1843 |
increment operator `++` for objects of that type. When the postfix
|
| 1844 |
increment is called as a result of using the `++` operator, the `int`
|
| 1845 |
-
argument will have value zero.[^
|
|
|
|
|
|
|
| 1846 |
|
| 1847 |
``` cpp
|
| 1848 |
struct X {
|
| 1849 |
X& operator++(); // prefix ++a
|
| 1850 |
X operator++(int); // postfix a++
|
|
@@ -1865,10 +2214,12 @@ void f(X a, Y b) {
|
|
| 1865 |
operator++(b); // explicit call: like ++b;
|
| 1866 |
operator++(b, 0); // explicit call: like b++;
|
| 1867 |
}
|
| 1868 |
```
|
| 1869 |
|
|
|
|
|
|
|
| 1870 |
The prefix and postfix decrement operators `-{-}` are handled
|
| 1871 |
analogously.
|
| 1872 |
|
| 1873 |
### User-defined literals <a id="over.literal">[[over.literal]]</a>
|
| 1874 |
|
|
@@ -1880,13 +2231,14 @@ literal-operator-id:
|
|
| 1880 |
|
| 1881 |
The *string-literal* or *user-defined-string-literal* in a
|
| 1882 |
*literal-operator-id* shall have no *encoding-prefix* and shall contain
|
| 1883 |
no characters other than the implicit terminating `'\0'`. The
|
| 1884 |
*ud-suffix* of the *user-defined-string-literal* or the *identifier* in
|
| 1885 |
-
a *literal-operator-id* is called a *literal suffix identifier*.
|
| 1886 |
literal suffix identifiers are reserved for future standardization; see
|
| 1887 |
-
[[usrlit.suffix]].
|
|
|
|
| 1888 |
|
| 1889 |
A declaration whose *declarator-id* is a *literal-operator-id* shall be
|
| 1890 |
a declaration of a namespace-scope function or function template (it
|
| 1891 |
could be a friend function ([[class.friend]])), an explicit
|
| 1892 |
instantiation or specialization of a function template, or a
|
|
@@ -1923,41 +2275,48 @@ have a single *template-parameter* that is a non-type template parameter
|
|
| 1923 |
pack ([[temp.variadic]]) with element type `char`.
|
| 1924 |
|
| 1925 |
Literal operators and literal operator templates shall not have C
|
| 1926 |
language linkage.
|
| 1927 |
|
| 1928 |
-
Literal operators and literal operator templates are usually
|
| 1929 |
-
implicitly through user-defined literals ([[lex.ext]]).
|
| 1930 |
-
for the constraints described above, they are ordinary
|
| 1931 |
-
functions and function templates. In particular, they
|
| 1932 |
-
ordinary functions and function templates and they
|
| 1933 |
-
overload resolution rules. Also, they can be declared
|
| 1934 |
-
`constexpr`, they may have internal or external linkage,
|
| 1935 |
-
called explicitly, their addresses can be taken,
|
|
|
|
|
|
|
|
|
|
| 1936 |
|
| 1937 |
``` cpp
|
| 1938 |
void operator "" _km(long double); // OK
|
| 1939 |
string operator "" _i18n(const char*, std::size_t); // OK
|
| 1940 |
template <char...> double operator "" _\u03C0(); // OK: UCN for lowercase pi
|
| 1941 |
float operator ""_e(const char*); // OK
|
| 1942 |
float operator ""E(const char*); // error: reserved literal suffix~([usrlit.suffix], [lex.ext])
|
| 1943 |
-
double operator""_Bq(long double); // OK: does not use the reserved
|
| 1944 |
-
double operator"" _Bq(long double); // uses the reserved
|
| 1945 |
float operator " " B(const char*); // error: non-empty string-literal
|
| 1946 |
string operator "" 5X(const char*, std::size_t); // error: invalid literal suffix identifier
|
| 1947 |
double operator "" _miles(double); // error: invalid parameter-declaration-clause
|
| 1948 |
template <char...> int operator "" _j(const char*); // error: invalid parameter-declaration-clause
|
| 1949 |
extern "C" void operator "" _m(long double); // error: C language linkage
|
| 1950 |
```
|
| 1951 |
|
|
|
|
|
|
|
| 1952 |
## Built-in operators <a id="over.built">[[over.built]]</a>
|
| 1953 |
|
| 1954 |
The candidate operator functions that represent the built-in operators
|
| 1955 |
defined in Clause [[expr]] are specified in this subclause. These
|
| 1956 |
candidate functions participate in the operator overload resolution
|
| 1957 |
process as described in [[over.match.oper]] and are used for no other
|
| 1958 |
-
purpose.
|
|
|
|
|
|
|
| 1959 |
type, and operator overload resolution occurs only when an operand
|
| 1960 |
expression originally has class or enumeration type, operator overload
|
| 1961 |
resolution can resolve to a built-in operator only when an operand has a
|
| 1962 |
class type that has a user-defined conversion to a non-class type
|
| 1963 |
appropriate for the operator, or when an operand has an enumeration type
|
|
@@ -1968,99 +2327,101 @@ in [[over.match.oper]], after a built-in operator is selected by
|
|
| 1968 |
overload resolution the expression is subject to the requirements for
|
| 1969 |
the built-in operator given in Clause [[expr]], and therefore to any
|
| 1970 |
additional semantic constraints given there. If there is a user-written
|
| 1971 |
candidate with the same name and parameter types as a built-in candidate
|
| 1972 |
operator function, the built-in operator function is hidden and is not
|
| 1973 |
-
included in the set of candidate functions.
|
| 1974 |
|
| 1975 |
In this subclause, the term *promoted integral type* is used to refer to
|
| 1976 |
-
those integral types which are preserved by integral promotion
|
| 1977 |
-
(including e.g. `int` and `long` but excluding e.g.
|
| 1978 |
-
the term *promoted arithmetic type* refers to
|
| 1979 |
-
|
| 1980 |
-
|
| 1981 |
-
|
| 1982 |
-
|
| 1983 |
-
|
| 1984 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1985 |
the form
|
| 1986 |
|
| 1987 |
``` cpp
|
| 1988 |
-
|
| 1989 |
-
T operator
|
|
|
|
|
|
|
| 1990 |
```
|
| 1991 |
|
| 1992 |
-
For every
|
| 1993 |
-
*bool*, and *VQ* is either `volatile` or empty, there exist candidate
|
| 1994 |
-
operator functions of the form
|
| 1995 |
-
|
| 1996 |
-
``` cpp
|
| 1997 |
-
VQ T& operator--(VQ T&);
|
| 1998 |
-
T operator--(VQ T&, int);
|
| 1999 |
-
```
|
| 2000 |
-
|
| 2001 |
-
For every pair (*T*, *VQ*), where *T* is a cv-qualified or
|
| 2002 |
-
cv-unqualified object type, and *VQ* is either `volatile` or empty,
|
| 2003 |
-
there exist candidate operator functions of the form
|
| 2004 |
-
|
| 2005 |
-
``` cpp
|
| 2006 |
-
T*VQ& operator++(T*VQ&);
|
| 2007 |
-
T*VQ& operator--(T*VQ&);
|
| 2008 |
-
T* operator++(T*VQ&, int);
|
| 2009 |
-
T* operator--(T*VQ&, int);
|
| 2010 |
-
```
|
| 2011 |
-
|
| 2012 |
-
For every cv-qualified or cv-unqualified object type *T*, there exist
|
| 2013 |
candidate operator functions of the form
|
| 2014 |
|
| 2015 |
``` cpp
|
| 2016 |
T& operator*(T*);
|
| 2017 |
```
|
| 2018 |
|
| 2019 |
-
For every function type
|
| 2020 |
*ref-qualifier*, there exist candidate operator functions of the form
|
| 2021 |
|
| 2022 |
``` cpp
|
| 2023 |
T& operator*(T*);
|
| 2024 |
```
|
| 2025 |
|
| 2026 |
-
For every type
|
| 2027 |
|
| 2028 |
``` cpp
|
| 2029 |
T* operator+(T*);
|
| 2030 |
```
|
| 2031 |
|
| 2032 |
-
For every promoted arithmetic type
|
| 2033 |
functions of the form
|
| 2034 |
|
| 2035 |
``` cpp
|
| 2036 |
T operator+(T);
|
| 2037 |
T operator-(T);
|
| 2038 |
```
|
| 2039 |
|
| 2040 |
-
For every promoted integral type
|
| 2041 |
functions of the form
|
| 2042 |
|
| 2043 |
``` cpp
|
| 2044 |
T operator~(T);
|
| 2045 |
```
|
| 2046 |
|
| 2047 |
-
For every quintuple (
|
| 2048 |
-
class type,
|
| 2049 |
-
is an object type or a function type,
|
| 2050 |
-
|
| 2051 |
-
form
|
| 2052 |
|
| 2053 |
``` cpp
|
| 2054 |
-
|
| 2055 |
```
|
| 2056 |
|
| 2057 |
-
where *
|
| 2058 |
for exposition only; see [[expr.mptr.oper]] for the determination of
|
| 2059 |
the operator’s result type.
|
| 2060 |
|
| 2061 |
-
For every pair of promoted arithmetic types
|
| 2062 |
candidate operator functions of the form
|
| 2063 |
|
| 2064 |
``` cpp
|
| 2065 |
LR operator*(L, R);
|
| 2066 |
LR operator/(L, R);
|
|
@@ -2072,32 +2433,32 @@ bool operator<=(L, R);
|
|
| 2072 |
bool operator>=(L, R);
|
| 2073 |
bool operator==(L, R);
|
| 2074 |
bool operator!=(L, R);
|
| 2075 |
```
|
| 2076 |
|
| 2077 |
-
where
|
| 2078 |
-
types
|
| 2079 |
|
| 2080 |
-
For every cv-qualified or cv-unqualified object type
|
| 2081 |
candidate operator functions of the form
|
| 2082 |
|
| 2083 |
``` cpp
|
| 2084 |
T* operator+(T*, std::ptrdiff_t);
|
| 2085 |
T& operator[](T*, std::ptrdiff_t);
|
| 2086 |
T* operator-(T*, std::ptrdiff_t);
|
| 2087 |
T* operator+(std::ptrdiff_t, T*);
|
| 2088 |
T& operator[](std::ptrdiff_t, T*);
|
| 2089 |
```
|
| 2090 |
|
| 2091 |
-
For every
|
| 2092 |
candidate operator functions of the form
|
| 2093 |
|
| 2094 |
``` cpp
|
| 2095 |
std::ptrdiff_t operator-(T, T);
|
| 2096 |
```
|
| 2097 |
|
| 2098 |
-
For every
|
| 2099 |
exist candidate operator functions of the form
|
| 2100 |
|
| 2101 |
``` cpp
|
| 2102 |
bool operator<(T, T);
|
| 2103 |
bool operator>(T, T);
|
|
@@ -2105,19 +2466,19 @@ bool operator<=(T, T);
|
|
| 2105 |
bool operator>=(T, T);
|
| 2106 |
bool operator==(T, T);
|
| 2107 |
bool operator!=(T, T);
|
| 2108 |
```
|
| 2109 |
|
| 2110 |
-
For every pointer to member type
|
| 2111 |
exist candidate operator functions of the form
|
| 2112 |
|
| 2113 |
``` cpp
|
| 2114 |
bool operator==(T, T);
|
| 2115 |
bool operator!=(T, T);
|
| 2116 |
```
|
| 2117 |
|
| 2118 |
-
For every pair of promoted integral types
|
| 2119 |
candidate operator functions of the form
|
| 2120 |
|
| 2121 |
``` cpp
|
| 2122 |
LR operator%(L, R);
|
| 2123 |
LR operator&(L, R);
|
|
@@ -2125,85 +2486,85 @@ LR operator^(L, R);
|
|
| 2125 |
LR operator|(L, R);
|
| 2126 |
L operator<<(L, R);
|
| 2127 |
L operator>>(L, R);
|
| 2128 |
```
|
| 2129 |
|
| 2130 |
-
where
|
| 2131 |
-
types
|
| 2132 |
|
| 2133 |
-
For every triple (
|
| 2134 |
-
|
| 2135 |
-
|
| 2136 |
|
| 2137 |
``` cpp
|
| 2138 |
-
|
| 2139 |
-
|
| 2140 |
-
|
| 2141 |
-
|
| 2142 |
-
|
| 2143 |
```
|
| 2144 |
|
| 2145 |
-
For every pair (
|
| 2146 |
-
|
| 2147 |
-
form
|
| 2148 |
|
| 2149 |
``` cpp
|
| 2150 |
-
T*
|
| 2151 |
```
|
| 2152 |
|
| 2153 |
-
For every pair (
|
| 2154 |
-
member type
|
| 2155 |
-
candidate operator functions of the form
|
| 2156 |
|
| 2157 |
``` cpp
|
| 2158 |
-
|
| 2159 |
```
|
| 2160 |
|
| 2161 |
-
For every pair (
|
| 2162 |
-
cv-unqualified object type
|
| 2163 |
-
|
| 2164 |
|
| 2165 |
``` cpp
|
| 2166 |
-
T*
|
| 2167 |
-
T*
|
| 2168 |
```
|
| 2169 |
|
| 2170 |
-
For every triple (
|
| 2171 |
-
|
| 2172 |
-
|
| 2173 |
|
| 2174 |
``` cpp
|
| 2175 |
-
|
| 2176 |
-
|
| 2177 |
-
|
| 2178 |
-
|
| 2179 |
-
|
| 2180 |
-
|
| 2181 |
```
|
| 2182 |
|
| 2183 |
There also exist candidate operator functions of the form
|
| 2184 |
|
| 2185 |
``` cpp
|
| 2186 |
bool operator!(bool);
|
| 2187 |
bool operator&&(bool, bool);
|
| 2188 |
bool operator||(bool, bool);
|
| 2189 |
```
|
| 2190 |
|
| 2191 |
-
For every pair of promoted arithmetic types
|
| 2192 |
candidate operator functions of the form
|
| 2193 |
|
| 2194 |
``` cpp
|
| 2195 |
LR operator?:(bool, L, R);
|
| 2196 |
```
|
| 2197 |
|
| 2198 |
-
where
|
| 2199 |
-
types
|
| 2200 |
-
functions, this declaration serves only to describe the built-in
|
| 2201 |
-
operator for purposes of overload resolution. The operator “`?:`” cannot
|
| 2202 |
-
be overloaded.
|
| 2203 |
|
| 2204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2205 |
enumeration type, there exist candidate operator functions of the form
|
| 2206 |
|
| 2207 |
``` cpp
|
| 2208 |
T operator?:(bool, T, T);
|
| 2209 |
```
|
|
@@ -2224,10 +2585,11 @@ T operator?:(bool, T, T);
|
|
| 2224 |
[class.this]: class.md#class.this
|
| 2225 |
[conv]: conv.md#conv
|
| 2226 |
[conv.array]: conv.md#conv.array
|
| 2227 |
[conv.bool]: conv.md#conv.bool
|
| 2228 |
[conv.double]: conv.md#conv.double
|
|
|
|
| 2229 |
[conv.fpint]: conv.md#conv.fpint
|
| 2230 |
[conv.fpprom]: conv.md#conv.fpprom
|
| 2231 |
[conv.func]: conv.md#conv.func
|
| 2232 |
[conv.integral]: conv.md#conv.integral
|
| 2233 |
[conv.lval]: conv.md#conv.lval
|
|
@@ -2236,16 +2598,19 @@ T operator?:(bool, T, T);
|
|
| 2236 |
[conv.ptr]: conv.md#conv.ptr
|
| 2237 |
[conv.qual]: conv.md#conv.qual
|
| 2238 |
[cpp]: cpp.md#cpp
|
| 2239 |
[dcl.array]: dcl.md#dcl.array
|
| 2240 |
[dcl.fct]: dcl.md#dcl.fct
|
|
|
|
| 2241 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 2242 |
[dcl.init]: dcl.md#dcl.init
|
| 2243 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
| 2244 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 2245 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
|
|
|
| 2246 |
[dcl.typedef]: dcl.md#dcl.typedef
|
|
|
|
| 2247 |
[expr]: expr.md#expr
|
| 2248 |
[expr.ass]: expr.md#expr.ass
|
| 2249 |
[expr.call]: expr.md#expr.call
|
| 2250 |
[expr.cast]: expr.md#expr.cast
|
| 2251 |
[expr.cond]: expr.md#expr.cond
|
|
@@ -2276,10 +2641,11 @@ T operator?:(bool, T, T);
|
|
| 2276 |
[over.literal]: #over.literal
|
| 2277 |
[over.load]: #over.load
|
| 2278 |
[over.match]: #over.match
|
| 2279 |
[over.match.best]: #over.match.best
|
| 2280 |
[over.match.call]: #over.match.call
|
|
|
|
| 2281 |
[over.match.conv]: #over.match.conv
|
| 2282 |
[over.match.copy]: #over.match.copy
|
| 2283 |
[over.match.ctor]: #over.match.ctor
|
| 2284 |
[over.match.funcs]: #over.match.funcs
|
| 2285 |
[over.match.list]: #over.match.list
|
|
@@ -2357,33 +2723,11 @@ T operator?:(bool, T, T);
|
|
| 2357 |
tournament `F` encountered another function `G` such that `F` was
|
| 2358 |
not better than `G`. Hence, `W` is either the best function or there
|
| 2359 |
is no best function. So, make a second pass over the viable
|
| 2360 |
functions to verify that `W` is better than all other functions.
|
| 2361 |
|
| 2362 |
-
[^11]:
|
| 2363 |
-
|
| 2364 |
-
argument can exist only if they involve different user-defined
|
| 2365 |
-
conversions. The ambiguous conversion sequence is indistinguishable
|
| 2366 |
-
from any other user-defined conversion sequence because it
|
| 2367 |
-
represents at least two user-defined conversion sequences, each with
|
| 2368 |
-
a different user-defined conversion, and any other user-defined
|
| 2369 |
-
conversion sequence must be indistinguishable from at least one of
|
| 2370 |
-
them.
|
| 2371 |
|
| 2372 |
-
|
| 2373 |
-
ambiguous conversion sequence for one of its parameters. Consider
|
| 2374 |
-
this example,
|
| 2375 |
-
|
| 2376 |
-
If it were not for this rule, `f(A)` would be eliminated as a viable
|
| 2377 |
-
function for the call `f(b)` causing overload resolution to select
|
| 2378 |
-
`f(C)` as the function to call even though it is not clearly the
|
| 2379 |
-
best choice. On the other hand, if an `f(B)` were to be declared
|
| 2380 |
-
then `f(b)` would resolve to that `f(B)` because the exact match
|
| 2381 |
-
with `f(B)` is better than any of the sequences required to match
|
| 2382 |
-
`f(A)`.
|
| 2383 |
-
|
| 2384 |
-
[^12]: Since there are no parameters of array type, this will only occur
|
| 2385 |
-
as the underlying type of a reference parameter.
|
| 2386 |
-
|
| 2387 |
-
[^13]: Calling `operator++` explicitly, as in expressions like
|
| 2388 |
`a.operator++(2)`, has no special properties: The argument to
|
| 2389 |
`operator++` is `2`.
|
|
|
|
| 12 |
types of the arguments at the point of use with the types of the
|
| 13 |
parameters in the overloaded declarations that are visible at the point
|
| 14 |
of use. This function selection process is called *overload resolution*
|
| 15 |
and is defined in [[over.match]].
|
| 16 |
|
| 17 |
+
[*Example 1*:
|
| 18 |
+
|
| 19 |
``` cpp
|
| 20 |
double abs(double);
|
| 21 |
int abs(int);
|
| 22 |
|
| 23 |
abs(1); // calls abs(int);
|
| 24 |
abs(1.0); // calls abs(double);
|
| 25 |
```
|
| 26 |
|
| 27 |
+
— *end example*]
|
| 28 |
+
|
| 29 |
## Overloadable declarations <a id="over.load">[[over.load]]</a>
|
| 30 |
|
| 31 |
Not all function declarations can be overloaded. Those that cannot be
|
| 32 |
overloaded are specified here. A program is ill-formed if it contains
|
| 33 |
+
two such non-overloadable declarations in the same scope.
|
| 34 |
+
|
| 35 |
+
[*Note 1*: This restriction applies to explicit declarations in a
|
| 36 |
+
scope, and between such declarations and declarations made through a
|
| 37 |
+
*using-declaration* ([[namespace.udecl]]). It does not apply to sets of
|
| 38 |
+
functions fabricated as a result of name lookup (e.g., because of
|
| 39 |
+
*using-directive*s) or overload resolution (e.g., for operator
|
| 40 |
+
functions). — *end note*]
|
| 41 |
|
| 42 |
Certain function declarations cannot be overloaded:
|
| 43 |
|
| 44 |
+
- Function declarations that differ only in the return type, the
|
| 45 |
+
exception specification ([[except.spec]]), or both cannot be
|
| 46 |
overloaded.
|
| 47 |
- Member function declarations with the same name and the same
|
| 48 |
+
parameter-type-list ([[dcl.fct]]) cannot be overloaded if any of them
|
| 49 |
+
is a `static` member function declaration ([[class.static]]).
|
| 50 |
+
Likewise, member function template declarations with the same name,
|
| 51 |
+
the same parameter-type-list, and the same template parameter lists
|
| 52 |
+
cannot be overloaded if any of them is a `static` member function
|
| 53 |
+
template declaration. The types of the implicit object parameters
|
| 54 |
+
constructed for the member functions for the purpose of overload
|
| 55 |
+
resolution ([[over.match.funcs]]) are not considered when comparing
|
| 56 |
parameter-type-lists for enforcement of this rule. In contrast, if
|
| 57 |
there is no `static` member function declaration among a set of member
|
| 58 |
function declarations with the same name and the same
|
| 59 |
parameter-type-list, then these member function declarations can be
|
| 60 |
overloaded if they differ in the type of their implicit object
|
| 61 |
+
parameter.
|
| 62 |
+
\[*Example 1*:
|
| 63 |
+
The following illustrates this distinction:
|
| 64 |
``` cpp
|
| 65 |
class X {
|
| 66 |
static void f();
|
| 67 |
void f(); // ill-formed
|
| 68 |
void f() const; // ill-formed
|
|
|
|
| 70 |
void g();
|
| 71 |
void g() const; // OK: no static g
|
| 72 |
void g() const volatile; // OK: no static g
|
| 73 |
};
|
| 74 |
```
|
| 75 |
+
|
| 76 |
+
— *end example*]
|
| 77 |
- Member function declarations with the same name and the same
|
| 78 |
+
parameter-type-list ([[dcl.fct]]) as well as member function template
|
| 79 |
+
declarations with the same name, the same parameter-type-list, and the
|
| 80 |
+
same template parameter lists cannot be overloaded if any of them, but
|
| 81 |
+
not all, have a *ref-qualifier* ([[dcl.fct]]).
|
| 82 |
+
\[*Example 2*:
|
| 83 |
``` cpp
|
| 84 |
class Y {
|
| 85 |
void h() &;
|
| 86 |
void h() const &; // OK
|
| 87 |
void h() &&; // OK, all declarations have a ref-qualifier
|
|
|
|
| 89 |
void i() const; // ill-formed, prior declaration of i
|
| 90 |
// has a ref-qualifier
|
| 91 |
};
|
| 92 |
```
|
| 93 |
|
| 94 |
+
— *end example*]
|
| 95 |
+
|
| 96 |
+
[*Note 2*:
|
| 97 |
+
|
| 98 |
As specified in [[dcl.fct]], function declarations that have equivalent
|
| 99 |
parameter declarations declare the same function and therefore cannot be
|
| 100 |
overloaded:
|
| 101 |
|
| 102 |
- Parameter declarations that differ only in the use of equivalent
|
| 103 |
typedef “types” are equivalent. A `typedef` is not a separate type,
|
| 104 |
but only a synonym for another type ([[dcl.typedef]]).
|
| 105 |
+
\[*Example 3*:
|
| 106 |
``` cpp
|
| 107 |
typedef int Int;
|
| 108 |
|
| 109 |
void f(int i);
|
| 110 |
void f(Int i); // OK: redeclaration of f(int)
|
| 111 |
+
void f(int i) { ... }
|
| 112 |
+
void f(Int i) { ... } // error: redefinition of f(int)
|
| 113 |
```
|
| 114 |
|
| 115 |
+
— *end example*]
|
| 116 |
Enumerations, on the other hand, are distinct types and can be used to
|
| 117 |
distinguish overloaded function declarations.
|
| 118 |
+
\[*Example 4*:
|
| 119 |
``` cpp
|
| 120 |
enum E { a };
|
| 121 |
|
| 122 |
+
void f(int i) { ... }
|
| 123 |
+
void f(E i) { ... }
|
| 124 |
```
|
| 125 |
+
|
| 126 |
+
— *end example*]
|
| 127 |
- Parameter declarations that differ only in a pointer `*` versus an
|
| 128 |
array `[]` are equivalent. That is, the array declaration is adjusted
|
| 129 |
to become a pointer declaration ([[dcl.fct]]). Only the second and
|
| 130 |
subsequent array dimensions are significant in parameter types (
|
| 131 |
[[dcl.array]]).
|
| 132 |
+
\[*Example 5*:
|
| 133 |
``` cpp
|
| 134 |
int f(char*);
|
| 135 |
int f(char[]); // same as f(char*);
|
| 136 |
int f(char[7]); // same as f(char*);
|
| 137 |
int f(char[9]); // same as f(char*);
|
|
|
|
| 139 |
int g(char(*)[10]);
|
| 140 |
int g(char[5][10]); // same as g(char(*)[10]);
|
| 141 |
int g(char[7][10]); // same as g(char(*)[10]);
|
| 142 |
int g(char(*)[20]); // different from g(char(*)[10]);
|
| 143 |
```
|
| 144 |
+
|
| 145 |
+
— *end example*]
|
| 146 |
- Parameter declarations that differ only in that one is a function type
|
| 147 |
and the other is a pointer to the same function type are equivalent.
|
| 148 |
That is, the function type is adjusted to become a pointer to function
|
| 149 |
type ([[dcl.fct]]).
|
| 150 |
+
\[*Example 6*:
|
| 151 |
``` cpp
|
| 152 |
void h(int());
|
| 153 |
void h(int (*)()); // redeclaration of h(int())
|
| 154 |
void h(int x()) { } // definition of h(int())
|
| 155 |
void h(int (*x)()) { } // ill-formed: redefinition of h(int())
|
| 156 |
```
|
| 157 |
+
|
| 158 |
+
— *end example*]
|
| 159 |
- Parameter declarations that differ only in the presence or absence of
|
| 160 |
`const` and/or `volatile` are equivalent. That is, the `const` and
|
| 161 |
`volatile` type-specifiers for each parameter type are ignored when
|
| 162 |
determining which function is being declared, defined, or called.
|
| 163 |
+
\[*Example 7*:
|
| 164 |
``` cpp
|
| 165 |
typedef const int cInt;
|
| 166 |
|
| 167 |
int f (int);
|
| 168 |
int f (const int); // redeclaration of f(int)
|
| 169 |
+
int f (int) { ... } // definition of f(int)
|
| 170 |
+
int f (cInt) { ... } // error: redefinition of f(int)
|
| 171 |
```
|
| 172 |
|
| 173 |
+
— *end example*]
|
| 174 |
Only the `const` and `volatile` type-specifiers at the outermost level
|
| 175 |
of the parameter type specification are ignored in this fashion;
|
| 176 |
`const` and `volatile` type-specifiers buried within a parameter type
|
| 177 |
specification are significant and can be used to distinguish
|
| 178 |
overloaded function declarations.[^1] In particular, for any type `T`,
|
| 179 |
+
“pointer to `T`”, “pointer to `const` `T`”, and “pointer to `volatile`
|
| 180 |
`T`” are considered distinct parameter types, as are “reference to
|
| 181 |
+
`T`”, “reference to `const` `T`”, and “reference to `volatile` `T`”.
|
| 182 |
- Two parameter declarations that differ only in their default arguments
|
| 183 |
+
are equivalent.
|
| 184 |
+
\[*Example 8*:
|
| 185 |
+
Consider the following:
|
| 186 |
``` cpp
|
| 187 |
void f (int i, int j);
|
| 188 |
void f (int i, int j = 99); // OK: redeclaration of f(int, int)
|
| 189 |
void f (int i = 88, int j); // OK: redeclaration of f(int, int)
|
| 190 |
void f (); // OK: overloaded declaration of f
|
|
|
|
| 194 |
f (1); // OK: call f(int, int)
|
| 195 |
f (); // Error: f(int, int) or f()?
|
| 196 |
}
|
| 197 |
```
|
| 198 |
|
| 199 |
+
— *end example*]
|
| 200 |
+
|
| 201 |
+
— *end note*]
|
| 202 |
+
|
| 203 |
## Declaration matching <a id="over.dcl">[[over.dcl]]</a>
|
| 204 |
|
| 205 |
Two function declarations of the same name refer to the same function if
|
| 206 |
they are in the same scope and have equivalent parameter declarations (
|
| 207 |
[[over.load]]). A function member of a derived class is *not* in the
|
| 208 |
same scope as a function member of the same name in a base class.
|
| 209 |
|
| 210 |
+
[*Example 1*:
|
| 211 |
+
|
| 212 |
``` cpp
|
| 213 |
struct B {
|
| 214 |
int f(int);
|
| 215 |
};
|
| 216 |
|
|
|
|
| 228 |
pd->B::f(1); // OK
|
| 229 |
pd->f("Ben"); // OK, calls D::f
|
| 230 |
}
|
| 231 |
```
|
| 232 |
|
| 233 |
+
— *end example*]
|
| 234 |
+
|
| 235 |
A locally declared function is not in the same scope as a function in a
|
| 236 |
containing scope.
|
| 237 |
|
| 238 |
+
[*Example 2*:
|
| 239 |
+
|
| 240 |
``` cpp
|
| 241 |
void f(const char*);
|
| 242 |
void g() {
|
| 243 |
extern void f(int);
|
| 244 |
f("asdf"); // error: f(int) hides f(const char*)
|
|
|
|
| 252 |
callee(88, 99); // error: only callee(int) in scope
|
| 253 |
}
|
| 254 |
}
|
| 255 |
```
|
| 256 |
|
| 257 |
+
— *end example*]
|
| 258 |
+
|
| 259 |
Different versions of an overloaded member function can be given
|
| 260 |
different access rules.
|
| 261 |
|
| 262 |
+
[*Example 3*:
|
| 263 |
+
|
| 264 |
``` cpp
|
| 265 |
class buffer {
|
| 266 |
private:
|
| 267 |
char* p;
|
| 268 |
int size;
|
|
|
|
| 271 |
public:
|
| 272 |
buffer(int s) { p = new char[size = s]; }
|
| 273 |
};
|
| 274 |
```
|
| 275 |
|
| 276 |
+
— *end example*]
|
| 277 |
+
|
| 278 |
## Overload resolution <a id="over.match">[[over.match]]</a>
|
| 279 |
|
| 280 |
Overload resolution is a mechanism for selecting the best function to
|
| 281 |
call given a list of expressions that are to be the arguments of the
|
| 282 |
call and a set of *candidate functions* that can be called based on the
|
| 283 |
context of the call. The selection criteria for the best function are
|
| 284 |
the number of arguments, how well the arguments match the
|
| 285 |
parameter-type-list of the candidate function, how well (for non-static
|
| 286 |
member functions) the object matches the implicit object parameter, and
|
| 287 |
+
certain other properties of the candidate function.
|
| 288 |
+
|
| 289 |
+
[*Note 1*: The function selected by overload resolution is not
|
| 290 |
+
guaranteed to be appropriate for the context. Other restrictions, such
|
| 291 |
+
as the accessibility of the function, can make its use in the calling
|
| 292 |
+
context ill-formed. — *end note*]
|
| 293 |
|
| 294 |
Overload resolution selects the function to call in seven distinct
|
| 295 |
contexts within the language:
|
| 296 |
|
| 297 |
- invocation of a function named in the function call syntax (
|
|
|
|
| 300 |
conversion function, a reference-to-pointer-to-function conversion
|
| 301 |
function, or a reference-to-function conversion function on a class
|
| 302 |
object named in the function call syntax ([[over.call.object]]);
|
| 303 |
- invocation of the operator referenced in an expression (
|
| 304 |
[[over.match.oper]]);
|
| 305 |
+
- invocation of a constructor for default- or direct-initialization (
|
| 306 |
+
[[dcl.init]]) of a class object ([[over.match.ctor]]);
|
| 307 |
- invocation of a user-defined conversion for copy-initialization (
|
| 308 |
[[dcl.init]]) of a class object ([[over.match.copy]]);
|
| 309 |
- invocation of a conversion function for initialization of an object of
|
| 310 |
+
a non-class type from an expression of class type (
|
| 311 |
[[over.match.conv]]); and
|
| 312 |
- invocation of a conversion function for conversion to a glvalue or
|
| 313 |
class prvalue to which a reference ([[dcl.init.ref]]) will be
|
| 314 |
directly bound ([[over.match.ref]]).
|
| 315 |
|
|
|
|
| 359 |
implied object argument, if present, is always the first argument.
|
| 360 |
|
| 361 |
For non-static member functions, the type of the implicit object
|
| 362 |
parameter is
|
| 363 |
|
| 364 |
+
- “lvalue reference to cv `X`” for functions declared without a
|
| 365 |
*ref-qualifier* or with the `&` *ref-qualifier*
|
| 366 |
+
- “rvalue reference to cv `X`” for functions declared with the `&&`
|
| 367 |
*ref-qualifier*
|
| 368 |
|
| 369 |
+
where `X` is the class of which the function is a member and cv is the
|
| 370 |
+
cv-qualification on the member function declaration.
|
| 371 |
+
|
| 372 |
+
[*Example 1*: For a `const` member function of class `X`, the extra
|
| 373 |
+
parameter is assumed to have type “reference to
|
| 374 |
+
`const X`”. — *end example*]
|
| 375 |
+
|
| 376 |
+
For conversion functions, the function is considered to be a member of
|
| 377 |
+
the class of the implied object argument for the purpose of defining the
|
| 378 |
+
type of the implicit object parameter. For non-conversion functions
|
| 379 |
+
introduced by a *using-declaration* into a derived class, the function
|
| 380 |
+
is considered to be a member of the derived class for the purpose of
|
| 381 |
+
defining the type of the implicit object parameter. For static member
|
| 382 |
+
functions, the implicit object parameter is considered to match any
|
| 383 |
+
object (since if the function is selected, the object is discarded).
|
| 384 |
+
|
| 385 |
+
[*Note 1*: No actual type is established for the implicit object
|
| 386 |
+
parameter of a static member function, and no attempt will be made to
|
| 387 |
+
determine a conversion sequence for that parameter (
|
| 388 |
+
[[over.match.best]]). — *end note*]
|
| 389 |
|
| 390 |
During overload resolution, the implied object argument is
|
| 391 |
indistinguishable from other arguments. The implicit object parameter,
|
| 392 |
+
however, retains its identity since no user-defined conversions can be
|
| 393 |
+
applied to achieve a type match with it. For non-static member functions
|
| 394 |
+
declared without a *ref-qualifier*, an additional rule applies:
|
| 395 |
|
| 396 |
+
- even if the implicit object parameter is not const-qualified, an
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
rvalue can be bound to the parameter as long as in all other respects
|
| 398 |
the argument can be converted to the type of the implicit object
|
| 399 |
+
parameter. \[*Note 2*: The fact that such an argument is an rvalue
|
| 400 |
+
does not affect the ranking of implicit conversion sequences (
|
| 401 |
+
[[over.ics.rank]]). — *end note*]
|
| 402 |
|
| 403 |
Because other than in list-initialization only one user-defined
|
| 404 |
conversion is allowed in an implicit conversion sequence, special rules
|
| 405 |
apply when selecting the best user-defined conversion (
|
| 406 |
[[over.match.best]], [[over.best.ics]]).
|
| 407 |
|
| 408 |
+
[*Example 2*:
|
| 409 |
+
|
| 410 |
``` cpp
|
| 411 |
class T {
|
| 412 |
public:
|
| 413 |
T();
|
| 414 |
};
|
|
|
|
| 418 |
C(int);
|
| 419 |
};
|
| 420 |
T a = 1; // ill-formed: T(C(1)) not tried
|
| 421 |
```
|
| 422 |
|
| 423 |
+
— *end example*]
|
| 424 |
+
|
| 425 |
In each case where a candidate is a function template, candidate
|
| 426 |
function template specializations are generated using template argument
|
| 427 |
deduction ([[temp.over]], [[temp.deduct]]). Those candidates are then
|
| 428 |
handled as candidate functions in the usual way.[^2] A given name can
|
| 429 |
refer to one or more function templates and also to a set of overloaded
|
|
|
|
| 451 |
|
| 452 |
If the *postfix-expression* denotes the address of a set of overloaded
|
| 453 |
functions and/or function templates, overload resolution is applied
|
| 454 |
using that set as described above. If the function selected by overload
|
| 455 |
resolution is a non-static member function, the program is ill-formed.
|
| 456 |
+
|
| 457 |
+
[*Note 1*: The resolution of the address of an overload set in other
|
| 458 |
+
contexts is described in [[over.over]]. — *end note*]
|
| 459 |
|
| 460 |
##### Call to named function <a id="over.call.func">[[over.call.func]]</a>
|
| 461 |
|
| 462 |
Of interest in [[over.call.func]] are only those function calls in
|
| 463 |
which the *postfix-expression* ultimately contains a name that denotes
|
|
|
|
| 480 |
construct `A->B` is generally equivalent to `(*A).B`, the rest of
|
| 481 |
Clause [[over]] assumes, without loss of generality, that all member
|
| 482 |
function calls have been normalized to the form that uses an object and
|
| 483 |
the `.` operator. Furthermore, Clause [[over]] assumes that the
|
| 484 |
*postfix-expression* that is the left operand of the `.` operator has
|
| 485 |
+
type “cv `T`” where `T` denotes a class[^3]. Under this assumption, the
|
| 486 |
+
*id-expression* in the call is looked up as a member function of `T`
|
| 487 |
following the rules for looking up names in classes (
|
| 488 |
[[class.member.lookup]]). The function declarations found by that lookup
|
| 489 |
constitute the set of candidate functions. The argument list is the
|
| 490 |
*expression-list* in the call augmented by the addition of the left
|
| 491 |
operand of the `.` operator in the normalized member function call as
|
|
|
|
| 511 |
of `T`, the call is ill-formed.
|
| 512 |
|
| 513 |
##### Call to object of class type <a id="over.call.object">[[over.call.object]]</a>
|
| 514 |
|
| 515 |
If the *primary-expression* `E` in the function call syntax evaluates to
|
| 516 |
+
a class object of type “cv `T`”, then the set of candidate functions
|
| 517 |
includes at least the function call operators of `T`. The function call
|
| 518 |
operators of `T` are obtained by ordinary lookup of the name
|
| 519 |
`operator()` in the context of `(E).operator()`.
|
| 520 |
|
| 521 |
In addition, for each non-explicit conversion function declared in `T`
|
| 522 |
of the form
|
| 523 |
|
| 524 |
``` bnf
|
| 525 |
+
'operator' conversion-type-id '( )' cv-qualifier ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ ';'
|
| 526 |
```
|
| 527 |
|
| 528 |
where *cv-qualifier* is the same cv-qualification as, or a greater
|
| 529 |
+
cv-qualification than, cv, and where *conversion-type-id* denotes the
|
| 530 |
+
type “pointer to function of (`P₁`, …, `Pₙ`) returning `R`”, or the type
|
| 531 |
+
“reference to pointer to function of (`P₁`, …, `Pₙ`) returning `R`”, or
|
| 532 |
+
the type “reference to function of (`P₁`, …, `Pₙ`) returning `R`”, a
|
| 533 |
*surrogate call function* with the unique name *call-function* and
|
| 534 |
having the form
|
| 535 |
|
| 536 |
``` bnf
|
| 537 |
+
'R' call-function '(' conversion-type-id \ %
|
| 538 |
+
'F, P₁ a₁, …, Pₙ aₙ)' '{ return F (a₁, …, aₙ); }'
|
| 539 |
```
|
| 540 |
|
| 541 |
is also considered as a candidate function. Similarly, surrogate call
|
| 542 |
functions are added to the set of candidate functions for each
|
| 543 |
non-explicit conversion function declared in a base class of `T`
|
|
|
|
| 551 |
function cannot be called (e.g., because of an ambiguity), the program
|
| 552 |
is ill-formed.
|
| 553 |
|
| 554 |
The argument list submitted to overload resolution consists of the
|
| 555 |
argument expressions present in the function call syntax preceded by the
|
| 556 |
+
implied object argument `(E)`.
|
| 557 |
+
|
| 558 |
+
[*Note 2*: When comparing the call against the function call operators,
|
| 559 |
+
the implied object argument is compared against the implicit object
|
| 560 |
+
parameter of the function call operator. When comparing the call against
|
| 561 |
+
a surrogate call function, the implied object argument is compared
|
| 562 |
+
against the first parameter of the surrogate call function. The
|
| 563 |
+
conversion function from which the surrogate call function was derived
|
| 564 |
+
will be used in the conversion sequence for that parameter since it
|
| 565 |
+
converts the implied object argument to the appropriate function pointer
|
| 566 |
+
or reference required by that first parameter. — *end note*]
|
| 567 |
+
|
| 568 |
+
[*Example 1*:
|
| 569 |
|
| 570 |
``` cpp
|
| 571 |
int f1(int);
|
| 572 |
int f2(float);
|
| 573 |
typedef int (*fp1)(int);
|
| 574 |
typedef int (*fp2)(float);
|
| 575 |
struct A {
|
| 576 |
operator fp1() { return f1; }
|
| 577 |
operator fp2() { return f2; }
|
| 578 |
} a;
|
| 579 |
+
int i = a(1); // calls f1 via pointer returned from conversion function
|
|
|
|
| 580 |
```
|
| 581 |
|
| 582 |
+
— *end example*]
|
| 583 |
+
|
| 584 |
#### Operators in expressions <a id="over.match.oper">[[over.match.oper]]</a>
|
| 585 |
|
| 586 |
If no operand of an operator in an expression has a type that is a class
|
| 587 |
or an enumeration, the operator is assumed to be a built-in operator and
|
| 588 |
+
interpreted according to Clause [[expr]].
|
| 589 |
+
|
| 590 |
+
[*Note 1*: Because `.`, `.*`, and `::` cannot be overloaded, these
|
| 591 |
+
operators are always built-in operators interpreted according to Clause
|
| 592 |
+
[[expr]]. `?:` cannot be overloaded, but the rules in this subclause are
|
| 593 |
+
used to determine the conversions to be applied to the second and third
|
| 594 |
+
operands when they have class or enumeration type (
|
| 595 |
+
[[expr.cond]]). — *end note*]
|
| 596 |
+
|
| 597 |
+
[*Example 1*:
|
| 598 |
|
| 599 |
``` cpp
|
| 600 |
struct String {
|
| 601 |
String (const String&);
|
| 602 |
String (const char*);
|
| 603 |
operator const char* ();
|
| 604 |
};
|
| 605 |
String operator + (const String&, const String&);
|
| 606 |
|
| 607 |
+
void f() {
|
| 608 |
+
const char* p= "one" + "two"; // ill-formed because neither operand has class or enumeration type
|
| 609 |
+
int I = 1 + 1; // always evaluates to 2 even if class or enumeration types exist
|
| 610 |
+
// that would perform the operation.
|
|
|
|
|
|
|
| 611 |
}
|
| 612 |
```
|
| 613 |
|
| 614 |
+
— *end example*]
|
| 615 |
+
|
| 616 |
If either operand has a type that is a class or an enumeration, a
|
| 617 |
user-defined operator function might be declared that implements this
|
| 618 |
operator or a user-defined conversion can be necessary to convert the
|
| 619 |
operand to a type that is appropriate for a built-in operator. In this
|
| 620 |
case, overload resolution is used to determine which operator function
|
| 621 |
or built-in operator is to be invoked to implement the operator.
|
| 622 |
Therefore, the operator notation is first transformed to the equivalent
|
| 623 |
function-call notation as summarized in Table [[tab:over.rel.op.func]]
|
| 624 |
(where `@` denotes one of the operators covered in the specified
|
| 625 |
+
subclause). However, the operands are sequenced in the order prescribed
|
| 626 |
+
for the built-in operator (Clause [[expr]]).
|
| 627 |
|
| 628 |
**Table: Relationship between operator and function call notation** <a id="tab:over.rel.op.func">[tab:over.rel.op.func]</a>
|
| 629 |
|
| 630 |
| Subclause | Expression | As member function | As non-member function |
|
| 631 |
+
| ------------ | ---------- | ------------------- | ---------------------- |
|
| 632 |
+
| (a)} |
|
| 633 |
+
| (a, b)} |
|
| 634 |
+
| [[over.ass]] | `a=b` | `(a).operator= (b)` | |
|
| 635 |
+
| [[over.sub]] | `a[b]` | `(a).operator[](b)` | |
|
| 636 |
+
| [[over.ref]] | `a->` | `(a).operator->( )` | |
|
| 637 |
+
| (a, 0)} |
|
| 638 |
|
| 639 |
|
| 640 |
For a unary operator `@` with an operand of a type whose cv-unqualified
|
| 641 |
version is `T1`, and for a binary operator `@` with a left operand of a
|
| 642 |
type whose cv-unqualified version is `T1` and a right operand of a type
|
|
|
|
| 652 |
lookup of `operator@` in the context of the expression according to
|
| 653 |
the usual rules for name lookup in unqualified function calls (
|
| 654 |
[[basic.lookup.argdep]]) except that all member functions are ignored.
|
| 655 |
However, if no operand has a class type, only those non-member
|
| 656 |
functions in the lookup set that have a first parameter of type `T1`
|
| 657 |
+
or “reference to cv `T1`”, when `T1` is an enumeration type, or (if
|
| 658 |
+
there is a right operand) a second parameter of type `T2` or
|
| 659 |
+
“reference to cv `T2`”, when `T2` is an enumeration type, are
|
| 660 |
+
candidate functions.
|
| 661 |
- For the operator `,`, the unary operator `&`, or the operator `->`,
|
| 662 |
the built-in candidates set is empty. For all other operators, the
|
| 663 |
built-in candidates include all of the candidate operator functions
|
| 664 |
defined in [[over.built]] that, compared to the given operator,
|
| 665 |
- have the same operator name, and
|
|
|
|
| 683 |
candidates. The argument list contains all of the operands of the
|
| 684 |
operator. The best function from the set of candidate functions is
|
| 685 |
selected according to [[over.match.viable]] and
|
| 686 |
[[over.match.best]].[^6]
|
| 687 |
|
| 688 |
+
[*Example 2*:
|
| 689 |
+
|
| 690 |
``` cpp
|
| 691 |
struct A {
|
| 692 |
operator int();
|
| 693 |
};
|
| 694 |
A operator+(const A&, const A&);
|
|
|
|
| 696 |
A a, b;
|
| 697 |
a + b; // operator+(a, b) chosen over int(a) + int(b)
|
| 698 |
}
|
| 699 |
```
|
| 700 |
|
| 701 |
+
— *end example*]
|
| 702 |
+
|
| 703 |
If a built-in candidate is selected by overload resolution, the operands
|
| 704 |
of class type are converted to the types of the corresponding parameters
|
| 705 |
of the selected operation function, except that the second standard
|
| 706 |
conversion sequence of a user-defined conversion sequence (
|
| 707 |
[[over.ics.user]]) is not applied. Then the operator is treated as the
|
| 708 |
corresponding built-in operator and interpreted according to Clause
|
| 709 |
[[expr]].
|
| 710 |
|
| 711 |
+
[*Example 3*:
|
| 712 |
+
|
| 713 |
``` cpp
|
| 714 |
struct X {
|
| 715 |
operator double();
|
| 716 |
};
|
| 717 |
|
|
|
|
| 721 |
|
| 722 |
int *a = Y() + 100.0; // error: pointer arithmetic requires integral operand
|
| 723 |
int *b = Y() + X(); // error: pointer arithmetic requires integral operand
|
| 724 |
```
|
| 725 |
|
| 726 |
+
— *end example*]
|
| 727 |
+
|
| 728 |
The second operand of operator `->` is ignored in selecting an
|
| 729 |
`operator->` function, and is not an argument when the `operator->`
|
| 730 |
function is called. When `operator->` returns, the operator `->` is
|
| 731 |
applied to the value returned, with the original second operand.[^7]
|
| 732 |
|
| 733 |
If the operator is the operator `,`, the unary operator `&`, or the
|
| 734 |
operator `->`, and there are no viable functions, then the operator is
|
| 735 |
assumed to be the built-in operator and interpreted according to Clause
|
| 736 |
[[expr]].
|
| 737 |
|
| 738 |
+
[*Note 2*:
|
| 739 |
+
|
| 740 |
The lookup rules for operators in expressions are different than the
|
| 741 |
lookup rules for operator function names in a function call, as shown in
|
| 742 |
the following example:
|
| 743 |
|
| 744 |
``` cpp
|
|
|
|
| 756 |
operator+ (a,a); // error: global operator hidden by member
|
| 757 |
a + a; // OK: calls global operator+
|
| 758 |
}
|
| 759 |
```
|
| 760 |
|
| 761 |
+
— *end note*]
|
| 762 |
+
|
| 763 |
#### Initialization by constructor <a id="over.match.ctor">[[over.match.ctor]]</a>
|
| 764 |
|
| 765 |
+
When objects of class type are direct-initialized ([[dcl.init]]),
|
| 766 |
copy-initialized from an expression of the same or a derived class
|
| 767 |
+
type ([[dcl.init]]), or default-initialized ([[dcl.init]]), overload
|
| 768 |
+
resolution selects the constructor. For direct-initialization or
|
| 769 |
+
default-initialization that is not in the context of
|
| 770 |
+
copy-initialization, the candidate functions are all the constructors of
|
| 771 |
+
the class of the object being initialized. For copy-initialization, the
|
| 772 |
+
candidate functions are all the converting constructors (
|
| 773 |
[[class.conv.ctor]]) of that class. The argument list is the
|
| 774 |
*expression-list* or *assignment-expression* of the *initializer*.
|
| 775 |
|
| 776 |
#### Copy-initialization of class by user-defined conversion <a id="over.match.copy">[[over.match.copy]]</a>
|
| 777 |
|
| 778 |
Under the conditions specified in [[dcl.init]], as part of a
|
| 779 |
copy-initialization of an object of class type, a user-defined
|
| 780 |
conversion can be invoked to convert an initializer expression to the
|
| 781 |
type of the object being initialized. Overload resolution is used to
|
| 782 |
+
select the user-defined conversion to be invoked.
|
| 783 |
+
|
| 784 |
+
[*Note 1*: The conversion performed for indirect binding to a reference
|
| 785 |
+
to a possibly cv-qualified class type is determined in terms of a
|
| 786 |
+
corresponding non-reference copy-initialization. — *end note*]
|
| 787 |
+
|
| 788 |
+
Assuming that “*cv1* `T`” is the type of the object being initialized,
|
| 789 |
+
with `T` a class type, the candidate functions are selected as follows:
|
| 790 |
|
| 791 |
- The converting constructors ([[class.conv.ctor]]) of `T` are
|
| 792 |
candidate functions.
|
| 793 |
+
- When the type of the initializer expression is a class type “cv `S`”,
|
| 794 |
+
the non-explicit conversion functions of `S` and its base classes are
|
| 795 |
+
considered. When initializing a temporary to be bound to the first
|
| 796 |
+
parameter of a constructor where the parameter is of type “reference
|
| 797 |
+
to possibly cv-qualified `T`” and the constructor is called with a
|
| 798 |
+
single argument in the context of direct-initialization of an object
|
| 799 |
+
of type “*cv2* `T`”, explicit conversion functions are also
|
| 800 |
+
considered. Those that are not hidden within `S` and yield a type
|
| 801 |
+
whose cv-unqualified version is the same type as `T` or is a derived
|
| 802 |
+
class thereof are candidate functions. Conversion functions that
|
| 803 |
+
return “reference to `X`” return lvalues or xvalues, depending on the
|
| 804 |
+
type of reference, of type `X` and are therefore considered to yield
|
| 805 |
+
`X` for this process of selecting candidate functions.
|
| 806 |
|
| 807 |
In both cases, the argument list has one argument, which is the
|
| 808 |
+
initializer expression.
|
| 809 |
+
|
| 810 |
+
[*Note 2*: This argument will be compared against the first parameter
|
| 811 |
+
of the constructors and against the implicit object parameter of the
|
| 812 |
+
conversion functions. — *end note*]
|
| 813 |
|
| 814 |
#### Initialization by conversion function <a id="over.match.conv">[[over.match.conv]]</a>
|
| 815 |
|
| 816 |
Under the conditions specified in [[dcl.init]], as part of an
|
| 817 |
+
initialization of an object of non-class type, a conversion function can
|
| 818 |
be invoked to convert an initializer expression of class type to the
|
| 819 |
type of the object being initialized. Overload resolution is used to
|
| 820 |
select the conversion function to be invoked. Assuming that “*cv1* `T`”
|
| 821 |
+
is the type of the object being initialized, and “cv `S`” is the type of
|
| 822 |
+
the initializer expression, with `S` a class type, the candidate
|
| 823 |
functions are selected as follows:
|
| 824 |
|
| 825 |
- The conversion functions of `S` and its base classes are considered.
|
| 826 |
Those non-explicit conversion functions that are not hidden within `S`
|
| 827 |
and yield type `T` or a type that can be converted to type `T` via a
|
|
|
|
| 836 |
return lvalues or xvalues, depending on the type of reference, of type
|
| 837 |
“*cv2* `X`” and are therefore considered to yield `X` for this process
|
| 838 |
of selecting candidate functions.
|
| 839 |
|
| 840 |
The argument list has one argument, which is the initializer expression.
|
| 841 |
+
|
| 842 |
+
[*Note 1*: This argument will be compared against the implicit object
|
| 843 |
+
parameter of the conversion functions. — *end note*]
|
| 844 |
|
| 845 |
#### Initialization by conversion function for direct reference binding <a id="over.match.ref">[[over.match.ref]]</a>
|
| 846 |
|
| 847 |
Under the conditions specified in [[dcl.init.ref]], a reference can be
|
| 848 |
bound directly to a glvalue or class prvalue that is the result of
|
| 849 |
applying a conversion function to an initializer expression. Overload
|
| 850 |
resolution is used to select the conversion function to be invoked.
|
| 851 |
+
Assuming that “reference to *cv1* `T`” is the type of the reference
|
| 852 |
+
being initialized, and “cv `S`” is the type of the initializer
|
| 853 |
+
expression, with `S` a class type, the candidate functions are selected
|
| 854 |
+
as follows:
|
| 855 |
|
| 856 |
- The conversion functions of `S` and its base classes are considered.
|
| 857 |
Those non-explicit conversion functions that are not hidden within `S`
|
| 858 |
and yield type “lvalue reference to *cv2* `T2`” (when initializing an
|
| 859 |
+
lvalue reference or an rvalue reference to function) or “*cv2* `T2`”
|
| 860 |
+
or “rvalue reference to *cv2* `T2`” (when initializing an rvalue
|
| 861 |
+
reference or an lvalue reference to function), where “*cv1* `T`” is
|
| 862 |
reference-compatible ([[dcl.init.ref]]) with “*cv2* `T2`”, are
|
| 863 |
candidate functions. For direct-initialization, those explicit
|
| 864 |
conversion functions that are not hidden within `S` and yield type
|
| 865 |
“lvalue reference to *cv2* `T2`” or “*cv2* `T2`” or “rvalue reference
|
| 866 |
+
to *cv2* `T2`”, respectively, where `T2` is the same type as `T` or
|
| 867 |
can be converted to type `T` with a qualification conversion (
|
| 868 |
[[conv.qual]]), are also candidate functions.
|
| 869 |
|
| 870 |
The argument list has one argument, which is the initializer expression.
|
| 871 |
+
|
| 872 |
+
[*Note 1*: This argument will be compared against the implicit object
|
| 873 |
+
parameter of the conversion functions. — *end note*]
|
| 874 |
|
| 875 |
#### Initialization by list-initialization <a id="over.match.list">[[over.match.list]]</a>
|
| 876 |
|
| 877 |
+
When objects of non-aggregate class type `T` are list-initialized such
|
| 878 |
+
that [[dcl.init.list]] specifies that overload resolution is performed
|
| 879 |
+
according to the rules in this section, overload resolution selects the
|
| 880 |
+
constructor in two phases:
|
| 881 |
|
| 882 |
- Initially, the candidate functions are the initializer-list
|
| 883 |
constructors ([[dcl.init.list]]) of the class `T` and the argument
|
| 884 |
list consists of the initializer list as a single argument.
|
| 885 |
- If no viable initializer-list constructor is found, overload
|
|
|
|
| 888 |
the elements of the initializer list.
|
| 889 |
|
| 890 |
If the initializer list has no elements and `T` has a default
|
| 891 |
constructor, the first phase is omitted. In copy-list-initialization, if
|
| 892 |
an `explicit` constructor is chosen, the initialization is ill-formed.
|
| 893 |
+
|
| 894 |
+
[*Note 1*: This differs from other situations ([[over.match.ctor]],
|
| 895 |
[[over.match.copy]]), where only converting constructors are considered
|
| 896 |
for copy-initialization. This restriction only applies if this
|
| 897 |
+
initialization is part of the final result of overload
|
| 898 |
+
resolution. — *end note*]
|
| 899 |
+
|
| 900 |
+
#### Class template argument deduction <a id="over.match.class.deduct">[[over.match.class.deduct]]</a>
|
| 901 |
+
|
| 902 |
+
A set of functions and function templates is formed comprising:
|
| 903 |
+
|
| 904 |
+
- For each constructor of the primary class template designated by the
|
| 905 |
+
*template-name*, if the template is defined, a function template with
|
| 906 |
+
the following properties:
|
| 907 |
+
- The template parameters are the template parameters of the class
|
| 908 |
+
template followed by the template parameters (including default
|
| 909 |
+
template arguments) of the constructor, if any.
|
| 910 |
+
- The types of the function parameters are those of the constructor.
|
| 911 |
+
- The return type is the class template specialization designated by
|
| 912 |
+
the *template-name* and template arguments corresponding to the
|
| 913 |
+
template parameters obtained from the class template.
|
| 914 |
+
- If the primary class template `C` is not defined or does not declare
|
| 915 |
+
any constructors, an additional function template derived as above
|
| 916 |
+
from a hypothetical constructor `C()`.
|
| 917 |
+
- An additional function template derived as above from a hypothetical
|
| 918 |
+
constructor `C(C)`, called the *copy deduction candidate*.
|
| 919 |
+
- For each *deduction-guide*, a function or function template with the
|
| 920 |
+
following properties:
|
| 921 |
+
- The template parameters, if any, and function parameters are those
|
| 922 |
+
of the *deduction-guide*.
|
| 923 |
+
- The return type is the *simple-template-id* of the
|
| 924 |
+
*deduction-guide*.
|
| 925 |
+
|
| 926 |
+
Initialization and overload resolution are performed as described in
|
| 927 |
+
[[dcl.init]] and [[over.match.ctor]], [[over.match.copy]], or
|
| 928 |
+
[[over.match.list]] (as appropriate for the type of initialization
|
| 929 |
+
performed) for an object of a hypothetical class type, where the
|
| 930 |
+
selected functions and function templates are considered to be the
|
| 931 |
+
constructors of that class type for the purpose of forming an overload
|
| 932 |
+
set, and the initializer is provided by the context in which class
|
| 933 |
+
template argument deduction was performed. Each such notional
|
| 934 |
+
constructor is considered to be explicit if the function or function
|
| 935 |
+
template was generated from a constructor or *deduction-guide* that was
|
| 936 |
+
declared `explicit`. All such notional constructors are considered to be
|
| 937 |
+
public members of the hypothetical class type.
|
| 938 |
+
|
| 939 |
+
[*Example 1*:
|
| 940 |
+
|
| 941 |
+
``` cpp
|
| 942 |
+
template <class T> struct A {
|
| 943 |
+
explicit A(const T&, ...) noexcept; // #1
|
| 944 |
+
A(T&&, ...); // #2
|
| 945 |
+
};
|
| 946 |
+
|
| 947 |
+
int i;
|
| 948 |
+
A a1 = { i, i }; // error: explicit constructor #1 selected in copy-list-initialization during deduction,
|
| 949 |
+
// cannot deduce from non-forwarding rvalue reference in #2
|
| 950 |
+
|
| 951 |
+
A a2{i, i}; // OK, #1 deduces to A<int> and also initializes
|
| 952 |
+
A a3{0, i}; // OK, #2 deduces to A<int> and also initializes
|
| 953 |
+
A a4 = {0, i}; // OK, #2 deduces to A<int> and also initializes
|
| 954 |
+
|
| 955 |
+
template <class T> A(const T&, const T&) -> A<T&>; // #3
|
| 956 |
+
template <class T> explicit A(T&&, T&&) -> A<T>; // #4
|
| 957 |
+
|
| 958 |
+
A a5 = {0, 1}; // error: explicit deduction guide #4 selected in copy-list-initialization during deduction
|
| 959 |
+
A a6{0,1}; // OK, #4 deduces to A<int> and #2 initializes
|
| 960 |
+
A a7 = {0, i}; // error: #3 deduces to A<int&>, #1 and #2 declare same constructor
|
| 961 |
+
A a8{0,i}; // error: #3 deduces to A<int&>, #1 and #2 declare same constructor
|
| 962 |
+
|
| 963 |
+
template <class T> struct B {
|
| 964 |
+
template <class U> using TA = T;
|
| 965 |
+
template <class U> B(U, TA<U>);
|
| 966 |
+
};
|
| 967 |
+
|
| 968 |
+
B b{(int*)0, (char*)0}; // OK, deduces B<char*>
|
| 969 |
+
```
|
| 970 |
+
|
| 971 |
+
— *end example*]
|
| 972 |
|
| 973 |
### Viable functions <a id="over.match.viable">[[over.match.viable]]</a>
|
| 974 |
|
| 975 |
From the set of candidate functions constructed for a given context (
|
| 976 |
[[over.match.funcs]]), a set of viable functions is chosen, from which
|
|
|
|
| 1006 |
|
| 1007 |
### Best viable function <a id="over.match.best">[[over.match.best]]</a>
|
| 1008 |
|
| 1009 |
Define ICS*i*(`F`) as follows:
|
| 1010 |
|
| 1011 |
+
- If `F` is a static member function, ICS*1*(`F`) is defined such that
|
| 1012 |
ICS*1*(`F`) is neither better nor worse than ICS*1*(`G`) for any
|
| 1013 |
function `G`, and, symmetrically, ICS*1*(`G`) is neither better nor
|
| 1014 |
+
worse than ICS*1*(`F`);[^9] otherwise,
|
| 1015 |
- let ICS*i*(`F`) denote the implicit conversion sequence that converts
|
| 1016 |
the *i*-th argument in the list to the type of the *i*-th parameter of
|
| 1017 |
viable function `F`. [[over.best.ics]] defines the implicit conversion
|
| 1018 |
sequences and [[over.ics.rank]] defines what it means for one implicit
|
| 1019 |
conversion sequence to be a better conversion sequence or worse
|
|
|
|
| 1029 |
- the context is an initialization by user-defined conversion (see
|
| 1030 |
[[dcl.init]], [[over.match.conv]], and [[over.match.ref]]) and the
|
| 1031 |
standard conversion sequence from the return type of `F1` to the
|
| 1032 |
destination type (i.e., the type of the entity being initialized) is a
|
| 1033 |
better conversion sequence than the standard conversion sequence from
|
| 1034 |
+
the return type of `F2` to the destination type
|
| 1035 |
+
\[*Example 1*:
|
| 1036 |
``` cpp
|
| 1037 |
struct A {
|
| 1038 |
A();
|
| 1039 |
operator int();
|
| 1040 |
operator double();
|
| 1041 |
} a;
|
| 1042 |
+
int i = a; // a.operator int() followed by no conversion is better than
|
| 1043 |
+
// a.operator double() followed by a conversion to int
|
|
|
|
| 1044 |
float x = a; // ambiguous: both possibilities require conversions,
|
| 1045 |
// and neither is better than the other
|
| 1046 |
```
|
| 1047 |
|
| 1048 |
+
— *end example*]
|
| 1049 |
or, if not that,
|
| 1050 |
- the context is an initialization by conversion function for direct
|
| 1051 |
reference binding ([[over.match.ref]]) of a reference to function
|
| 1052 |
type, the return type of `F1` is the same kind of reference (i.e.
|
| 1053 |
lvalue or rvalue) as the reference being initialized, and the return
|
| 1054 |
type of `F2` is not
|
| 1055 |
+
\[*Example 2*:
|
| 1056 |
``` cpp
|
| 1057 |
template <class T> struct A {
|
| 1058 |
operator T&(); // #1
|
| 1059 |
operator T&&(); // #2
|
| 1060 |
};
|
|
|
|
| 1062 |
A<Fn> a;
|
| 1063 |
Fn& lf = a; // calls #1
|
| 1064 |
Fn&& rf = a; // calls #2
|
| 1065 |
```
|
| 1066 |
|
| 1067 |
+
— *end example*]
|
| 1068 |
or, if not that,
|
| 1069 |
- `F1` is not a function template specialization and `F2` is a function
|
| 1070 |
template specialization, or, if not that,
|
| 1071 |
- `F1` and `F2` are function template specializations, and the function
|
| 1072 |
template for `F1` is more specialized than the template for `F2`
|
| 1073 |
according to the partial ordering rules described in
|
| 1074 |
+
[[temp.func.order]], or, if not that,
|
| 1075 |
+
- `F1` is generated from a *deduction-guide* (
|
| 1076 |
+
[[over.match.class.deduct]]) and `F2` is not, or, if not that,
|
| 1077 |
+
- `F1` is the copy deduction candidate ([[over.match.class.deduct]])
|
| 1078 |
+
and `F2` is not, or, if not that,
|
| 1079 |
+
- `F1` is generated from a non-template constructor and `F2` is
|
| 1080 |
+
generated from a constructor template.
|
| 1081 |
+
\[*Example 3*:
|
| 1082 |
+
``` cpp
|
| 1083 |
+
template <class T> struct A {
|
| 1084 |
+
using value_type = T;
|
| 1085 |
+
A(value_type); // #1
|
| 1086 |
+
A(const A&); // #2
|
| 1087 |
+
A(T, T, int); // #3
|
| 1088 |
+
template<class U>
|
| 1089 |
+
A(int, T, U); // #4
|
| 1090 |
+
// #5 is the copy deduction candidate, A(A)
|
| 1091 |
+
};
|
| 1092 |
+
|
| 1093 |
+
A x(1, 2, 3); // uses #3, generated from a non-template constructor
|
| 1094 |
+
|
| 1095 |
+
template <class T>
|
| 1096 |
+
A(T) -> A<T>; // #6, less specialized than #5
|
| 1097 |
+
|
| 1098 |
+
A a(42); // uses #6 to deduce A<int> and #1 to initialize
|
| 1099 |
+
A b = a; // uses #5 to deduce A<int> and #2 to initialize
|
| 1100 |
+
|
| 1101 |
+
template <class T>
|
| 1102 |
+
A(A<T>) -> A<A<T>>; // #7, as specialized as #5
|
| 1103 |
+
|
| 1104 |
+
A b2 = a; // uses #7 to deduce A<A<int>> and #1 to initialize
|
| 1105 |
+
```
|
| 1106 |
+
|
| 1107 |
+
— *end example*]
|
| 1108 |
|
| 1109 |
If there is exactly one viable function that is a better function than
|
| 1110 |
all other viable functions, then it is the one selected by overload
|
| 1111 |
+
resolution; otherwise the call is ill-formed.[^10]
|
| 1112 |
+
|
| 1113 |
+
[*Example 4*:
|
| 1114 |
|
| 1115 |
``` cpp
|
| 1116 |
void Fcn(const int*, short);
|
| 1117 |
void Fcn(int*, int);
|
| 1118 |
|
| 1119 |
int i;
|
| 1120 |
short s = 0;
|
| 1121 |
|
| 1122 |
void f() {
|
| 1123 |
+
Fcn(&i, s); // is ambiguous because &i → int* is better than &i → const int*
|
|
|
|
| 1124 |
// but s → short is also better than s → int
|
| 1125 |
|
| 1126 |
+
Fcn(&i, 1L); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
|
|
|
| 1127 |
// and 1L → short and 1L → int are indistinguishable
|
| 1128 |
|
| 1129 |
+
Fcn(&i, 'c'); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
|
|
|
| 1130 |
// and c → int is better than c → short
|
| 1131 |
}
|
| 1132 |
```
|
| 1133 |
|
| 1134 |
+
— *end example*]
|
| 1135 |
+
|
| 1136 |
If the best viable function resolves to a function for which multiple
|
| 1137 |
declarations were found, and if at least two of these declarations — or
|
| 1138 |
the declarations they refer to in the case of *using-declaration*s —
|
| 1139 |
specify a default argument that made the function viable, the program is
|
| 1140 |
ill-formed.
|
| 1141 |
|
| 1142 |
+
[*Example 5*:
|
| 1143 |
+
|
| 1144 |
``` cpp
|
| 1145 |
namespace A {
|
| 1146 |
extern "C" void f(int = 5);
|
| 1147 |
}
|
| 1148 |
namespace B {
|
|
|
|
| 1156 |
f(3); // OK, default argument was not used for viability
|
| 1157 |
f(); // Error: found default argument twice
|
| 1158 |
}
|
| 1159 |
```
|
| 1160 |
|
| 1161 |
+
— *end example*]
|
| 1162 |
+
|
| 1163 |
#### Implicit conversion sequences <a id="over.best.ics">[[over.best.ics]]</a>
|
| 1164 |
|
| 1165 |
An *implicit conversion sequence* is a sequence of conversions used to
|
| 1166 |
convert an argument in a function call to the type of the corresponding
|
| 1167 |
parameter of the function being called. The sequence of conversions is
|
|
|
|
| 1170 |
single expression ([[dcl.init]], [[dcl.init.ref]]).
|
| 1171 |
|
| 1172 |
Implicit conversion sequences are concerned only with the type,
|
| 1173 |
cv-qualification, and value category of the argument and how these are
|
| 1174 |
converted to match the corresponding properties of the parameter. Other
|
| 1175 |
+
properties, such as the lifetime, storage class, alignment,
|
| 1176 |
+
accessibility of the argument, whether the argument is a bit-field, and
|
| 1177 |
+
whether a function is deleted ([[dcl.fct.def.delete]]), are ignored.
|
| 1178 |
+
So, although an implicit conversion sequence can be defined for a given
|
| 1179 |
+
argument-parameter pair, the conversion from the argument to the
|
| 1180 |
+
parameter might still be ill-formed in the final analysis.
|
| 1181 |
|
| 1182 |
A well-formed implicit conversion sequence is one of the following
|
| 1183 |
forms:
|
| 1184 |
|
| 1185 |
- a *standard conversion sequence* ([[over.ics.scs]]),
|
|
|
|
| 1197 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
| 1198 |
step of a class copy-initialization,
|
| 1199 |
- [[over.match.copy]], [[over.match.conv]], or [[over.match.ref]] (in
|
| 1200 |
all cases), or
|
| 1201 |
- the second phase of [[over.match.list]] when the initializer list has
|
| 1202 |
+
exactly one element that is itself an initializer list, and the target
|
| 1203 |
+
is the first parameter of a constructor of class `X`, and the
|
| 1204 |
+
conversion is to `X` or reference to cv `X`,
|
| 1205 |
|
| 1206 |
+
user-defined conversion sequences are not considered.
|
| 1207 |
+
|
| 1208 |
+
[*Note 1*: These rules prevent more than one user-defined conversion
|
| 1209 |
+
from being applied during overload resolution, thereby avoiding infinite
|
| 1210 |
+
recursion. — *end note*]
|
| 1211 |
+
|
| 1212 |
+
[*Example 1*:
|
| 1213 |
|
| 1214 |
``` cpp
|
| 1215 |
struct Y { Y(int); };
|
| 1216 |
struct A { operator int(); };
|
| 1217 |
Y y1 = A(); // error: A::operator int() is not a candidate
|
|
|
|
| 1220 |
struct B { operator X(); };
|
| 1221 |
B b;
|
| 1222 |
X x({b}); // error: B::operator X() is not a candidate
|
| 1223 |
```
|
| 1224 |
|
| 1225 |
+
— *end example*]
|
| 1226 |
+
|
| 1227 |
For the case where the parameter type is a reference, see
|
| 1228 |
[[over.ics.ref]].
|
| 1229 |
|
| 1230 |
When the parameter type is not a reference, the implicit conversion
|
| 1231 |
sequence models a copy-initialization of the parameter from the argument
|
| 1232 |
expression. The implicit conversion sequence is the one required to
|
| 1233 |
convert the argument expression to a prvalue of the type of the
|
| 1234 |
+
parameter.
|
| 1235 |
+
|
| 1236 |
+
[*Note 2*: When the parameter has a class type, this is a conceptual
|
| 1237 |
conversion defined for the purposes of Clause [[over]]; the actual
|
| 1238 |
initialization is defined in terms of constructors and is not a
|
| 1239 |
+
conversion. — *end note*]
|
| 1240 |
+
|
| 1241 |
+
Any difference in top-level cv-qualification is subsumed by the
|
| 1242 |
+
initialization itself and does not constitute a conversion.
|
| 1243 |
+
|
| 1244 |
+
[*Example 2*: A parameter of type `A` can be initialized from an
|
| 1245 |
+
argument of type `const A`. The implicit conversion sequence for that
|
| 1246 |
+
case is the identity sequence; it contains no “conversion” from
|
| 1247 |
+
`const A` to `A`. — *end example*]
|
| 1248 |
+
|
| 1249 |
When the parameter has a class type and the argument expression has the
|
| 1250 |
same type, the implicit conversion sequence is an identity conversion.
|
| 1251 |
When the parameter has a class type and the argument expression has a
|
| 1252 |
derived class type, the implicit conversion sequence is a
|
| 1253 |
derived-to-base Conversion from the derived class to the base class.
|
| 1254 |
+
|
| 1255 |
+
[*Note 3*: There is no such standard conversion; this derived-to-base
|
| 1256 |
+
Conversion exists only in the description of implicit conversion
|
| 1257 |
+
sequences. — *end note*]
|
| 1258 |
+
|
| 1259 |
+
A derived-to-base Conversion has Conversion rank ([[over.ics.scs]]).
|
| 1260 |
|
| 1261 |
In all contexts, when converting to the implicit object parameter or
|
| 1262 |
when converting to the left operand of an assignment operation only
|
| 1263 |
+
standard conversion sequences are allowed.
|
|
|
|
| 1264 |
|
| 1265 |
If no conversions are required to match an argument to a parameter type,
|
| 1266 |
the implicit conversion sequence is the standard conversion sequence
|
| 1267 |
consisting of the identity conversion ([[over.ics.scs]]).
|
| 1268 |
|
| 1269 |
If no sequence of conversions can be found to convert an argument to a
|
| 1270 |
+
parameter type, an implicit conversion sequence cannot be formed.
|
|
|
|
| 1271 |
|
| 1272 |
If several different sequences of conversions exist that each convert
|
| 1273 |
the argument to the parameter type, the implicit conversion sequence
|
| 1274 |
associated with the parameter is defined to be the unique conversion
|
| 1275 |
sequence designated the *ambiguous conversion sequence*. For the purpose
|
| 1276 |
of ranking implicit conversion sequences as described in
|
| 1277 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 1278 |
+
user-defined conversion sequence that is indistinguishable from any
|
| 1279 |
+
other user-defined conversion sequence.
|
| 1280 |
+
|
| 1281 |
+
[*Note 4*:
|
| 1282 |
+
|
| 1283 |
+
This rule prevents a function from becoming non-viable because of an
|
| 1284 |
+
ambiguous conversion sequence for one of its parameters.
|
| 1285 |
+
|
| 1286 |
+
[*Example 3*:
|
| 1287 |
+
|
| 1288 |
+
``` cpp
|
| 1289 |
+
class B;
|
| 1290 |
+
class A { A (B&);};
|
| 1291 |
+
class B { operator A (); };
|
| 1292 |
+
class C { C (B&); };
|
| 1293 |
+
void f(A) { }
|
| 1294 |
+
void f(C) { }
|
| 1295 |
+
B b;
|
| 1296 |
+
f(b); // ill-formed: ambiguous because there is a conversion b → C (via constructor)
|
| 1297 |
+
// and an (ambiguous) conversion b → A (via constructor or conversion function)
|
| 1298 |
+
void f(B) { }
|
| 1299 |
+
f(b); // OK, unambiguous
|
| 1300 |
+
```
|
| 1301 |
+
|
| 1302 |
+
— *end example*]
|
| 1303 |
+
|
| 1304 |
+
— *end note*]
|
| 1305 |
+
|
| 1306 |
+
If a function that uses the ambiguous conversion sequence is selected as
|
| 1307 |
+
the best viable function, the call will be ill-formed because the
|
| 1308 |
+
conversion of one of the arguments in the call is ambiguous.
|
| 1309 |
|
| 1310 |
The three forms of implicit conversion sequences mentioned above are
|
| 1311 |
defined in the following subclauses.
|
| 1312 |
|
| 1313 |
##### Standard conversion sequences <a id="over.ics.scs">[[over.ics.scs]]</a>
|
| 1314 |
|
| 1315 |
Table [[tab:over.conversions]] summarizes the conversions defined in
|
| 1316 |
Clause [[conv]] and partitions them into four disjoint categories:
|
| 1317 |
Lvalue Transformation, Qualification Adjustment, Promotion, and
|
| 1318 |
+
Conversion.
|
| 1319 |
+
|
| 1320 |
+
[*Note 5*: These categories are orthogonal with respect to value
|
| 1321 |
category, cv-qualification, and data representation: the Lvalue
|
| 1322 |
Transformations do not change the cv-qualification or data
|
| 1323 |
representation of the type; the Qualification Adjustments do not change
|
| 1324 |
the value category or data representation of the type; and the
|
| 1325 |
Promotions and Conversions do not change the value category or
|
| 1326 |
+
cv-qualification of the type. — *end note*]
|
| 1327 |
|
| 1328 |
+
[*Note 6*: As described in Clause [[conv]], a standard conversion
|
| 1329 |
+
sequence is either the Identity conversion by itself (that is, no
|
| 1330 |
+
conversion) or consists of one to three conversions from the other four
|
| 1331 |
+
categories. If there are two or more conversions in the sequence, the
|
| 1332 |
+
conversions are applied in the canonical order: **Lvalue
|
|
|
|
| 1333 |
Transformation**, **Promotion** or **Conversion**, **Qualification
|
| 1334 |
+
Adjustment**. — *end note*]
|
| 1335 |
|
| 1336 |
Each conversion in Table [[tab:over.conversions]] also has an
|
| 1337 |
associated rank (Exact Match, Promotion, or Conversion). These are used
|
| 1338 |
to rank standard conversion sequences ([[over.ics.rank]]). The rank of
|
| 1339 |
a conversion sequence is determined by considering the rank of each
|
|
|
|
| 1394 |
an argument expression, the implicit conversion sequence is the identity
|
| 1395 |
conversion, unless the argument expression has a type that is a derived
|
| 1396 |
class of the parameter type, in which case the implicit conversion
|
| 1397 |
sequence is a derived-to-base Conversion ([[over.best.ics]]).
|
| 1398 |
|
| 1399 |
+
[*Example 4*:
|
| 1400 |
+
|
| 1401 |
``` cpp
|
| 1402 |
struct A {};
|
| 1403 |
struct B : public A {} b;
|
| 1404 |
int f(A&);
|
| 1405 |
int f(B&);
|
| 1406 |
+
int i = f(b); // calls f(B&), an exact match, rather than f(A&), a conversion
|
|
|
|
| 1407 |
```
|
| 1408 |
|
| 1409 |
+
— *end example*]
|
| 1410 |
+
|
| 1411 |
If the parameter binds directly to the result of applying a conversion
|
| 1412 |
function to the argument expression, the implicit conversion sequence is
|
| 1413 |
a user-defined conversion sequence ([[over.ics.user]]), with the second
|
| 1414 |
standard conversion sequence either an identity conversion or, if the
|
| 1415 |
conversion function returns an entity of a type that is a derived class
|
| 1416 |
of the parameter type, a derived-to-base Conversion.
|
| 1417 |
|
| 1418 |
When a parameter of reference type is not bound directly to an argument
|
| 1419 |
expression, the conversion sequence is the one required to convert the
|
| 1420 |
+
argument expression to the referenced type according to
|
| 1421 |
+
[[over.best.ics]]. Conceptually, this conversion sequence corresponds to
|
| 1422 |
+
copy-initializing a temporary of the referenced type with the argument
|
| 1423 |
+
expression. Any difference in top-level cv-qualification is subsumed by
|
| 1424 |
+
the initialization itself and does not constitute a conversion.
|
|
|
|
| 1425 |
|
| 1426 |
Except for an implicit object parameter, for which see
|
| 1427 |
[[over.match.funcs]], a standard conversion sequence cannot be formed if
|
| 1428 |
it requires binding an lvalue reference other than a reference to a
|
| 1429 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 1430 |
+
an lvalue other than a function lvalue.
|
| 1431 |
+
|
| 1432 |
+
[*Note 7*: This means, for example, that a candidate function cannot be
|
| 1433 |
+
a viable function if it has a non-`const` lvalue reference parameter
|
| 1434 |
+
(other than the implicit object parameter) and the corresponding
|
| 1435 |
+
argument would require a temporary to be created to initialize the
|
| 1436 |
+
lvalue reference (see [[dcl.init.ref]]). — *end note*]
|
| 1437 |
|
| 1438 |
Other restrictions on binding a reference to a particular argument that
|
| 1439 |
are not based on the types of the reference and the argument do not
|
| 1440 |
+
affect the formation of a standard conversion sequence, however.
|
| 1441 |
+
|
| 1442 |
+
[*Example 5*: A function with an “lvalue reference to `int`” parameter
|
| 1443 |
+
can be a viable candidate even if the corresponding argument is an `int`
|
| 1444 |
+
bit-field. The formation of implicit conversion sequences treats the
|
| 1445 |
+
`int` bit-field as an `int` lvalue and finds an exact match with the
|
| 1446 |
+
parameter. If the function is selected by overload resolution, the call
|
| 1447 |
+
will nonetheless be ill-formed because of the prohibition on binding a
|
| 1448 |
+
non-`const` lvalue reference to a bit-field (
|
| 1449 |
+
[[dcl.init.ref]]). — *end example*]
|
| 1450 |
|
| 1451 |
##### List-initialization sequence <a id="over.ics.list">[[over.ics.list]]</a>
|
| 1452 |
|
| 1453 |
When an argument is an initializer list ([[dcl.init.list]]), it is not
|
| 1454 |
an expression and special rules apply for converting it to a parameter
|
| 1455 |
type.
|
| 1456 |
|
| 1457 |
+
If the parameter type is an aggregate class `X` and the initializer list
|
| 1458 |
+
has a single element of type cv `U`, where `U` is `X` or a class derived
|
| 1459 |
+
from `X`, the implicit conversion sequence is the one required to
|
| 1460 |
+
convert the element to the parameter type.
|
| 1461 |
+
|
| 1462 |
+
Otherwise, if the parameter type is a character array [^11] and the
|
| 1463 |
+
initializer list has a single element that is an appropriately-typed
|
| 1464 |
+
string literal ([[dcl.init.string]]), the implicit conversion sequence
|
| 1465 |
+
is the identity conversion.
|
| 1466 |
+
|
| 1467 |
+
Otherwise, if the parameter type is `std::initializer_list<X>` and all
|
| 1468 |
+
the elements of the initializer list can be implicitly converted to `X`,
|
| 1469 |
+
the implicit conversion sequence is the worst conversion necessary to
|
| 1470 |
+
convert an element of the list to `X`, or if the initializer list has no
|
| 1471 |
+
elements, the identity conversion. This conversion can be a user-defined
|
| 1472 |
conversion even in the context of a call to an initializer-list
|
| 1473 |
constructor.
|
| 1474 |
|
| 1475 |
+
[*Example 6*:
|
| 1476 |
+
|
| 1477 |
``` cpp
|
| 1478 |
void f(std::initializer_list<int>);
|
| 1479 |
f( {} ); // OK: f(initializer_list<int>) identity conversion
|
| 1480 |
f( {1,2,3} ); // OK: f(initializer_list<int>) identity conversion
|
| 1481 |
f( {'a','b'} ); // OK: f(initializer_list<int>) integral promotion
|
|
|
|
| 1494 |
typedef int IA[3];
|
| 1495 |
void h(const IA&);
|
| 1496 |
h({ 1, 2, 3 }); // OK: identity conversion
|
| 1497 |
```
|
| 1498 |
|
| 1499 |
+
— *end example*]
|
| 1500 |
+
|
| 1501 |
+
Otherwise, if the parameter type is “array of `N` `X`”, if there exists
|
| 1502 |
+
an implicit conversion sequence for each element of the array from the
|
| 1503 |
+
corresponding element of the initializer list (or from `{}` if there is
|
| 1504 |
+
no such element), the implicit conversion sequence is the worst such
|
| 1505 |
+
implicit conversion sequence.
|
| 1506 |
|
| 1507 |
Otherwise, if the parameter is a non-aggregate class `X` and overload
|
| 1508 |
+
resolution per [[over.match.list]] chooses a single best constructor
|
| 1509 |
+
`C` of `X` to perform the initialization of an object of type `X` from
|
| 1510 |
+
the argument initializer list:
|
| 1511 |
+
|
| 1512 |
+
- If `C` is not an initializer-list constructor and the initializer list
|
| 1513 |
+
has a single element of type cv `U`, where `U` is `X` or a class
|
| 1514 |
+
derived from `X`, the implicit conversion sequence has Exact Match
|
| 1515 |
+
rank if `U` is `X`, or Conversion rank if `U` is derived from `X`.
|
| 1516 |
+
- Otherwise, the implicit conversion sequence is a user-defined
|
| 1517 |
+
conversion sequence with the second standard conversion sequence an
|
| 1518 |
+
identity conversion.
|
| 1519 |
+
|
| 1520 |
+
If multiple constructors are viable but none is better than the others,
|
| 1521 |
+
the implicit conversion sequence is the ambiguous conversion sequence.
|
| 1522 |
+
User-defined conversions are allowed for conversion of the initializer
|
| 1523 |
+
list elements to the constructor parameter types except as noted in
|
| 1524 |
+
[[over.best.ics]].
|
| 1525 |
+
|
| 1526 |
+
[*Example 7*:
|
| 1527 |
|
| 1528 |
``` cpp
|
| 1529 |
struct A {
|
| 1530 |
A(std::initializer_list<int>);
|
| 1531 |
};
|
|
|
|
| 1553 |
};
|
| 1554 |
void i(D);
|
| 1555 |
i({ {1,2}, {"bar"} }); // OK: i(D(A(std::initializer_list<int>{1,2\), C(std::string("bar"))))}
|
| 1556 |
```
|
| 1557 |
|
| 1558 |
+
— *end example*]
|
| 1559 |
+
|
| 1560 |
Otherwise, if the parameter has an aggregate type which can be
|
| 1561 |
initialized from the initializer list according to the rules for
|
| 1562 |
aggregate initialization ([[dcl.init.aggr]]), the implicit conversion
|
| 1563 |
sequence is a user-defined conversion sequence with the second standard
|
| 1564 |
conversion sequence an identity conversion.
|
| 1565 |
|
| 1566 |
+
[*Example 8*:
|
| 1567 |
+
|
| 1568 |
``` cpp
|
| 1569 |
struct A {
|
| 1570 |
int m1;
|
| 1571 |
double m2;
|
| 1572 |
};
|
|
|
|
| 1574 |
void f(A);
|
| 1575 |
f( {'a', 'b'} ); // OK: f(A(int,double)) user-defined conversion
|
| 1576 |
f( {1.0} ); // error: narrowing
|
| 1577 |
```
|
| 1578 |
|
| 1579 |
+
— *end example*]
|
| 1580 |
+
|
| 1581 |
+
Otherwise, if the parameter is a reference, see [[over.ics.ref]].
|
| 1582 |
+
|
| 1583 |
+
[*Note 8*: The rules in this section will apply for initializing the
|
| 1584 |
+
underlying temporary for the reference. — *end note*]
|
| 1585 |
+
|
| 1586 |
+
[*Example 9*:
|
| 1587 |
|
| 1588 |
``` cpp
|
| 1589 |
struct A {
|
| 1590 |
int m1;
|
| 1591 |
double m2;
|
|
|
|
| 1597 |
|
| 1598 |
void g(const double &);
|
| 1599 |
g({1}); // same conversion as int to double
|
| 1600 |
```
|
| 1601 |
|
| 1602 |
+
— *end example*]
|
| 1603 |
+
|
| 1604 |
Otherwise, if the parameter type is not a class:
|
| 1605 |
|
| 1606 |
+
- if the initializer list has one element that is not itself an
|
| 1607 |
+
initializer list, the implicit conversion sequence is the one required
|
| 1608 |
+
to convert the element to the parameter type;
|
| 1609 |
+
\[*Example 10*:
|
| 1610 |
``` cpp
|
| 1611 |
void f(int);
|
| 1612 |
f( {'a'} ); // OK: same conversion as char to int
|
| 1613 |
f( {1.0} ); // error: narrowing
|
| 1614 |
```
|
| 1615 |
+
|
| 1616 |
+
— *end example*]
|
| 1617 |
- if the initializer list has no elements, the implicit conversion
|
| 1618 |
sequence is the identity conversion.
|
| 1619 |
+
\[*Example 11*:
|
| 1620 |
``` cpp
|
| 1621 |
void f(int);
|
| 1622 |
f( { } ); // OK: identity conversion
|
| 1623 |
```
|
| 1624 |
|
| 1625 |
+
— *end example*]
|
| 1626 |
+
|
| 1627 |
In all cases other than those enumerated above, no conversion is
|
| 1628 |
possible.
|
| 1629 |
|
| 1630 |
#### Ranking implicit conversion sequences <a id="over.ics.rank">[[over.ics.rank]]</a>
|
| 1631 |
|
| 1632 |
+
This subclause defines a partial ordering of implicit conversion
|
| 1633 |
sequences based on the relationships *better conversion sequence* and
|
| 1634 |
*better conversion*. If an implicit conversion sequence S1 is defined by
|
| 1635 |
these rules to be a better conversion sequence than S2, then it is also
|
| 1636 |
the case that S2 is a *worse conversion sequence* than S1. If conversion
|
| 1637 |
sequence S1 is neither better than nor worse than conversion sequence
|
|
|
|
| 1648 |
[[over.ics.ellipsis]]).
|
| 1649 |
|
| 1650 |
Two implicit conversion sequences of the same form are indistinguishable
|
| 1651 |
conversion sequences unless one of the following rules applies:
|
| 1652 |
|
| 1653 |
+
- List-initialization sequence `L1` is a better conversion sequence than
|
| 1654 |
+
list-initialization sequence `L2` if
|
| 1655 |
+
- `L1` converts to `std::initializer_list<X>` for some `X` and `L2`
|
| 1656 |
+
does not, or, if not that,
|
| 1657 |
+
- `L1` converts to type “array of `N1` `T`”, `L2` converts to type
|
| 1658 |
+
“array of `N2` `T`”, and `N1` is smaller than `N2`,
|
| 1659 |
+
|
| 1660 |
+
even if one of the other rules in this paragraph would otherwise
|
| 1661 |
+
apply.
|
| 1662 |
+
\[*Example 1*:
|
| 1663 |
+
``` cpp
|
| 1664 |
+
void f1(int); // #1
|
| 1665 |
+
void f1(std::initializer_list<long>); // #2
|
| 1666 |
+
void g1() { f1({42}); } // chooses #2
|
| 1667 |
+
|
| 1668 |
+
void f2(std::pair<const char*, const char*>); // #3
|
| 1669 |
+
void f2(std::initializer_list<std::string>); // #4
|
| 1670 |
+
void g2() { f2({"foo","bar"}); } // chooses #4
|
| 1671 |
+
```
|
| 1672 |
+
|
| 1673 |
+
— *end example*]
|
| 1674 |
- Standard conversion sequence `S1` is a better conversion sequence than
|
| 1675 |
standard conversion sequence `S2` if
|
| 1676 |
- `S1` is a proper subsequence of `S2` (comparing the conversion
|
| 1677 |
sequences in the canonical form defined by [[over.ics.scs]],
|
| 1678 |
excluding any Lvalue Transformation; the identity conversion
|
|
|
|
| 1682 |
have the same rank and are distinguishable by the rules in the
|
| 1683 |
paragraph below, or, if not that,
|
| 1684 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and neither
|
| 1685 |
refers to an implicit object parameter of a non-static member
|
| 1686 |
function declared without a *ref-qualifier*, and `S1` binds an
|
| 1687 |
+
rvalue reference to an rvalue and `S2` binds an lvalue reference
|
| 1688 |
+
\[*Example 2*:
|
| 1689 |
``` cpp
|
| 1690 |
int i;
|
| 1691 |
int f1();
|
| 1692 |
int&& f2();
|
| 1693 |
int g(const int&);
|
|
|
|
| 1709 |
a << 'c'; // calls A::operator<<(int)
|
| 1710 |
A().p(); // calls A::p()&&
|
| 1711 |
a.p(); // calls A::p()&
|
| 1712 |
```
|
| 1713 |
|
| 1714 |
+
— *end example*]
|
| 1715 |
or, if not that,
|
| 1716 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and `S1`
|
| 1717 |
binds an lvalue reference to a function lvalue and `S2` binds an
|
| 1718 |
+
rvalue reference to a function lvalue
|
| 1719 |
+
\[*Example 3*:
|
| 1720 |
``` cpp
|
| 1721 |
int f(void(&)()); // #1
|
| 1722 |
int f(void(&&)()); // #2
|
| 1723 |
void g();
|
| 1724 |
int i1 = f(g); // calls #1
|
| 1725 |
```
|
| 1726 |
|
| 1727 |
+
— *end example*]
|
| 1728 |
or, if not that,
|
| 1729 |
- `S1`
|
| 1730 |
and `S2` differ only in their qualification conversion and yield
|
| 1731 |
similar types `T1` and `T2` ([[conv.qual]]), respectively, and the
|
| 1732 |
cv-qualification signature of type `T1` is a proper subset of the
|
| 1733 |
+
cv-qualification signature of type `T2`
|
| 1734 |
+
\[*Example 4*:
|
| 1735 |
``` cpp
|
| 1736 |
int f(const volatile int *);
|
| 1737 |
int f(const int *);
|
| 1738 |
int i;
|
| 1739 |
int j = f(&i); // calls f(const int*)
|
| 1740 |
```
|
| 1741 |
|
| 1742 |
+
— *end example*]
|
| 1743 |
or, if not that,
|
| 1744 |
- `S1`
|
| 1745 |
and `S2` are reference bindings ([[dcl.init.ref]]), and the types
|
| 1746 |
to which the references refer are the same type except for top-level
|
| 1747 |
cv-qualifiers, and the type to which the reference initialized by
|
| 1748 |
`S2` refers is more cv-qualified than the type to which the
|
| 1749 |
reference initialized by `S1` refers.
|
| 1750 |
+
\[*Example 5*:
|
| 1751 |
``` cpp
|
| 1752 |
int f(const int &);
|
| 1753 |
int f(int &);
|
| 1754 |
int g(const int &);
|
| 1755 |
int g(int);
|
|
|
|
| 1765 |
void g(const X& a, X b) {
|
| 1766 |
a.f(); // calls X::f() const
|
| 1767 |
b.f(); // calls X::f()
|
| 1768 |
}
|
| 1769 |
```
|
| 1770 |
+
|
| 1771 |
+
— *end example*]
|
| 1772 |
- User-defined conversion sequence `U1` is a better conversion sequence
|
| 1773 |
than another user-defined conversion sequence `U2` if they contain the
|
| 1774 |
same user-defined conversion function or constructor or they
|
| 1775 |
initialize the same class in an aggregate initialization and in either
|
| 1776 |
case the second standard conversion sequence of `U1` is better than
|
| 1777 |
the second standard conversion sequence of `U2`.
|
| 1778 |
+
\[*Example 6*:
|
| 1779 |
``` cpp
|
| 1780 |
struct A {
|
| 1781 |
operator short();
|
| 1782 |
} a;
|
| 1783 |
int f(int);
|
| 1784 |
int f(float);
|
| 1785 |
int i = f(a); // calls f(int), because short → int is
|
| 1786 |
// better than short → float.
|
| 1787 |
```
|
| 1788 |
+
|
| 1789 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1790 |
|
| 1791 |
Standard conversion sequences are ordered by their ranks: an Exact Match
|
| 1792 |
is a better conversion than a Promotion, which is a better conversion
|
| 1793 |
than a Conversion. Two conversion sequences with the same rank are
|
| 1794 |
indistinguishable unless one of the following rules applies:
|
|
|
|
| 1804 |
of `B*` to `void*`.
|
| 1805 |
- If class `B` is derived directly or indirectly from class `A` and
|
| 1806 |
class `C` is derived directly or indirectly from `B`,
|
| 1807 |
- conversion of `C*` to `B*` is better than conversion of `C*` to
|
| 1808 |
`A*`,
|
| 1809 |
+
\[*Example 7*:
|
| 1810 |
``` cpp
|
| 1811 |
struct A {};
|
| 1812 |
struct B : public A {};
|
| 1813 |
struct C : public B {};
|
| 1814 |
C* pc;
|
| 1815 |
int f(A*);
|
| 1816 |
int f(B*);
|
| 1817 |
int i = f(pc); // calls f(B*)
|
| 1818 |
```
|
| 1819 |
+
|
| 1820 |
+
— *end example*]
|
| 1821 |
- binding of an expression of type `C` to a reference to type `B` is
|
| 1822 |
better than binding an expression of type `C` to a reference to type
|
| 1823 |
`A`,
|
| 1824 |
- conversion of `A::*` to `B::*` is better than conversion of `A::*`
|
| 1825 |
to `C::*`,
|
|
|
|
| 1831 |
`A`,
|
| 1832 |
- conversion of `B::*` to `C::*` is better than conversion of `A::*`
|
| 1833 |
to `C::*`, and
|
| 1834 |
- conversion of `B` to `A` is better than conversion of `C` to `A`.
|
| 1835 |
|
| 1836 |
+
\[*Note 1*: Compared conversion sequences will have different source
|
| 1837 |
+
types only in the context of comparing the second standard conversion
|
| 1838 |
+
sequence of an initialization by user-defined conversion (see
|
| 1839 |
+
[[over.match.best]]); in all other contexts, the source types will be
|
| 1840 |
+
the same and the target types will be different. — *end note*]
|
| 1841 |
|
| 1842 |
## Address of overloaded function <a id="over.over">[[over.over]]</a>
|
| 1843 |
|
| 1844 |
A use of an overloaded function name without arguments is resolved in
|
| 1845 |
certain contexts to a function, a pointer to function or a pointer to
|
| 1846 |
member function for a specific function from the overload set. A
|
| 1847 |
function template name is considered to name a set of overloaded
|
| 1848 |
+
functions in such contexts. A function with type `F` is selected for the
|
| 1849 |
+
function type `FT` of the target type required in the context if `F`
|
| 1850 |
+
(after possibly applying the function pointer conversion (
|
| 1851 |
+
[[conv.fctptr]])) is identical to `FT`.
|
| 1852 |
+
|
| 1853 |
+
[*Note 1*: That is, the class of which the function is a member is
|
| 1854 |
+
ignored when matching a pointer-to-member-function type. — *end note*]
|
| 1855 |
+
|
| 1856 |
+
The target can be
|
| 1857 |
|
| 1858 |
- an object or reference being initialized ([[dcl.init]],
|
| 1859 |
[[dcl.init.ref]], [[dcl.init.list]]),
|
| 1860 |
- the left side of an assignment ([[expr.ass]]),
|
| 1861 |
- a parameter of a function ([[expr.call]]),
|
|
|
|
| 1866 |
[[expr.static.cast]], [[expr.cast]]), or
|
| 1867 |
- a non-type *template-parameter* ([[temp.arg.nontype]]).
|
| 1868 |
|
| 1869 |
The overloaded function name can be preceded by the `&` operator. An
|
| 1870 |
overloaded function name shall not be used without arguments in contexts
|
| 1871 |
+
other than those listed.
|
| 1872 |
+
|
| 1873 |
+
[*Note 2*: Any redundant set of parentheses surrounding the overloaded
|
| 1874 |
+
function name is ignored ([[expr.prim]]). — *end note*]
|
| 1875 |
|
| 1876 |
If the name is a function template, template argument deduction is
|
| 1877 |
done ([[temp.deduct.funcaddr]]), and if the argument deduction
|
| 1878 |
succeeds, the resulting template argument list is used to generate a
|
| 1879 |
single function template specialization, which is added to the set of
|
| 1880 |
+
overloaded functions considered.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1881 |
|
| 1882 |
+
[*Note 3*: As described in [[temp.arg.explicit]], if deduction fails
|
| 1883 |
+
and the function template name is followed by an explicit template
|
| 1884 |
+
argument list, the *template-id* is then examined to see whether it
|
| 1885 |
+
identifies a single function template specialization. If it does, the
|
| 1886 |
+
*template-id* is considered to be an lvalue for that function template
|
| 1887 |
+
specialization. The target type is not used in that
|
| 1888 |
+
determination. — *end note*]
|
| 1889 |
+
|
| 1890 |
+
Non-member functions and static member functions match targets of
|
| 1891 |
+
function pointer type or reference to function type. Non-static member
|
| 1892 |
+
functions match targets of pointer to member function type. If a
|
| 1893 |
non-static member function is selected, the reference to the overloaded
|
| 1894 |
function name is required to have the form of a pointer to member as
|
| 1895 |
described in [[expr.unary.op]].
|
| 1896 |
|
| 1897 |
If more than one function is selected, any function template
|
|
|
|
| 1901 |
a second function template specialization whose function template is
|
| 1902 |
more specialized than the function template of `F1` according to the
|
| 1903 |
partial ordering rules of [[temp.func.order]]. After such eliminations,
|
| 1904 |
if any, there shall remain exactly one selected function.
|
| 1905 |
|
| 1906 |
+
[*Example 1*:
|
| 1907 |
+
|
| 1908 |
``` cpp
|
| 1909 |
int f(double);
|
| 1910 |
int f(int);
|
| 1911 |
int (*pfd)(double) = &f; // selects f(double)
|
| 1912 |
int (*pfi)(int) = &f; // selects f(int)
|
|
|
|
| 1935 |
int (X::*p5)(int) = &(X::f); // error: wrong syntax for
|
| 1936 |
// pointer to member
|
| 1937 |
int (*p6)(long) = &(X::f); // OK
|
| 1938 |
```
|
| 1939 |
|
| 1940 |
+
— *end example*]
|
|
|
|
|
|
|
| 1941 |
|
| 1942 |
+
[*Note 4*: If `f()` and `g()` are both overloaded functions, the cross
|
| 1943 |
+
product of possibilities must be considered to resolve `f(&g)`, or the
|
| 1944 |
+
equivalent expression `f(g)`. — *end note*]
|
| 1945 |
+
|
| 1946 |
+
[*Note 5*:
|
| 1947 |
+
|
| 1948 |
+
Even if `B` is a public base of `D`, we have
|
| 1949 |
|
| 1950 |
``` cpp
|
| 1951 |
D* f();
|
| 1952 |
B* (*p1)() = &f; // error
|
| 1953 |
|
| 1954 |
void g(D*);
|
| 1955 |
void (*p2)(B*) = &g; // error
|
| 1956 |
```
|
| 1957 |
|
| 1958 |
+
— *end note*]
|
| 1959 |
+
|
| 1960 |
## Overloaded operators <a id="over.oper">[[over.oper]]</a>
|
| 1961 |
|
| 1962 |
A function declaration having one of the following
|
| 1963 |
*operator-function-id*s as its name declares an *operator function*. A
|
| 1964 |
function template declaration having one of the following
|
|
|
|
| 1970 |
``` bnf
|
| 1971 |
operator-function-id:
|
| 1972 |
'operator' operator
|
| 1973 |
```
|
| 1974 |
|
| 1975 |
+
[*Note 1*: The last two operators are function call ([[expr.call]])
|
| 1976 |
+
and subscripting ([[expr.sub]]). The operators `new[]`, `delete[]`,
|
| 1977 |
+
`()`, and `[]` are formed from more than one token. — *end note*]
|
| 1978 |
|
| 1979 |
Both the unary and binary forms of
|
| 1980 |
|
| 1981 |
``` cpp
|
| 1982 |
+ - * &
|
|
|
|
| 1996 |
invoked to evaluate the operators they implement ([[over.unary]] –
|
| 1997 |
[[over.inc]]). They can be explicitly called, however, using the
|
| 1998 |
*operator-function-id* as the name of the function in the function call
|
| 1999 |
syntax ([[expr.call]]).
|
| 2000 |
|
| 2001 |
+
[*Example 1*:
|
| 2002 |
+
|
| 2003 |
``` cpp
|
| 2004 |
complex z = a.operator+(b); // complex z = a+b;
|
| 2005 |
void* p = operator new(sizeof(int)*n);
|
| 2006 |
```
|
| 2007 |
|
| 2008 |
+
— *end example*]
|
| 2009 |
+
|
| 2010 |
The allocation and deallocation functions, `operator` `new`, `operator`
|
| 2011 |
`new[]`, `operator` `delete` and `operator` `delete[]`, are described
|
| 2012 |
completely in [[basic.stc.dynamic]]. The attributes and restrictions
|
| 2013 |
found in the rest of this subclause do not apply to them unless
|
| 2014 |
explicitly stated in [[basic.stc.dynamic]].
|
|
|
|
| 2048 |
[[over.match.oper]] determine which, if any, interpretation is used.
|
| 2049 |
See [[over.inc]] for an explanation of the postfix unary operators `++`
|
| 2050 |
and `\dcr`.
|
| 2051 |
|
| 2052 |
The unary and binary forms of the same operator are considered to have
|
| 2053 |
+
the same name.
|
| 2054 |
+
|
| 2055 |
+
[*Note 1*: Consequently, a unary operator can hide a binary operator
|
| 2056 |
+
from an enclosing scope, and vice versa. — *end note*]
|
| 2057 |
|
| 2058 |
### Binary operators <a id="over.binary">[[over.binary]]</a>
|
| 2059 |
|
| 2060 |
A binary operator shall be implemented either by a non-static member
|
| 2061 |
function ([[class.mfct]]) with one parameter or by a non-member
|
|
|
|
| 2071 |
`operator=` is implicitly declared for a class if not declared by the
|
| 2072 |
user ([[class.copy]]), a base class assignment operator is always
|
| 2073 |
hidden by the copy assignment operator of the derived class.
|
| 2074 |
|
| 2075 |
Any assignment operator, even the copy and move assignment operators,
|
| 2076 |
+
can be virtual.
|
| 2077 |
+
|
| 2078 |
+
[*Note 1*:
|
| 2079 |
+
|
| 2080 |
+
For a derived class `D` with a base class `B` for which a virtual
|
| 2081 |
+
copy/move assignment has been declared, the copy/move assignment
|
| 2082 |
+
operator in `D` does not override `B`’s virtual copy/move assignment
|
| 2083 |
+
operator.
|
| 2084 |
+
|
| 2085 |
+
[*Example 1*:
|
| 2086 |
|
| 2087 |
``` cpp
|
| 2088 |
struct B {
|
| 2089 |
virtual int operator= (int);
|
| 2090 |
virtual B& operator= (const B&);
|
|
|
|
| 2100 |
void f() {
|
| 2101 |
bptr->operator=(99); // calls D::operator=(int)
|
| 2102 |
*bptr = 99; // ditto
|
| 2103 |
bptr->operator=(dobj2); // calls D::operator=(const B&)
|
| 2104 |
*bptr = dobj2; // ditto
|
| 2105 |
+
dobj1 = dobj2; // calls implicitly-declared D::operator=(const D&)
|
|
|
|
| 2106 |
}
|
| 2107 |
```
|
| 2108 |
|
| 2109 |
+
— *end example*]
|
| 2110 |
+
|
| 2111 |
+
— *end note*]
|
| 2112 |
+
|
| 2113 |
### Function call <a id="over.call">[[over.call]]</a>
|
| 2114 |
|
| 2115 |
`operator()`
|
| 2116 |
|
| 2117 |
shall be a non-static member function with an arbitrary number of
|
|
|
|
| 2136 |
|
| 2137 |
shall be a non-static member function with exactly one parameter. It
|
| 2138 |
implements the subscripting syntax
|
| 2139 |
|
| 2140 |
``` bnf
|
| 2141 |
+
postfix-expression '[' expr-or-braced-init-list ']'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2142 |
```
|
| 2143 |
|
| 2144 |
Thus, a subscripting expression `x[y]` is interpreted as
|
| 2145 |
`x.operator[](y)` for a class object `x` of type `T` if
|
| 2146 |
`T::operator[](T1)` exists and if the operator is selected as the best
|
| 2147 |
match function by the overload resolution mechanism (
|
| 2148 |
[[over.match.best]]).
|
| 2149 |
|
| 2150 |
+
[*Example 1*:
|
| 2151 |
+
|
| 2152 |
``` cpp
|
| 2153 |
struct X {
|
| 2154 |
Z operator[](std::initializer_list<int>);
|
| 2155 |
};
|
| 2156 |
X x;
|
| 2157 |
x[{1,2,3}] = 7; // OK: meaning x.operator[]({1,2,3\)}
|
| 2158 |
int a[10];
|
| 2159 |
a[{1,2,3}] = 7; // error: built-in subscript operator
|
| 2160 |
```
|
| 2161 |
|
| 2162 |
+
— *end example*]
|
| 2163 |
+
|
| 2164 |
### Class member access <a id="over.ref">[[over.ref]]</a>
|
| 2165 |
|
| 2166 |
`operator->`
|
| 2167 |
|
| 2168 |
shall be a non-static member function taking no parameters. It
|
|
|
|
| 2187 |
the function is a non-static member function with one parameter (which
|
| 2188 |
shall be of type `int`) or a non-member function with two parameters
|
| 2189 |
(the second of which shall be of type `int`), it defines the postfix
|
| 2190 |
increment operator `++` for objects of that type. When the postfix
|
| 2191 |
increment is called as a result of using the `++` operator, the `int`
|
| 2192 |
+
argument will have value zero.[^12]
|
| 2193 |
+
|
| 2194 |
+
[*Example 1*:
|
| 2195 |
|
| 2196 |
``` cpp
|
| 2197 |
struct X {
|
| 2198 |
X& operator++(); // prefix ++a
|
| 2199 |
X operator++(int); // postfix a++
|
|
|
|
| 2214 |
operator++(b); // explicit call: like ++b;
|
| 2215 |
operator++(b, 0); // explicit call: like b++;
|
| 2216 |
}
|
| 2217 |
```
|
| 2218 |
|
| 2219 |
+
— *end example*]
|
| 2220 |
+
|
| 2221 |
The prefix and postfix decrement operators `-{-}` are handled
|
| 2222 |
analogously.
|
| 2223 |
|
| 2224 |
### User-defined literals <a id="over.literal">[[over.literal]]</a>
|
| 2225 |
|
|
|
|
| 2231 |
|
| 2232 |
The *string-literal* or *user-defined-string-literal* in a
|
| 2233 |
*literal-operator-id* shall have no *encoding-prefix* and shall contain
|
| 2234 |
no characters other than the implicit terminating `'\0'`. The
|
| 2235 |
*ud-suffix* of the *user-defined-string-literal* or the *identifier* in
|
| 2236 |
+
a *literal-operator-id* is called a *literal suffix identifier*. Some
|
| 2237 |
literal suffix identifiers are reserved for future standardization; see
|
| 2238 |
+
[[usrlit.suffix]]. A declaration whose *literal-operator-id* uses such a
|
| 2239 |
+
literal suffix identifier is ill-formed, no diagnostic required.
|
| 2240 |
|
| 2241 |
A declaration whose *declarator-id* is a *literal-operator-id* shall be
|
| 2242 |
a declaration of a namespace-scope function or function template (it
|
| 2243 |
could be a friend function ([[class.friend]])), an explicit
|
| 2244 |
instantiation or specialization of a function template, or a
|
|
|
|
| 2275 |
pack ([[temp.variadic]]) with element type `char`.
|
| 2276 |
|
| 2277 |
Literal operators and literal operator templates shall not have C
|
| 2278 |
language linkage.
|
| 2279 |
|
| 2280 |
+
[*Note 1*: Literal operators and literal operator templates are usually
|
| 2281 |
+
invoked implicitly through user-defined literals ([[lex.ext]]).
|
| 2282 |
+
However, except for the constraints described above, they are ordinary
|
| 2283 |
+
namespace-scope functions and function templates. In particular, they
|
| 2284 |
+
are looked up like ordinary functions and function templates and they
|
| 2285 |
+
follow the same overload resolution rules. Also, they can be declared
|
| 2286 |
+
`inline` or `constexpr`, they may have internal or external linkage,
|
| 2287 |
+
they can be called explicitly, their addresses can be taken,
|
| 2288 |
+
etc. — *end note*]
|
| 2289 |
+
|
| 2290 |
+
[*Example 1*:
|
| 2291 |
|
| 2292 |
``` cpp
|
| 2293 |
void operator "" _km(long double); // OK
|
| 2294 |
string operator "" _i18n(const char*, std::size_t); // OK
|
| 2295 |
template <char...> double operator "" _\u03C0(); // OK: UCN for lowercase pi
|
| 2296 |
float operator ""_e(const char*); // OK
|
| 2297 |
float operator ""E(const char*); // error: reserved literal suffix~([usrlit.suffix], [lex.ext])
|
| 2298 |
+
double operator""_Bq(long double); // OK: does not use the reserved identifier _Bq~([lex.name])
|
| 2299 |
+
double operator"" _Bq(long double); // uses the reserved identifier _Bq~([lex.name])
|
| 2300 |
float operator " " B(const char*); // error: non-empty string-literal
|
| 2301 |
string operator "" 5X(const char*, std::size_t); // error: invalid literal suffix identifier
|
| 2302 |
double operator "" _miles(double); // error: invalid parameter-declaration-clause
|
| 2303 |
template <char...> int operator "" _j(const char*); // error: invalid parameter-declaration-clause
|
| 2304 |
extern "C" void operator "" _m(long double); // error: C language linkage
|
| 2305 |
```
|
| 2306 |
|
| 2307 |
+
— *end example*]
|
| 2308 |
+
|
| 2309 |
## Built-in operators <a id="over.built">[[over.built]]</a>
|
| 2310 |
|
| 2311 |
The candidate operator functions that represent the built-in operators
|
| 2312 |
defined in Clause [[expr]] are specified in this subclause. These
|
| 2313 |
candidate functions participate in the operator overload resolution
|
| 2314 |
process as described in [[over.match.oper]] and are used for no other
|
| 2315 |
+
purpose.
|
| 2316 |
+
|
| 2317 |
+
[*Note 1*: Because built-in operators take only operands with non-class
|
| 2318 |
type, and operator overload resolution occurs only when an operand
|
| 2319 |
expression originally has class or enumeration type, operator overload
|
| 2320 |
resolution can resolve to a built-in operator only when an operand has a
|
| 2321 |
class type that has a user-defined conversion to a non-class type
|
| 2322 |
appropriate for the operator, or when an operand has an enumeration type
|
|
|
|
| 2327 |
overload resolution the expression is subject to the requirements for
|
| 2328 |
the built-in operator given in Clause [[expr]], and therefore to any
|
| 2329 |
additional semantic constraints given there. If there is a user-written
|
| 2330 |
candidate with the same name and parameter types as a built-in candidate
|
| 2331 |
operator function, the built-in operator function is hidden and is not
|
| 2332 |
+
included in the set of candidate functions. — *end note*]
|
| 2333 |
|
| 2334 |
In this subclause, the term *promoted integral type* is used to refer to
|
| 2335 |
+
those integral types which are preserved by integral promotion (
|
| 2336 |
+
[[conv.prom]]) (including e.g. `int` and `long` but excluding e.g.
|
| 2337 |
+
`char`). Similarly, the term *promoted arithmetic type* refers to
|
| 2338 |
+
floating types plus promoted integral types.
|
| 2339 |
+
|
| 2340 |
+
[*Note 2*: In all cases where a promoted integral type or promoted
|
| 2341 |
+
arithmetic type is required, an operand of enumeration type will be
|
| 2342 |
+
acceptable by way of the integral promotions. — *end note*]
|
| 2343 |
+
|
| 2344 |
+
In the remainder of this section, *vq* represents either `volatile` or
|
| 2345 |
+
no cv-qualifier.
|
| 2346 |
+
|
| 2347 |
+
For every pair (`T`, *vq*), where `T` is an arithmetic type other than
|
| 2348 |
+
`bool`, there exist candidate operator functions of the form
|
| 2349 |
+
|
| 2350 |
+
``` cpp
|
| 2351 |
+
vq T& operator++(vq T&);
|
| 2352 |
+
T operator++(vq T&, int);
|
| 2353 |
+
```
|
| 2354 |
+
|
| 2355 |
+
For every pair (`T`, *vq*), where `T` is an arithmetic type other than
|
| 2356 |
+
`bool`, there exist candidate operator functions of the form
|
| 2357 |
+
|
| 2358 |
+
``` cpp
|
| 2359 |
+
vq T& operator--(vq T&);
|
| 2360 |
+
T operator--(vq T&, int);
|
| 2361 |
+
```
|
| 2362 |
+
|
| 2363 |
+
For every pair (`T`, *vq*), where `T` is a cv-qualified or
|
| 2364 |
+
cv-unqualified object type, there exist candidate operator functions of
|
| 2365 |
the form
|
| 2366 |
|
| 2367 |
``` cpp
|
| 2368 |
+
T*vq& operator++(T*vq&);
|
| 2369 |
+
T*vq& operator--(T*vq&);
|
| 2370 |
+
T* operator++(T*vq&, int);
|
| 2371 |
+
T* operator--(T*vq&, int);
|
| 2372 |
```
|
| 2373 |
|
| 2374 |
+
For every cv-qualified or cv-unqualified object type `T`, there exist
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2375 |
candidate operator functions of the form
|
| 2376 |
|
| 2377 |
``` cpp
|
| 2378 |
T& operator*(T*);
|
| 2379 |
```
|
| 2380 |
|
| 2381 |
+
For every function type `T` that does not have cv-qualifiers or a
|
| 2382 |
*ref-qualifier*, there exist candidate operator functions of the form
|
| 2383 |
|
| 2384 |
``` cpp
|
| 2385 |
T& operator*(T*);
|
| 2386 |
```
|
| 2387 |
|
| 2388 |
+
For every type `T` there exist candidate operator functions of the form
|
| 2389 |
|
| 2390 |
``` cpp
|
| 2391 |
T* operator+(T*);
|
| 2392 |
```
|
| 2393 |
|
| 2394 |
+
For every promoted arithmetic type `T`, there exist candidate operator
|
| 2395 |
functions of the form
|
| 2396 |
|
| 2397 |
``` cpp
|
| 2398 |
T operator+(T);
|
| 2399 |
T operator-(T);
|
| 2400 |
```
|
| 2401 |
|
| 2402 |
+
For every promoted integral type `T`, there exist candidate operator
|
| 2403 |
functions of the form
|
| 2404 |
|
| 2405 |
``` cpp
|
| 2406 |
T operator~(T);
|
| 2407 |
```
|
| 2408 |
|
| 2409 |
+
For every quintuple (`C1`, `C2`, `T`, *cv1*, *cv2*), where `C2` is a
|
| 2410 |
+
class type, `C1` is the same type as `C2` or is a derived class of `C2`,
|
| 2411 |
+
and `T` is an object type or a function type, there exist candidate
|
| 2412 |
+
operator functions of the form
|
|
|
|
| 2413 |
|
| 2414 |
``` cpp
|
| 2415 |
+
cv12 T& operator->*(cv1 C1*, cv2 T C2::*);
|
| 2416 |
```
|
| 2417 |
|
| 2418 |
+
where *cv12* is the union of *cv1* and *cv2*. The return type is shown
|
| 2419 |
for exposition only; see [[expr.mptr.oper]] for the determination of
|
| 2420 |
the operator’s result type.
|
| 2421 |
|
| 2422 |
+
For every pair of promoted arithmetic types `L` and `R`, there exist
|
| 2423 |
candidate operator functions of the form
|
| 2424 |
|
| 2425 |
``` cpp
|
| 2426 |
LR operator*(L, R);
|
| 2427 |
LR operator/(L, R);
|
|
|
|
| 2433 |
bool operator>=(L, R);
|
| 2434 |
bool operator==(L, R);
|
| 2435 |
bool operator!=(L, R);
|
| 2436 |
```
|
| 2437 |
|
| 2438 |
+
where `LR` is the result of the usual arithmetic conversions between
|
| 2439 |
+
types `L` and `R`.
|
| 2440 |
|
| 2441 |
+
For every cv-qualified or cv-unqualified object type `T` there exist
|
| 2442 |
candidate operator functions of the form
|
| 2443 |
|
| 2444 |
``` cpp
|
| 2445 |
T* operator+(T*, std::ptrdiff_t);
|
| 2446 |
T& operator[](T*, std::ptrdiff_t);
|
| 2447 |
T* operator-(T*, std::ptrdiff_t);
|
| 2448 |
T* operator+(std::ptrdiff_t, T*);
|
| 2449 |
T& operator[](std::ptrdiff_t, T*);
|
| 2450 |
```
|
| 2451 |
|
| 2452 |
+
For every `T`, where `T` is a pointer to object type, there exist
|
| 2453 |
candidate operator functions of the form
|
| 2454 |
|
| 2455 |
``` cpp
|
| 2456 |
std::ptrdiff_t operator-(T, T);
|
| 2457 |
```
|
| 2458 |
|
| 2459 |
+
For every `T`, where `T` is an enumeration type or a pointer type, there
|
| 2460 |
exist candidate operator functions of the form
|
| 2461 |
|
| 2462 |
``` cpp
|
| 2463 |
bool operator<(T, T);
|
| 2464 |
bool operator>(T, T);
|
|
|
|
| 2466 |
bool operator>=(T, T);
|
| 2467 |
bool operator==(T, T);
|
| 2468 |
bool operator!=(T, T);
|
| 2469 |
```
|
| 2470 |
|
| 2471 |
+
For every pointer to member type `T` or type `std::nullptr_t` there
|
| 2472 |
exist candidate operator functions of the form
|
| 2473 |
|
| 2474 |
``` cpp
|
| 2475 |
bool operator==(T, T);
|
| 2476 |
bool operator!=(T, T);
|
| 2477 |
```
|
| 2478 |
|
| 2479 |
+
For every pair of promoted integral types `L` and `R`, there exist
|
| 2480 |
candidate operator functions of the form
|
| 2481 |
|
| 2482 |
``` cpp
|
| 2483 |
LR operator%(L, R);
|
| 2484 |
LR operator&(L, R);
|
|
|
|
| 2486 |
LR operator|(L, R);
|
| 2487 |
L operator<<(L, R);
|
| 2488 |
L operator>>(L, R);
|
| 2489 |
```
|
| 2490 |
|
| 2491 |
+
where `LR` is the result of the usual arithmetic conversions between
|
| 2492 |
+
types `L` and `R`.
|
| 2493 |
|
| 2494 |
+
For every triple (`L`, *vq*, `R`), where `L` is an arithmetic type, and
|
| 2495 |
+
`R` is a promoted arithmetic type, there exist candidate operator
|
| 2496 |
+
functions of the form
|
| 2497 |
|
| 2498 |
``` cpp
|
| 2499 |
+
vq L& operator=(vq L&, R);
|
| 2500 |
+
vq L& operator*=(vq L&, R);
|
| 2501 |
+
vq L& operator/=(vq L&, R);
|
| 2502 |
+
vq L& operator+=(vq L&, R);
|
| 2503 |
+
vq L& operator-=(vq L&, R);
|
| 2504 |
```
|
| 2505 |
|
| 2506 |
+
For every pair (`T`, *vq*), where `T` is any type, there exist candidate
|
| 2507 |
+
operator functions of the form
|
|
|
|
| 2508 |
|
| 2509 |
``` cpp
|
| 2510 |
+
T*vq& operator=(T*vq&, T*);
|
| 2511 |
```
|
| 2512 |
|
| 2513 |
+
For every pair (`T`, *vq*), where `T` is an enumeration or pointer to
|
| 2514 |
+
member type, there exist candidate operator functions of the form
|
|
|
|
| 2515 |
|
| 2516 |
``` cpp
|
| 2517 |
+
vq T& operator=(vq T&, T);
|
| 2518 |
```
|
| 2519 |
|
| 2520 |
+
For every pair (`T`, *vq*), where `T` is a cv-qualified or
|
| 2521 |
+
cv-unqualified object type, there exist candidate operator functions of
|
| 2522 |
+
the form
|
| 2523 |
|
| 2524 |
``` cpp
|
| 2525 |
+
T*vq& operator+=(T*vq&, std::ptrdiff_t);
|
| 2526 |
+
T*vq& operator-=(T*vq&, std::ptrdiff_t);
|
| 2527 |
```
|
| 2528 |
|
| 2529 |
+
For every triple (`L`, *vq*, `R`), where `L` is an integral type, and
|
| 2530 |
+
`R` is a promoted integral type, there exist candidate operator
|
| 2531 |
+
functions of the form
|
| 2532 |
|
| 2533 |
``` cpp
|
| 2534 |
+
vq L& operator%=(vq L&, R);
|
| 2535 |
+
vq L& operator<<=(vq L&, R);
|
| 2536 |
+
vq L& operator>>=(vq L&, R);
|
| 2537 |
+
vq L& operator&=(vq L&, R);
|
| 2538 |
+
vq L& operator^=(vq L&, R);
|
| 2539 |
+
vq L& operator|=(vq L&, R);
|
| 2540 |
```
|
| 2541 |
|
| 2542 |
There also exist candidate operator functions of the form
|
| 2543 |
|
| 2544 |
``` cpp
|
| 2545 |
bool operator!(bool);
|
| 2546 |
bool operator&&(bool, bool);
|
| 2547 |
bool operator||(bool, bool);
|
| 2548 |
```
|
| 2549 |
|
| 2550 |
+
For every pair of promoted arithmetic types `L` and `R`, there exist
|
| 2551 |
candidate operator functions of the form
|
| 2552 |
|
| 2553 |
``` cpp
|
| 2554 |
LR operator?:(bool, L, R);
|
| 2555 |
```
|
| 2556 |
|
| 2557 |
+
where `LR` is the result of the usual arithmetic conversions between
|
| 2558 |
+
types `L` and `R`.
|
|
|
|
|
|
|
|
|
|
| 2559 |
|
| 2560 |
+
[*Note 3*: As with all these descriptions of candidate functions, this
|
| 2561 |
+
declaration serves only to describe the built-in operator for purposes
|
| 2562 |
+
of overload resolution. The operator “`?:`” cannot be
|
| 2563 |
+
overloaded. — *end note*]
|
| 2564 |
+
|
| 2565 |
+
For every type `T`, where `T` is a pointer, pointer-to-member, or scoped
|
| 2566 |
enumeration type, there exist candidate operator functions of the form
|
| 2567 |
|
| 2568 |
``` cpp
|
| 2569 |
T operator?:(bool, T, T);
|
| 2570 |
```
|
|
|
|
| 2585 |
[class.this]: class.md#class.this
|
| 2586 |
[conv]: conv.md#conv
|
| 2587 |
[conv.array]: conv.md#conv.array
|
| 2588 |
[conv.bool]: conv.md#conv.bool
|
| 2589 |
[conv.double]: conv.md#conv.double
|
| 2590 |
+
[conv.fctptr]: conv.md#conv.fctptr
|
| 2591 |
[conv.fpint]: conv.md#conv.fpint
|
| 2592 |
[conv.fpprom]: conv.md#conv.fpprom
|
| 2593 |
[conv.func]: conv.md#conv.func
|
| 2594 |
[conv.integral]: conv.md#conv.integral
|
| 2595 |
[conv.lval]: conv.md#conv.lval
|
|
|
|
| 2598 |
[conv.ptr]: conv.md#conv.ptr
|
| 2599 |
[conv.qual]: conv.md#conv.qual
|
| 2600 |
[cpp]: cpp.md#cpp
|
| 2601 |
[dcl.array]: dcl.md#dcl.array
|
| 2602 |
[dcl.fct]: dcl.md#dcl.fct
|
| 2603 |
+
[dcl.fct.def.delete]: dcl.md#dcl.fct.def.delete
|
| 2604 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 2605 |
[dcl.init]: dcl.md#dcl.init
|
| 2606 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
| 2607 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 2608 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
| 2609 |
+
[dcl.init.string]: dcl.md#dcl.init.string
|
| 2610 |
[dcl.typedef]: dcl.md#dcl.typedef
|
| 2611 |
+
[except.spec]: except.md#except.spec
|
| 2612 |
[expr]: expr.md#expr
|
| 2613 |
[expr.ass]: expr.md#expr.ass
|
| 2614 |
[expr.call]: expr.md#expr.call
|
| 2615 |
[expr.cast]: expr.md#expr.cast
|
| 2616 |
[expr.cond]: expr.md#expr.cond
|
|
|
|
| 2641 |
[over.literal]: #over.literal
|
| 2642 |
[over.load]: #over.load
|
| 2643 |
[over.match]: #over.match
|
| 2644 |
[over.match.best]: #over.match.best
|
| 2645 |
[over.match.call]: #over.match.call
|
| 2646 |
+
[over.match.class.deduct]: #over.match.class.deduct
|
| 2647 |
[over.match.conv]: #over.match.conv
|
| 2648 |
[over.match.copy]: #over.match.copy
|
| 2649 |
[over.match.ctor]: #over.match.ctor
|
| 2650 |
[over.match.funcs]: #over.match.funcs
|
| 2651 |
[over.match.list]: #over.match.list
|
|
|
|
| 2723 |
tournament `F` encountered another function `G` such that `F` was
|
| 2724 |
not better than `G`. Hence, `W` is either the best function or there
|
| 2725 |
is no best function. So, make a second pass over the viable
|
| 2726 |
functions to verify that `W` is better than all other functions.
|
| 2727 |
|
| 2728 |
+
[^11]: Since there are no parameters of array type, this will only occur
|
| 2729 |
+
as the referenced type of a reference parameter.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2730 |
|
| 2731 |
+
[^12]: Calling `operator++` explicitly, as in expressions like
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2732 |
`a.operator++(2)`, has no special properties: The argument to
|
| 2733 |
`operator++` is `2`.
|