- tmp/tmpid_evar4/{from.md → to.md} +478 -261
tmp/tmpid_evar4/{from.md → to.md}
RENAMED
|
@@ -1,15 +1,17 @@
|
|
| 1 |
## Overload resolution <a id="over.match">[[over.match]]</a>
|
| 2 |
|
|
|
|
|
|
|
| 3 |
Overload resolution is a mechanism for selecting the best function to
|
| 4 |
call given a list of expressions that are to be the arguments of the
|
| 5 |
call and a set of *candidate functions* that can be called based on the
|
| 6 |
context of the call. The selection criteria for the best function are
|
| 7 |
the number of arguments, how well the arguments match the
|
| 8 |
parameter-type-list of the candidate function, how well (for non-static
|
| 9 |
-
member functions) the object matches the
|
| 10 |
-
|
| 11 |
|
| 12 |
[*Note 1*: The function selected by overload resolution is not
|
| 13 |
guaranteed to be appropriate for the context. Other restrictions, such
|
| 14 |
as the accessibility of the function, can make its use in the calling
|
| 15 |
context ill-formed. — *end note*]
|
|
@@ -59,31 +61,36 @@ resolution succeeds and the selected candidate is either not a function
|
|
| 59 |
[[over.built]], or is a function that is not deleted and is accessible
|
| 60 |
from the context in which overload resolution was performed.
|
| 61 |
|
| 62 |
### Candidate functions and argument lists <a id="over.match.funcs">[[over.match.funcs]]</a>
|
| 63 |
|
|
|
|
|
|
|
| 64 |
The subclauses of [[over.match.funcs]] describe the set of candidate
|
| 65 |
functions and the argument list submitted to overload resolution in each
|
| 66 |
context in which overload resolution is used. The source transformations
|
| 67 |
and constructions defined in these subclauses are only for the purpose
|
| 68 |
of describing the overload resolution process. An implementation is not
|
| 69 |
required to use such transformations and constructions.
|
| 70 |
|
| 71 |
The set of candidate functions can contain both member and non-member
|
| 72 |
-
functions to be resolved against the same argument list.
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
Similarly, when appropriate, the context can construct an argument list
|
| 81 |
that contains an *implied object argument* as the first argument in the
|
| 82 |
list to denote the object to be operated on.
|
| 83 |
|
| 84 |
-
For
|
| 85 |
parameter is
|
| 86 |
|
| 87 |
- “lvalue reference to cv `X`” for functions declared without a
|
| 88 |
*ref-qualifier* or with the `&` *ref-qualifier*
|
| 89 |
- “rvalue reference to cv `X`” for functions declared with the `&&`
|
|
@@ -91,44 +98,46 @@ parameter is
|
|
| 91 |
|
| 92 |
where `X` is the class of which the function is a member and cv is the
|
| 93 |
cv-qualification on the member function declaration.
|
| 94 |
|
| 95 |
[*Example 1*: For a `const` member function of class `X`, the extra
|
| 96 |
-
parameter is assumed to have type “reference to
|
| 97 |
`const X`”. — *end example*]
|
| 98 |
|
| 99 |
-
For conversion functions
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
| 107 |
|
| 108 |
[*Note 1*: No actual type is established for the implicit object
|
| 109 |
parameter of a static member function, and no attempt will be made to
|
| 110 |
determine a conversion sequence for that parameter
|
| 111 |
[[over.match.best]]. — *end note*]
|
| 112 |
|
| 113 |
During overload resolution, the implied object argument is
|
| 114 |
indistinguishable from other arguments. The implicit object parameter,
|
| 115 |
however, retains its identity since no user-defined conversions can be
|
| 116 |
-
applied to achieve a type match with it. For
|
| 117 |
-
declared without a *ref-qualifier*, even if the implicit
|
| 118 |
-
parameter is not const-qualified, an rvalue can be bound to the
|
| 119 |
parameter as long as in all other respects the argument can be converted
|
| 120 |
to the type of the implicit object parameter.
|
| 121 |
|
| 122 |
[*Note 2*: The fact that such an argument is an rvalue does not affect
|
| 123 |
the ranking of implicit conversion sequences
|
| 124 |
[[over.ics.rank]]. — *end note*]
|
| 125 |
|
| 126 |
Because other than in list-initialization only one user-defined
|
| 127 |
conversion is allowed in an implicit conversion sequence, special rules
|
| 128 |
-
apply when selecting the best user-defined conversion
|
| 129 |
-
[[over.match.best]], [[over.best.ics]]
|
| 130 |
|
| 131 |
[*Example 2*:
|
| 132 |
|
| 133 |
``` cpp
|
| 134 |
class T {
|
|
@@ -143,33 +152,57 @@ public:
|
|
| 143 |
T a = 1; // error: no viable conversion (T(C(1)) not considered)
|
| 144 |
```
|
| 145 |
|
| 146 |
— *end example*]
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
In each case where a candidate is a function template, candidate
|
| 149 |
function template specializations are generated using template argument
|
| 150 |
-
deduction
|
| 151 |
-
|
| 152 |
*constant-expression* is value-dependent [[temp.dep]], template argument
|
| 153 |
-
deduction is performed first and then, if the context
|
| 154 |
-
|
| 155 |
explicit [[dcl.fct.spec]], it will be removed from the candidate set.
|
| 156 |
Those candidates are then handled as candidate functions in the usual
|
| 157 |
-
way.[^
|
| 158 |
-
also to a set of non-template functions. In such a case, the candidate
|
| 159 |
-
functions generated from each function template are combined with the
|
| 160 |
-
set of non-template candidate functions.
|
| 161 |
|
| 162 |
-
A
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
[*Example 3*:
|
| 173 |
|
| 174 |
``` cpp
|
| 175 |
struct A {
|
|
@@ -192,10 +225,12 @@ B b3 = C(); // calls #4
|
|
| 192 |
|
| 193 |
— *end example*]
|
| 194 |
|
| 195 |
#### Function call syntax <a id="over.match.call">[[over.match.call]]</a>
|
| 196 |
|
|
|
|
|
|
|
| 197 |
In a function call [[expr.call]]
|
| 198 |
|
| 199 |
``` bnf
|
| 200 |
postfix-expression '(' expression-listₒₚₜ ')'
|
| 201 |
```
|
|
@@ -205,24 +240,27 @@ template, overload resolution is applied as specified in
|
|
| 205 |
[[over.call.func]]. If the *postfix-expression* denotes an object of
|
| 206 |
class type, overload resolution is applied as specified in
|
| 207 |
[[over.call.object]].
|
| 208 |
|
| 209 |
If the *postfix-expression* is the address of an overload set, overload
|
| 210 |
-
resolution is applied using that set as described above.
|
| 211 |
-
selected by overload resolution is a non-static member function, the
|
| 212 |
-
program is ill-formed.
|
| 213 |
|
| 214 |
-
[*Note 1*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
contexts is described in [[over.over]]. — *end note*]
|
| 216 |
|
| 217 |
##### Call to named function <a id="over.call.func">[[over.call.func]]</a>
|
| 218 |
|
| 219 |
Of interest in [[over.call.func]] are only those function calls in
|
| 220 |
-
which the *postfix-expression* ultimately contains
|
| 221 |
-
one or more functions
|
| 222 |
-
|
| 223 |
-
forms:
|
| 224 |
|
| 225 |
``` bnf
|
| 226 |
postfix-expression:
|
| 227 |
postfix-expression '.' id-expression
|
| 228 |
postfix-expression '->' id-expression
|
|
@@ -230,52 +268,94 @@ postfix-expression:
|
|
| 230 |
```
|
| 231 |
|
| 232 |
These represent two syntactic subcategories of function calls: qualified
|
| 233 |
function calls and unqualified function calls.
|
| 234 |
|
| 235 |
-
In qualified function calls, the
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
##### Call to object of class type <a id="over.call.object">[[over.call.object]]</a>
|
| 271 |
|
| 272 |
If the *postfix-expression* `E` in the function call syntax evaluates to
|
| 273 |
a class object of type “cv `T`”, then the set of candidate functions
|
| 274 |
includes at least the function call operators of `T`. The function call
|
| 275 |
-
operators of `T` are
|
| 276 |
-
|
| 277 |
|
| 278 |
In addition, for each non-explicit conversion function declared in `T`
|
| 279 |
of the form
|
| 280 |
|
| 281 |
``` bnf
|
|
@@ -297,27 +377,23 @@ returning `R`”, a *surrogate call function* with the unique name
|
|
| 297 |
|
| 298 |
is also considered as a candidate function. Similarly, surrogate call
|
| 299 |
functions are added to the set of candidate functions for each
|
| 300 |
non-explicit conversion function declared in a base class of `T`
|
| 301 |
provided the function is not hidden within `T` by another intervening
|
| 302 |
-
declaration.[^
|
| 303 |
|
| 304 |
The argument list submitted to overload resolution consists of the
|
| 305 |
argument expressions present in the function call syntax preceded by the
|
| 306 |
implied object argument `(E)`.
|
| 307 |
|
| 308 |
-
[*Note
|
| 309 |
-
the implied object argument is compared against the
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
conversion function from which the surrogate call function was derived
|
| 314 |
-
will be used in the conversion sequence for that parameter since it
|
| 315 |
-
converts the implied object argument to the appropriate function pointer
|
| 316 |
-
or reference required by that first parameter. — *end note*]
|
| 317 |
|
| 318 |
-
[*Example
|
| 319 |
|
| 320 |
``` cpp
|
| 321 |
int f1(int);
|
| 322 |
int f2(float);
|
| 323 |
typedef int (*fp1)(int);
|
|
@@ -363,11 +439,11 @@ void f() {
|
|
| 363 |
```
|
| 364 |
|
| 365 |
— *end example*]
|
| 366 |
|
| 367 |
If either operand has a type that is a class or an enumeration, a
|
| 368 |
-
user-defined operator function
|
| 369 |
operator or a user-defined conversion can be necessary to convert the
|
| 370 |
operand to a type that is appropriate for a built-in operator. In this
|
| 371 |
case, overload resolution is used to determine which operator function
|
| 372 |
or built-in operator is to be invoked to implement the operator.
|
| 373 |
Therefore, the operator notation is first transformed to the equivalent
|
|
@@ -393,21 +469,21 @@ binary operator `@` with a left operand of type *cv1* `T1` and a right
|
|
| 393 |
operand of type *cv2* `T2`, four sets of candidate functions, designated
|
| 394 |
*member candidates*, *non-member candidates*, *built-in candidates*, and
|
| 395 |
*rewritten candidates*, are constructed as follows:
|
| 396 |
|
| 397 |
- If `T1` is a complete class type or a class currently being defined,
|
| 398 |
-
the set of member candidates is the result of
|
| 399 |
-
`T1
|
| 400 |
-
|
| 401 |
-
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
[[basic.lookup.argdep]]
|
| 405 |
-
However, if no operand has a class type, only those
|
| 406 |
-
functions in the lookup set that have a first parameter of
|
| 407 |
-
or “reference to cv `T1`”, when `T1` is an enumeration type,
|
| 408 |
-
there is a right operand) a second parameter of type `T2` or
|
| 409 |
“reference to cv `T2`”, when `T2` is an enumeration type, are
|
| 410 |
candidate functions.
|
| 411 |
- For the operator `,`, the unary operator `&`, or the operator `->`,
|
| 412 |
the built-in candidates set is empty. For all other operators, the
|
| 413 |
built-in candidates include all of the candidate operator functions
|
|
@@ -415,32 +491,76 @@ operand of type *cv2* `T2`, four sets of candidate functions, designated
|
|
| 415 |
- have the same operator name, and
|
| 416 |
- accept the same number of operands, and
|
| 417 |
- accept operand types to which the given operand or operands can be
|
| 418 |
converted according to [[over.best.ics]], and
|
| 419 |
- do not have the same parameter-type-list as any non-member candidate
|
| 420 |
-
that is not a function template
|
|
|
|
| 421 |
- The rewritten candidate set is determined as follows:
|
| 422 |
- For the relational [[expr.rel]] operators, the rewritten candidates
|
| 423 |
include all non-rewritten candidates for the expression `x <=> y`.
|
| 424 |
- For the relational [[expr.rel]] and three-way comparison
|
| 425 |
[[expr.spaceship]] operators, the rewritten candidates also include
|
| 426 |
a synthesized candidate, with the order of the two parameters
|
| 427 |
reversed, for each non-rewritten candidate for the expression
|
| 428 |
`y <=> x`.
|
| 429 |
- For the `!=` operator [[expr.eq]], the rewritten candidates include
|
| 430 |
-
all non-rewritten candidates for the expression `x == y`
|
|
|
|
| 431 |
- For the equality operators, the rewritten candidates also include a
|
| 432 |
synthesized candidate, with the order of the two parameters
|
| 433 |
reversed, for each non-rewritten candidate for the expression
|
| 434 |
-
`y == x`.
|
| 435 |
- For all other operators, the rewritten candidate set is empty.
|
| 436 |
|
| 437 |
\[*Note 2*: A candidate synthesized from a member candidate has its
|
| 438 |
-
|
| 439 |
-
|
| 440 |
parameter. — *end note*]
|
| 441 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
For the built-in assignment operators, conversions of the left operand
|
| 443 |
are restricted as follows:
|
| 444 |
|
| 445 |
- no temporaries are introduced to hold the left operand, and
|
| 446 |
- no user-defined conversions are applied to the left operand to achieve
|
|
@@ -453,13 +573,13 @@ The set of candidate functions for overload resolution for some operator
|
|
| 453 |
the built-in candidates, and the rewritten candidates for that operator
|
| 454 |
`@`.
|
| 455 |
|
| 456 |
The argument list contains all of the operands of the operator. The best
|
| 457 |
function from the set of candidate functions is selected according to
|
| 458 |
-
[[over.match.viable]] and [[over.match.best]].[^
|
| 459 |
|
| 460 |
-
[*Example
|
| 461 |
|
| 462 |
``` cpp
|
| 463 |
struct A {
|
| 464 |
operator int();
|
| 465 |
};
|
|
@@ -497,11 +617,11 @@ of the selected operation function, except that the second standard
|
|
| 497 |
conversion sequence of a user-defined conversion sequence
|
| 498 |
[[over.ics.user]] is not applied. Then the operator is treated as the
|
| 499 |
corresponding built-in operator and interpreted according to
|
| 500 |
[[expr.compound]].
|
| 501 |
|
| 502 |
-
[*Example
|
| 503 |
|
| 504 |
``` cpp
|
| 505 |
struct X {
|
| 506 |
operator double();
|
| 507 |
};
|
|
@@ -517,11 +637,11 @@ int *b = Y() + X(); // error: pointer arithmetic requires integral o
|
|
| 517 |
— *end example*]
|
| 518 |
|
| 519 |
The second operand of operator `->` is ignored in selecting an
|
| 520 |
`operator->` function, and is not an argument when the `operator->`
|
| 521 |
function is called. When `operator->` returns, the operator `->` is
|
| 522 |
-
applied to the value returned, with the original second operand.[^
|
| 523 |
|
| 524 |
If the operator is the operator `,`, the unary operator `&`, or the
|
| 525 |
operator `->`, and there are no viable functions, then the operator is
|
| 526 |
assumed to be the built-in operator and interpreted according to
|
| 527 |
[[expr.compound]].
|
|
@@ -543,11 +663,11 @@ struct B {
|
|
| 543 |
|
| 544 |
A a;
|
| 545 |
|
| 546 |
void B::f() {
|
| 547 |
operator+ (a,a); // error: global operator hidden by member
|
| 548 |
-
a + a; // OK
|
| 549 |
}
|
| 550 |
```
|
| 551 |
|
| 552 |
— *end note*]
|
| 553 |
|
|
@@ -581,90 +701,75 @@ Assuming that “*cv1* `T`” is the type of the object being initialized,
|
|
| 581 |
with `T` a class type, the candidate functions are selected as follows:
|
| 582 |
|
| 583 |
- The converting constructors [[class.conv.ctor]] of `T` are candidate
|
| 584 |
functions.
|
| 585 |
- When the type of the initializer expression is a class type “cv `S`”,
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
class thereof are candidate functions. A call to a conversion function
|
| 595 |
-
returning “reference to `X`” is a glvalue of type `X`, and such a
|
| 596 |
-
conversion function is therefore considered to yield `X` for this
|
| 597 |
-
process of selecting candidate functions.
|
| 598 |
|
| 599 |
In both cases, the argument list has one argument, which is the
|
| 600 |
initializer expression.
|
| 601 |
|
| 602 |
[*Note 2*: This argument will be compared against the first parameter
|
| 603 |
-
of the constructors and against the
|
| 604 |
-
|
| 605 |
|
| 606 |
#### Initialization by conversion function <a id="over.match.conv">[[over.match.conv]]</a>
|
| 607 |
|
| 608 |
Under the conditions specified in [[dcl.init]], as part of an
|
| 609 |
initialization of an object of non-class type, a conversion function can
|
| 610 |
be invoked to convert an initializer expression of class type to the
|
| 611 |
type of the object being initialized. Overload resolution is used to
|
| 612 |
-
select the conversion function to be invoked. Assuming that “
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
functions are selected as follows:
|
| 616 |
|
| 617 |
-
- The
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
converted to type `T` with a qualification conversion [[conv.qual]]
|
| 624 |
-
are also candidate functions. Conversion functions that return a
|
| 625 |
-
cv-qualified type are considered to yield the cv-unqualified version
|
| 626 |
-
of that type for this process of selecting candidate functions. A call
|
| 627 |
-
to a conversion function returning “reference to `X`” is a glvalue of
|
| 628 |
-
type `X`, and such a conversion function is therefore considered to
|
| 629 |
-
yield `X` for this process of selecting candidate functions.
|
| 630 |
|
| 631 |
The argument list has one argument, which is the initializer expression.
|
| 632 |
|
| 633 |
-
[*Note 1*: This argument will be compared against the
|
| 634 |
-
|
| 635 |
|
| 636 |
#### Initialization by conversion function for direct reference binding <a id="over.match.ref">[[over.match.ref]]</a>
|
| 637 |
|
| 638 |
Under the conditions specified in [[dcl.init.ref]], a reference can be
|
| 639 |
bound directly to the result of applying a conversion function to an
|
| 640 |
initializer expression. Overload resolution is used to select the
|
| 641 |
conversion function to be invoked. Assuming that “reference to *cv1*
|
| 642 |
-
`T`” is the type of the reference being initialized,
|
| 643 |
-
type of the initializer expression, with `S` a class type, the candidate
|
| 644 |
functions are selected as follows:
|
| 645 |
|
| 646 |
-
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
functions
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
type `T` with a qualification conversion [[conv.qual]], are also
|
| 660 |
-
candidate functions.
|
| 661 |
|
| 662 |
The argument list has one argument, which is the initializer expression.
|
| 663 |
|
| 664 |
-
[*Note 1*: This argument will be compared against the
|
| 665 |
-
|
| 666 |
|
| 667 |
#### Initialization by list-initialization <a id="over.match.list">[[over.match.list]]</a>
|
| 668 |
|
| 669 |
When objects of non-aggregate class type `T` are list-initialized such
|
| 670 |
that [[dcl.init.list]] specifies that overload resolution is performed
|
|
@@ -683,15 +788,15 @@ resolution selects the constructor in two phases:
|
|
| 683 |
consists of the elements of the initializer list.
|
| 684 |
|
| 685 |
In copy-list-initialization, if an explicit constructor is chosen, the
|
| 686 |
initialization is ill-formed.
|
| 687 |
|
| 688 |
-
[*Note 1*: This differs from other situations
|
| 689 |
-
[[over.match.copy]]
|
| 690 |
-
for copy-initialization. This restriction
|
| 691 |
-
initialization is part of the final result of
|
| 692 |
-
resolution. — *end note*]
|
| 693 |
|
| 694 |
#### Class template argument deduction <a id="over.match.class.deduct">[[over.match.class.deduct]]</a>
|
| 695 |
|
| 696 |
When resolving a placeholder for a deduced class type
|
| 697 |
[[dcl.type.class.deduct]] where the *template-name* names a primary
|
|
@@ -730,35 +835,112 @@ the elements of the *initializer-list* or *designated-initializer-list*
|
|
| 730 |
of the *braced-init-list*, or of the *expression-list*. For each xᵢ, let
|
| 731 |
eᵢ be the corresponding aggregate element of `C` or of one of its
|
| 732 |
(possibly recursive) subaggregates that would be initialized by xᵢ
|
| 733 |
[[dcl.init.aggr]] if
|
| 734 |
|
| 735 |
-
- brace elision is not considered for any aggregate element that has
|
| 736 |
-
dependent non-array type
|
| 737 |
-
bound,
|
|
|
|
|
|
|
| 738 |
- each non-trailing aggregate element that is a pack expansion is
|
| 739 |
assumed to correspond to no elements of the initializer list, and
|
| 740 |
- a trailing aggregate element that is a pack expansion is assumed to
|
| 741 |
correspond to all remaining elements of the initializer list (if any).
|
| 742 |
|
| 743 |
If there is no such aggregate element eᵢ for any xᵢ, the aggregate
|
| 744 |
deduction candidate is not added to the set. The aggregate deduction
|
| 745 |
candidate is derived as above from a hypothetical constructor
|
| 746 |
`C`(`T₁`, …, `Tₙ`), where
|
| 747 |
|
| 748 |
-
- if eᵢ is of array type and xᵢ is a *braced-init-list*
|
| 749 |
-
|
| 750 |
-
|
|
|
|
| 751 |
- otherwise, `Tᵢ` is the declared type of eᵢ,
|
| 752 |
|
| 753 |
except that additional parameter packs of the form `Pⱼ` `...` are
|
| 754 |
inserted into the parameter list in their original aggregate element
|
| 755 |
position corresponding to each non-trailing aggregate element of type
|
| 756 |
`Pⱼ` that was skipped because it was a parameter pack, and the trailing
|
| 757 |
sequence of parameters corresponding to a trailing aggregate element
|
| 758 |
that is a pack expansion (if any) is replaced by a single parameter of
|
| 759 |
-
the form `Tₙ` `...`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 760 |
|
| 761 |
When resolving a placeholder for a deduced class type
|
| 762 |
[[dcl.type.simple]] where the *template-name* names an alias template
|
| 763 |
`A`, the *defining-type-id* of `A` must be of the form
|
| 764 |
|
|
@@ -770,14 +952,16 @@ as specified in [[dcl.type.simple]]. The guides of `A` are the set of
|
|
| 770 |
functions or function templates formed as follows. For each function or
|
| 771 |
function template `f` in the guides of the template named by the
|
| 772 |
*simple-template-id* of the *defining-type-id*, the template arguments
|
| 773 |
of the return type of `f` are deduced from the *defining-type-id* of `A`
|
| 774 |
according to the process in [[temp.deduct.type]] with the exception that
|
| 775 |
-
deduction does not fail if not all template arguments are deduced.
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
|
|
|
|
|
|
| 779 |
|
| 780 |
- The function type of `f'` is the function type of `g`.
|
| 781 |
- If `f` is a function template, `f'` is a function template whose
|
| 782 |
template parameter list consists of all the template parameters of `A`
|
| 783 |
(including their default template arguments) that appear in the above
|
|
@@ -787,14 +971,14 @@ the following properties and add it to the set of guides of `A`:
|
|
| 787 |
function template.
|
| 788 |
- The associated constraints [[temp.constr.decl]] are the conjunction of
|
| 789 |
the associated constraints of `g` and a constraint that is satisfied
|
| 790 |
if and only if the arguments of `A` are deducible (see below) from the
|
| 791 |
return type.
|
| 792 |
-
- If `f` is a copy deduction candidate
|
| 793 |
-
|
| 794 |
-
- If `f` was generated from a *deduction-guide*
|
| 795 |
-
|
| 796 |
- The *explicit-specifier* of `f'` is the *explicit-specifier* of `g`
|
| 797 |
(if any).
|
| 798 |
|
| 799 |
The arguments of a template `A` are said to be deducible from a type `T`
|
| 800 |
if, given a class template
|
|
@@ -832,11 +1016,11 @@ If the function or function template was generated from a constructor or
|
|
| 832 |
*deduction-guide* that had an *explicit-specifier*, each such notional
|
| 833 |
constructor is considered to have that same *explicit-specifier*. All
|
| 834 |
such notional constructors are considered to be public members of the
|
| 835 |
hypothetical class type.
|
| 836 |
|
| 837 |
-
[*Example
|
| 838 |
|
| 839 |
``` cpp
|
| 840 |
template <class T> struct A {
|
| 841 |
explicit A(const T&, ...) noexcept; // #1
|
| 842 |
A(T&&, ...); // #2
|
|
@@ -914,11 +1098,11 @@ F f2 = {Types<X, Y, Z>{}, X{}, Y{}}; // OK, F<X, Y, Z> deduced
|
|
| 914 |
F f3 = {Types<X, Y, Z>{}, X{}, W{}}; // error: conflicting types deduced; operator Y not considered
|
| 915 |
```
|
| 916 |
|
| 917 |
— *end example*]
|
| 918 |
|
| 919 |
-
[*Example
|
| 920 |
|
| 921 |
``` cpp
|
| 922 |
template <class T, class U> struct C {
|
| 923 |
C(T, U); // #1
|
| 924 |
};
|
|
@@ -945,11 +1129,11 @@ Possible exposition-only implementation of the above procedure:
|
|
| 945 |
template <class> class AA;
|
| 946 |
template <class V> class AA<A<V>> { };
|
| 947 |
template <class T> concept deduces_A = requires { sizeof(AA<T>); };
|
| 948 |
|
| 949 |
// f1 is formed from the constructor #1 of C, generating the following function template
|
| 950 |
-
template<T, U>
|
| 951 |
auto f1(T, U) -> C<T, U>;
|
| 952 |
|
| 953 |
// Deducing arguments for C<T, U> from C<V *, V*> deduces T as V * and U as V *;
|
| 954 |
// f1' is obtained by transforming f1 as described by the above procedure.
|
| 955 |
template<class V> requires deduces_A<C<V *, V *>>
|
|
@@ -993,64 +1177,60 @@ associated constraints, if any, and relationships between arguments and
|
|
| 993 |
function parameters other than the ranking of conversion sequences.
|
| 994 |
|
| 995 |
First, to be a viable function, a candidate function shall have enough
|
| 996 |
parameters to agree in number with the arguments in the list.
|
| 997 |
|
| 998 |
-
- If there are
|
| 999 |
-
exactly
|
| 1000 |
-
- A candidate function having fewer than
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
corresponding parameter is considered to “match the ellipsis”
|
| 1004 |
[[over.ics.ellipsis]].
|
| 1005 |
-
- A candidate function having more than
|
| 1006 |
all parameters following the mᵗʰ have default arguments
|
| 1007 |
[[dcl.fct.default]]. For the purposes of overload resolution, the
|
| 1008 |
-
parameter list is truncated on the right, so that there are exactly
|
| 1009 |
-
|
| 1010 |
|
| 1011 |
Second, for a function to be viable, if it has associated constraints
|
| 1012 |
[[temp.constr.decl]], those constraints shall be satisfied
|
| 1013 |
[[temp.constr.constr]].
|
| 1014 |
|
| 1015 |
Third, for `F` to be a viable function, there shall exist for each
|
| 1016 |
argument an implicit conversion sequence [[over.best.ics]] that converts
|
| 1017 |
that argument to the corresponding parameter of `F`. If the parameter
|
| 1018 |
has reference type, the implicit conversion sequence includes the
|
| 1019 |
operation of binding the reference, and the fact that an lvalue
|
| 1020 |
-
reference to non-`const` cannot
|
| 1021 |
-
reference cannot
|
| 1022 |
function (see [[over.ics.ref]]).
|
| 1023 |
|
| 1024 |
### Best viable function <a id="over.match.best">[[over.match.best]]</a>
|
| 1025 |
|
| 1026 |
-
|
| 1027 |
|
| 1028 |
-
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
worse than ICS*1*(`F`);[^8] otherwise,
|
| 1032 |
-
- let ICS*i*(`F`) denote the implicit conversion sequence that converts
|
| 1033 |
-
the *i*-th argument in the list to the type of the *i*-th parameter of
|
| 1034 |
-
viable function `F`. [[over.best.ics]] defines the implicit conversion
|
| 1035 |
sequences and [[over.ics.rank]] defines what it means for one implicit
|
| 1036 |
conversion sequence to be a better conversion sequence or worse
|
| 1037 |
conversion sequence than another.
|
| 1038 |
|
| 1039 |
-
Given these definitions, a viable function `
|
| 1040 |
-
*better* function than another viable function `
|
| 1041 |
-
|
| 1042 |
-
|
| 1043 |
|
| 1044 |
-
- for some argument
|
| 1045 |
-
|
| 1046 |
- the context is an initialization by user-defined conversion (see
|
| 1047 |
[[dcl.init]], [[over.match.conv]], and [[over.match.ref]]) and the
|
| 1048 |
-
standard conversion sequence from the return type of `
|
| 1049 |
destination type (i.e., the type of the entity being initialized) is a
|
| 1050 |
better conversion sequence than the standard conversion sequence from
|
| 1051 |
-
the return type of `
|
| 1052 |
\[*Example 1*:
|
| 1053 |
``` cpp
|
| 1054 |
struct A {
|
| 1055 |
A();
|
| 1056 |
operator int();
|
|
@@ -1093,11 +1273,11 @@ and then
|
|
| 1093 |
parameter-type-lists, and `F1` is more constrained than `F2` according
|
| 1094 |
to the partial ordering of constraints described in
|
| 1095 |
[[temp.constr.order]], or if not that,
|
| 1096 |
- `F1` is a constructor for a class `D`, `F2` is a constructor for a
|
| 1097 |
base class `B` of `D`, and for all arguments the corresponding
|
| 1098 |
-
parameters of `F1` and `F2` have the same type
|
| 1099 |
\[*Example 3*:
|
| 1100 |
``` cpp
|
| 1101 |
struct A {
|
| 1102 |
A(int = 0);
|
| 1103 |
};
|
|
@@ -1137,10 +1317,16 @@ and then
|
|
| 1137 |
bool b = 1 < S(); // calls #2
|
| 1138 |
```
|
| 1139 |
|
| 1140 |
— *end example*]
|
| 1141 |
or, if not that
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1142 |
- `F1` is generated from a *deduction-guide* [[over.match.class.deduct]]
|
| 1143 |
and `F2` is not, or, if not that,
|
| 1144 |
- `F1` is the copy deduction candidate [[over.match.class.deduct]] and
|
| 1145 |
`F2` is not, or, if not that,
|
| 1146 |
- `F1` is generated from a non-template constructor and `F2` is
|
|
@@ -1173,11 +1359,11 @@ and then
|
|
| 1173 |
|
| 1174 |
— *end example*]
|
| 1175 |
|
| 1176 |
If there is exactly one viable function that is a better function than
|
| 1177 |
all other viable functions, then it is the one selected by overload
|
| 1178 |
-
resolution; otherwise the call is ill-formed.[^
|
| 1179 |
|
| 1180 |
[*Example 7*:
|
| 1181 |
|
| 1182 |
``` cpp
|
| 1183 |
void Fcn(const int*, short);
|
|
@@ -1192,21 +1378,20 @@ void f() {
|
|
| 1192 |
|
| 1193 |
Fcn(&i, 1L); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
| 1194 |
// and 1L → short and 1L → int are indistinguishable
|
| 1195 |
|
| 1196 |
Fcn(&i, 'c'); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
| 1197 |
-
// and c → int is better than c → short
|
| 1198 |
}
|
| 1199 |
```
|
| 1200 |
|
| 1201 |
— *end example*]
|
| 1202 |
|
| 1203 |
If the best viable function resolves to a function for which multiple
|
| 1204 |
-
declarations were found, and if
|
| 1205 |
-
|
| 1206 |
-
|
| 1207 |
-
ill-formed.
|
| 1208 |
|
| 1209 |
[*Example 8*:
|
| 1210 |
|
| 1211 |
``` cpp
|
| 1212 |
namespace A {
|
|
@@ -1227,16 +1412,18 @@ void use() {
|
|
| 1227 |
|
| 1228 |
— *end example*]
|
| 1229 |
|
| 1230 |
#### Implicit conversion sequences <a id="over.best.ics">[[over.best.ics]]</a>
|
| 1231 |
|
|
|
|
|
|
|
| 1232 |
An *implicit conversion sequence* is a sequence of conversions used to
|
| 1233 |
convert an argument in a function call to the type of the corresponding
|
| 1234 |
parameter of the function being called. The sequence of conversions is
|
| 1235 |
an implicit conversion as defined in [[conv]], which means it is
|
| 1236 |
governed by the rules for initialization of an object or reference by a
|
| 1237 |
-
single expression
|
| 1238 |
|
| 1239 |
Implicit conversion sequences are concerned only with the type,
|
| 1240 |
cv-qualification, and value category of the argument and how these are
|
| 1241 |
converted to match the corresponding properties of the parameter.
|
| 1242 |
|
|
@@ -1256,11 +1443,11 @@ forms:
|
|
| 1256 |
- an ellipsis conversion sequence [[over.ics.ellipsis]].
|
| 1257 |
|
| 1258 |
However, if the target is
|
| 1259 |
|
| 1260 |
- the first parameter of a constructor or
|
| 1261 |
-
- the
|
| 1262 |
|
| 1263 |
and the constructor or user-defined conversion function is a candidate
|
| 1264 |
by
|
| 1265 |
|
| 1266 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
|
@@ -1317,22 +1504,29 @@ case is the identity sequence; it contains no “conversion” from
|
|
| 1317 |
|
| 1318 |
When the parameter has a class type and the argument expression has the
|
| 1319 |
same type, the implicit conversion sequence is an identity conversion.
|
| 1320 |
When the parameter has a class type and the argument expression has a
|
| 1321 |
derived class type, the implicit conversion sequence is a
|
| 1322 |
-
derived-to-base conversion from the derived class to the base class.
|
|
|
|
| 1323 |
|
| 1324 |
[*Note 4*: There is no such standard conversion; this derived-to-base
|
| 1325 |
conversion exists only in the description of implicit conversion
|
| 1326 |
sequences. — *end note*]
|
| 1327 |
|
| 1328 |
-
|
|
|
|
|
|
|
|
|
|
| 1329 |
|
| 1330 |
In all contexts, when converting to the implicit object parameter or
|
| 1331 |
when converting to the left operand of an assignment operation only
|
| 1332 |
standard conversion sequences are allowed.
|
| 1333 |
|
|
|
|
|
|
|
|
|
|
| 1334 |
If no conversions are required to match an argument to a parameter type,
|
| 1335 |
the implicit conversion sequence is the standard conversion sequence
|
| 1336 |
consisting of the identity conversion [[over.ics.scs]].
|
| 1337 |
|
| 1338 |
If no sequence of conversions can be found to convert an argument to a
|
|
@@ -1345,11 +1539,11 @@ conversion sequence designated the *ambiguous conversion sequence*. For
|
|
| 1345 |
the purpose of ranking implicit conversion sequences as described in
|
| 1346 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 1347 |
user-defined conversion sequence that is indistinguishable from any
|
| 1348 |
other user-defined conversion sequence.
|
| 1349 |
|
| 1350 |
-
[*Note
|
| 1351 |
|
| 1352 |
This rule prevents a function from becoming non-viable because of an
|
| 1353 |
ambiguous conversion sequence for one of its parameters.
|
| 1354 |
|
| 1355 |
[*Example 3*:
|
|
@@ -1383,19 +1577,19 @@ defined in the following subclauses.
|
|
| 1383 |
|
| 1384 |
summarizes the conversions defined in [[conv]] and partitions them into
|
| 1385 |
four disjoint categories: Lvalue Transformation, Qualification
|
| 1386 |
Adjustment, Promotion, and Conversion.
|
| 1387 |
|
| 1388 |
-
[*Note
|
| 1389 |
category, cv-qualification, and data representation: the Lvalue
|
| 1390 |
Transformations do not change the cv-qualification or data
|
| 1391 |
representation of the type; the Qualification Adjustments do not change
|
| 1392 |
the value category or data representation of the type; and the
|
| 1393 |
Promotions and Conversions do not change the value category or
|
| 1394 |
cv-qualification of the type. — *end note*]
|
| 1395 |
|
| 1396 |
-
[*Note
|
| 1397 |
either is the Identity conversion by itself (that is, no conversion) or
|
| 1398 |
consists of one to three conversions from the other four categories. If
|
| 1399 |
there are two or more conversions in the sequence, the conversions are
|
| 1400 |
applied in the canonical order: **Lvalue Transformation**, **Promotion**
|
| 1401 |
or **Conversion**, **Qualification Adjustment**. — *end note*]
|
|
@@ -1423,14 +1617,14 @@ Promotion rank; otherwise, the sequence has Exact Match rank.
|
|
| 1423 |
A *user-defined conversion sequence* consists of an initial standard
|
| 1424 |
conversion sequence followed by a user-defined conversion [[class.conv]]
|
| 1425 |
followed by a second standard conversion sequence. If the user-defined
|
| 1426 |
conversion is specified by a constructor [[class.conv.ctor]], the
|
| 1427 |
initial standard conversion sequence converts the source type to the
|
| 1428 |
-
type
|
| 1429 |
conversion is specified by a conversion function [[class.conv.fct]], the
|
| 1430 |
initial standard conversion sequence converts the source type to the
|
| 1431 |
-
|
| 1432 |
|
| 1433 |
The second standard conversion sequence converts the result of the
|
| 1434 |
user-defined conversion to the target type for the sequence; any
|
| 1435 |
reference binding is included in the second standard conversion
|
| 1436 |
sequence. Since an implicit conversion sequence is an initialization,
|
|
@@ -1458,11 +1652,11 @@ function called (see [[expr.call]]).
|
|
| 1458 |
|
| 1459 |
When a parameter of reference type binds directly [[dcl.init.ref]] to an
|
| 1460 |
argument expression, the implicit conversion sequence is the identity
|
| 1461 |
conversion, unless the argument expression has a type that is a derived
|
| 1462 |
class of the parameter type, in which case the implicit conversion
|
| 1463 |
-
sequence is a derived-to-base
|
| 1464 |
|
| 1465 |
[*Example 4*:
|
| 1466 |
|
| 1467 |
``` cpp
|
| 1468 |
struct A {};
|
|
@@ -1474,12 +1668,12 @@ int i = f(b); // calls f(B&), an exact match, rather than f(A&), a convers
|
|
| 1474 |
|
| 1475 |
— *end example*]
|
| 1476 |
|
| 1477 |
If the parameter binds directly to the result of applying a conversion
|
| 1478 |
function to the argument expression, the implicit conversion sequence is
|
| 1479 |
-
a user-defined conversion sequence [[over.ics.user]]
|
| 1480 |
-
standard conversion sequence either an identity conversion or, if the
|
| 1481 |
conversion function returns an entity of a type that is a derived class
|
| 1482 |
of the parameter type, a derived-to-base conversion.
|
| 1483 |
|
| 1484 |
When a parameter of reference type is not bound directly to an argument
|
| 1485 |
expression, the conversion sequence is the one required to convert the
|
|
@@ -1493,11 +1687,11 @@ Except for an implicit object parameter, for which see
|
|
| 1493 |
[[over.match.funcs]], an implicit conversion sequence cannot be formed
|
| 1494 |
if it requires binding an lvalue reference other than a reference to a
|
| 1495 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 1496 |
an lvalue other than a function lvalue.
|
| 1497 |
|
| 1498 |
-
[*Note
|
| 1499 |
a viable function if it has a non-`const` lvalue reference parameter
|
| 1500 |
(other than the implicit object parameter) and the corresponding
|
| 1501 |
argument would require a temporary to be created to initialize the
|
| 1502 |
lvalue reference (see [[dcl.init.ref]]). — *end note*]
|
| 1503 |
|
|
@@ -1525,11 +1719,11 @@ is only possible if the parameter has an aggregate type that can be
|
|
| 1525 |
initialized from the initializer list according to the rules for
|
| 1526 |
aggregate initialization [[dcl.init.aggr]], in which case the implicit
|
| 1527 |
conversion sequence is a user-defined conversion sequence whose second
|
| 1528 |
standard conversion sequence is an identity conversion.
|
| 1529 |
|
| 1530 |
-
[*Note
|
| 1531 |
|
| 1532 |
Aggregate initialization does not require that the members are declared
|
| 1533 |
in designation order. If, after overload resolution, the order does not
|
| 1534 |
match for the selected overload, the initialization of the parameter
|
| 1535 |
will be ill-formed [[dcl.init.list]].
|
|
@@ -1558,14 +1752,15 @@ void h() {
|
|
| 1558 |
Otherwise, if the parameter type is an aggregate class `X` and the
|
| 1559 |
initializer list has a single element of type cv `U`, where `U` is `X`
|
| 1560 |
or a class derived from `X`, the implicit conversion sequence is the one
|
| 1561 |
required to convert the element to the parameter type.
|
| 1562 |
|
| 1563 |
-
Otherwise, if the parameter type is a character array
|
| 1564 |
-
|
| 1565 |
-
|
| 1566 |
-
|
|
|
|
| 1567 |
|
| 1568 |
Otherwise, if the parameter type is `std::initializer_list<X>` and all
|
| 1569 |
the elements of the initializer list can be implicitly converted to `X`,
|
| 1570 |
the implicit conversion sequence is the worst conversion necessary to
|
| 1571 |
convert an element of the list to `X`, or if the initializer list has no
|
|
@@ -1575,13 +1770,13 @@ constructor.
|
|
| 1575 |
|
| 1576 |
[*Example 7*:
|
| 1577 |
|
| 1578 |
``` cpp
|
| 1579 |
void f(std::initializer_list<int>);
|
| 1580 |
-
f( {} ); // OK
|
| 1581 |
-
f( {1,2,3} ); // OK
|
| 1582 |
-
f( {'a','b'} ); // OK
|
| 1583 |
f( {1.0} ); // error: narrowing
|
| 1584 |
|
| 1585 |
struct A {
|
| 1586 |
A(std::initializer_list<double>); // #1
|
| 1587 |
A(std::initializer_list<complex<double>>); // #2
|
|
@@ -1592,11 +1787,11 @@ A a{ 1.0,2.0 }; // OK, uses #1
|
|
| 1592 |
void g(A);
|
| 1593 |
g({ "foo", "bar" }); // OK, uses #3
|
| 1594 |
|
| 1595 |
typedef int IA[3];
|
| 1596 |
void h(const IA&);
|
| 1597 |
-
h({ 1, 2, 3 }); // OK
|
| 1598 |
```
|
| 1599 |
|
| 1600 |
— *end example*]
|
| 1601 |
|
| 1602 |
Otherwise, if the parameter type is “array of `N` `X`” or “array of
|
|
@@ -1614,11 +1809,11 @@ the argument initializer list:
|
|
| 1614 |
- If `C` is not an initializer-list constructor and the initializer list
|
| 1615 |
has a single element of type cv `U`, where `U` is `X` or a class
|
| 1616 |
derived from `X`, the implicit conversion sequence has Exact Match
|
| 1617 |
rank if `U` is `X`, or Conversion rank if `U` is derived from `X`.
|
| 1618 |
- Otherwise, the implicit conversion sequence is a user-defined
|
| 1619 |
-
conversion sequence
|
| 1620 |
identity conversion.
|
| 1621 |
|
| 1622 |
If multiple constructors are viable but none is better than the others,
|
| 1623 |
the implicit conversion sequence is the ambiguous conversion sequence.
|
| 1624 |
User-defined conversions are allowed for conversion of the initializer
|
|
@@ -1630,61 +1825,61 @@ list elements to the constructor parameter types except as noted in
|
|
| 1630 |
``` cpp
|
| 1631 |
struct A {
|
| 1632 |
A(std::initializer_list<int>);
|
| 1633 |
};
|
| 1634 |
void f(A);
|
| 1635 |
-
f( {'a', 'b'} ); // OK
|
| 1636 |
|
| 1637 |
struct B {
|
| 1638 |
B(int, double);
|
| 1639 |
};
|
| 1640 |
void g(B);
|
| 1641 |
-
g( {'a', 'b'} ); // OK
|
| 1642 |
g( {1.0, 1.0} ); // error: narrowing
|
| 1643 |
|
| 1644 |
void f(B);
|
| 1645 |
f( {'a', 'b'} ); // error: ambiguous f(A) or f(B)
|
| 1646 |
|
| 1647 |
struct C {
|
| 1648 |
C(std::string);
|
| 1649 |
};
|
| 1650 |
void h(C);
|
| 1651 |
-
h({"foo"}); // OK
|
| 1652 |
|
| 1653 |
struct D {
|
| 1654 |
D(A, C);
|
| 1655 |
};
|
| 1656 |
void i(D);
|
| 1657 |
-
i({ {1,2}, {"bar"} }); // OK
|
| 1658 |
```
|
| 1659 |
|
| 1660 |
— *end example*]
|
| 1661 |
|
| 1662 |
Otherwise, if the parameter has an aggregate type which can be
|
| 1663 |
initialized from the initializer list according to the rules for
|
| 1664 |
aggregate initialization [[dcl.init.aggr]], the implicit conversion
|
| 1665 |
-
sequence is a user-defined conversion sequence
|
| 1666 |
-
conversion sequence an identity conversion.
|
| 1667 |
|
| 1668 |
[*Example 9*:
|
| 1669 |
|
| 1670 |
``` cpp
|
| 1671 |
struct A {
|
| 1672 |
int m1;
|
| 1673 |
double m2;
|
| 1674 |
};
|
| 1675 |
|
| 1676 |
void f(A);
|
| 1677 |
-
f( {'a', 'b'} ); // OK
|
| 1678 |
f( {1.0} ); // error: narrowing
|
| 1679 |
```
|
| 1680 |
|
| 1681 |
— *end example*]
|
| 1682 |
|
| 1683 |
Otherwise, if the parameter is a reference, see [[over.ics.ref]].
|
| 1684 |
|
| 1685 |
-
[*Note
|
| 1686 |
underlying temporary for the reference. — *end note*]
|
| 1687 |
|
| 1688 |
[*Example 10*:
|
| 1689 |
|
| 1690 |
``` cpp
|
|
@@ -1692,11 +1887,11 @@ struct A {
|
|
| 1692 |
int m1;
|
| 1693 |
double m2;
|
| 1694 |
};
|
| 1695 |
|
| 1696 |
void f(const A&);
|
| 1697 |
-
f( {'a', 'b'} ); // OK
|
| 1698 |
f( {1.0} ); // error: narrowing
|
| 1699 |
|
| 1700 |
void g(const double &);
|
| 1701 |
g({1}); // same conversion as int to double
|
| 1702 |
```
|
|
@@ -1709,21 +1904,21 @@ Otherwise, if the parameter type is not a class:
|
|
| 1709 |
initializer list, the implicit conversion sequence is the one required
|
| 1710 |
to convert the element to the parameter type;
|
| 1711 |
\[*Example 11*:
|
| 1712 |
``` cpp
|
| 1713 |
void f(int);
|
| 1714 |
-
f( {'a'} ); // OK
|
| 1715 |
f( {1.0} ); // error: narrowing
|
| 1716 |
```
|
| 1717 |
|
| 1718 |
— *end example*]
|
| 1719 |
- if the initializer list has no elements, the implicit conversion
|
| 1720 |
sequence is the identity conversion.
|
| 1721 |
\[*Example 12*:
|
| 1722 |
``` cpp
|
| 1723 |
void f(int);
|
| 1724 |
-
f( { } ); // OK
|
| 1725 |
```
|
| 1726 |
|
| 1727 |
— *end example*]
|
| 1728 |
|
| 1729 |
In all cases other than those enumerated above, no conversion is
|
|
@@ -1912,19 +2107,41 @@ indistinguishable unless one of the following rules applies:
|
|
| 1912 |
- A conversion that does not convert a pointer or a pointer to member to
|
| 1913 |
`bool` is better than one that does.
|
| 1914 |
- A conversion that promotes an enumeration whose underlying type is
|
| 1915 |
fixed to its underlying type is better than one that promotes to the
|
| 1916 |
promoted underlying type, if the two are different.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1917 |
- If class `B` is derived directly or indirectly from class `A`,
|
| 1918 |
conversion of `B*` to `A*` is better than conversion of `B*` to
|
| 1919 |
`void*`, and conversion of `A*` to `void*` is better than conversion
|
| 1920 |
of `B*` to `void*`.
|
| 1921 |
- If class `B` is derived directly or indirectly from class `A` and
|
| 1922 |
class `C` is derived directly or indirectly from `B`,
|
| 1923 |
- conversion of `C*` to `B*` is better than conversion of `C*` to
|
| 1924 |
`A*`,
|
| 1925 |
-
\[*Example
|
| 1926 |
``` cpp
|
| 1927 |
struct A {};
|
| 1928 |
struct B : public A {};
|
| 1929 |
struct C : public B {};
|
| 1930 |
C* pc;
|
|
|
|
| 1 |
## Overload resolution <a id="over.match">[[over.match]]</a>
|
| 2 |
|
| 3 |
+
### General <a id="over.match.general">[[over.match.general]]</a>
|
| 4 |
+
|
| 5 |
Overload resolution is a mechanism for selecting the best function to
|
| 6 |
call given a list of expressions that are to be the arguments of the
|
| 7 |
call and a set of *candidate functions* that can be called based on the
|
| 8 |
context of the call. The selection criteria for the best function are
|
| 9 |
the number of arguments, how well the arguments match the
|
| 10 |
parameter-type-list of the candidate function, how well (for non-static
|
| 11 |
+
member functions) the object matches the object parameter, and certain
|
| 12 |
+
other properties of the candidate function.
|
| 13 |
|
| 14 |
[*Note 1*: The function selected by overload resolution is not
|
| 15 |
guaranteed to be appropriate for the context. Other restrictions, such
|
| 16 |
as the accessibility of the function, can make its use in the calling
|
| 17 |
context ill-formed. — *end note*]
|
|
|
|
| 61 |
[[over.built]], or is a function that is not deleted and is accessible
|
| 62 |
from the context in which overload resolution was performed.
|
| 63 |
|
| 64 |
### Candidate functions and argument lists <a id="over.match.funcs">[[over.match.funcs]]</a>
|
| 65 |
|
| 66 |
+
#### General <a id="over.match.funcs.general">[[over.match.funcs.general]]</a>
|
| 67 |
+
|
| 68 |
The subclauses of [[over.match.funcs]] describe the set of candidate
|
| 69 |
functions and the argument list submitted to overload resolution in each
|
| 70 |
context in which overload resolution is used. The source transformations
|
| 71 |
and constructions defined in these subclauses are only for the purpose
|
| 72 |
of describing the overload resolution process. An implementation is not
|
| 73 |
required to use such transformations and constructions.
|
| 74 |
|
| 75 |
The set of candidate functions can contain both member and non-member
|
| 76 |
+
functions to be resolved against the same argument list. If a member
|
| 77 |
+
function is
|
| 78 |
+
|
| 79 |
+
- an implicit object member function that is not a constructor, or
|
| 80 |
+
- a static member function and the argument list includes an implied
|
| 81 |
+
object argument,
|
| 82 |
+
|
| 83 |
+
it is considered to have an extra first parameter, called the
|
| 84 |
+
*implicit object parameter*, which represents the object for which the
|
| 85 |
+
member function has been called.
|
| 86 |
|
| 87 |
Similarly, when appropriate, the context can construct an argument list
|
| 88 |
that contains an *implied object argument* as the first argument in the
|
| 89 |
list to denote the object to be operated on.
|
| 90 |
|
| 91 |
+
For implicit object member functions, the type of the implicit object
|
| 92 |
parameter is
|
| 93 |
|
| 94 |
- “lvalue reference to cv `X`” for functions declared without a
|
| 95 |
*ref-qualifier* or with the `&` *ref-qualifier*
|
| 96 |
- “rvalue reference to cv `X`” for functions declared with the `&&`
|
|
|
|
| 98 |
|
| 99 |
where `X` is the class of which the function is a member and cv is the
|
| 100 |
cv-qualification on the member function declaration.
|
| 101 |
|
| 102 |
[*Example 1*: For a `const` member function of class `X`, the extra
|
| 103 |
+
parameter is assumed to have type “lvalue reference to
|
| 104 |
`const X`”. — *end example*]
|
| 105 |
|
| 106 |
+
For conversion functions that are implicit object member functions, the
|
| 107 |
+
function is considered to be a member of the class of the implied object
|
| 108 |
+
argument for the purpose of defining the type of the implicit object
|
| 109 |
+
parameter. For non-conversion functions that are implicit object member
|
| 110 |
+
functions nominated by a *using-declaration* in a derived class, the
|
| 111 |
+
function is considered to be a member of the derived class for the
|
| 112 |
+
purpose of defining the type of the implicit object parameter. For
|
| 113 |
+
static member functions, the implicit object parameter is considered to
|
| 114 |
+
match any object (since if the function is selected, the object is
|
| 115 |
+
discarded).
|
| 116 |
|
| 117 |
[*Note 1*: No actual type is established for the implicit object
|
| 118 |
parameter of a static member function, and no attempt will be made to
|
| 119 |
determine a conversion sequence for that parameter
|
| 120 |
[[over.match.best]]. — *end note*]
|
| 121 |
|
| 122 |
During overload resolution, the implied object argument is
|
| 123 |
indistinguishable from other arguments. The implicit object parameter,
|
| 124 |
however, retains its identity since no user-defined conversions can be
|
| 125 |
+
applied to achieve a type match with it. For implicit object member
|
| 126 |
+
functions declared without a *ref-qualifier*, even if the implicit
|
| 127 |
+
object parameter is not const-qualified, an rvalue can be bound to the
|
| 128 |
parameter as long as in all other respects the argument can be converted
|
| 129 |
to the type of the implicit object parameter.
|
| 130 |
|
| 131 |
[*Note 2*: The fact that such an argument is an rvalue does not affect
|
| 132 |
the ranking of implicit conversion sequences
|
| 133 |
[[over.ics.rank]]. — *end note*]
|
| 134 |
|
| 135 |
Because other than in list-initialization only one user-defined
|
| 136 |
conversion is allowed in an implicit conversion sequence, special rules
|
| 137 |
+
apply when selecting the best user-defined conversion
|
| 138 |
+
[[over.match.best]], [[over.best.ics]].
|
| 139 |
|
| 140 |
[*Example 2*:
|
| 141 |
|
| 142 |
``` cpp
|
| 143 |
class T {
|
|
|
|
| 152 |
T a = 1; // error: no viable conversion (T(C(1)) not considered)
|
| 153 |
```
|
| 154 |
|
| 155 |
— *end example*]
|
| 156 |
|
| 157 |
+
In each case where conversion functions of a class `S` are considered
|
| 158 |
+
for initializing an object or reference of type `T`, the candidate
|
| 159 |
+
functions include the result of a search for the
|
| 160 |
+
*conversion-function-id* `operator T` in `S`.
|
| 161 |
+
|
| 162 |
+
[*Note 3*: This search can find a specialization of a conversion
|
| 163 |
+
function template [[basic.lookup]]. — *end note*]
|
| 164 |
+
|
| 165 |
+
Each such case also defines sets of *permissible types* for explicit and
|
| 166 |
+
non-explicit conversion functions; each (non-template) conversion
|
| 167 |
+
function that
|
| 168 |
+
|
| 169 |
+
- is a non-hidden member of `S`,
|
| 170 |
+
- yields a permissible type, and,
|
| 171 |
+
- for the former set, is non-explicit
|
| 172 |
+
|
| 173 |
+
is also a candidate function. If initializing an object, for any
|
| 174 |
+
permissible type cv `U`, any *cv2* `U`, *cv2* `U&`, or *cv2* `U&&` is
|
| 175 |
+
also a permissible type. If the set of permissible types for explicit
|
| 176 |
+
conversion functions is empty, any candidates that are explicit are
|
| 177 |
+
discarded.
|
| 178 |
+
|
| 179 |
In each case where a candidate is a function template, candidate
|
| 180 |
function template specializations are generated using template argument
|
| 181 |
+
deduction [[temp.over]], [[temp.deduct]]. If a constructor template or
|
| 182 |
+
conversion function template has an *explicit-specifier* whose
|
| 183 |
*constant-expression* is value-dependent [[temp.dep]], template argument
|
| 184 |
+
deduction is performed first and then, if the context admits only
|
| 185 |
+
candidates that are not explicit and the generated specialization is
|
| 186 |
explicit [[dcl.fct.spec]], it will be removed from the candidate set.
|
| 187 |
Those candidates are then handled as candidate functions in the usual
|
| 188 |
+
way.[^1]
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
+
A given name can refer to, or a conversion can consider, one or more
|
| 191 |
+
function templates as well as a set of non-template functions. In such a
|
| 192 |
+
case, the candidate functions generated from each function template are
|
| 193 |
+
combined with the set of non-template candidate functions.
|
| 194 |
+
|
| 195 |
+
A defaulted move special member function
|
| 196 |
+
[[class.copy.ctor]], [[class.copy.assign]] that is defined as deleted is
|
| 197 |
+
excluded from the set of candidate functions in all contexts. A
|
| 198 |
+
constructor inherited from class type `C` [[class.inhctor.init]] that
|
| 199 |
+
has a first parameter of type “reference to *cv1* `P`” (including such a
|
| 200 |
+
constructor instantiated from a template) is excluded from the set of
|
| 201 |
+
candidate functions when constructing an object of type *cv2* `D` if the
|
| 202 |
+
argument list has exactly one argument and `C` is reference-related to
|
| 203 |
+
`P` and `P` is reference-related to `D`.
|
| 204 |
|
| 205 |
[*Example 3*:
|
| 206 |
|
| 207 |
``` cpp
|
| 208 |
struct A {
|
|
|
|
| 225 |
|
| 226 |
— *end example*]
|
| 227 |
|
| 228 |
#### Function call syntax <a id="over.match.call">[[over.match.call]]</a>
|
| 229 |
|
| 230 |
+
##### General <a id="over.match.call.general">[[over.match.call.general]]</a>
|
| 231 |
+
|
| 232 |
In a function call [[expr.call]]
|
| 233 |
|
| 234 |
``` bnf
|
| 235 |
postfix-expression '(' expression-listₒₚₜ ')'
|
| 236 |
```
|
|
|
|
| 240 |
[[over.call.func]]. If the *postfix-expression* denotes an object of
|
| 241 |
class type, overload resolution is applied as specified in
|
| 242 |
[[over.call.object]].
|
| 243 |
|
| 244 |
If the *postfix-expression* is the address of an overload set, overload
|
| 245 |
+
resolution is applied using that set as described above.
|
|
|
|
|
|
|
| 246 |
|
| 247 |
+
[*Note 1*: No implied object argument is added in this
|
| 248 |
+
case. — *end note*]
|
| 249 |
+
|
| 250 |
+
If the function selected by overload resolution is an implicit object
|
| 251 |
+
member function, the program is ill-formed.
|
| 252 |
+
|
| 253 |
+
[*Note 2*: The resolution of the address of an overload set in other
|
| 254 |
contexts is described in [[over.over]]. — *end note*]
|
| 255 |
|
| 256 |
##### Call to named function <a id="over.call.func">[[over.call.func]]</a>
|
| 257 |
|
| 258 |
Of interest in [[over.call.func]] are only those function calls in
|
| 259 |
+
which the *postfix-expression* ultimately contains an *id-expression*
|
| 260 |
+
that denotes one or more functions. Such a *postfix-expression*, perhaps
|
| 261 |
+
nested arbitrarily deep in parentheses, has one of the following forms:
|
|
|
|
| 262 |
|
| 263 |
``` bnf
|
| 264 |
postfix-expression:
|
| 265 |
postfix-expression '.' id-expression
|
| 266 |
postfix-expression '->' id-expression
|
|
|
|
| 268 |
```
|
| 269 |
|
| 270 |
These represent two syntactic subcategories of function calls: qualified
|
| 271 |
function calls and unqualified function calls.
|
| 272 |
|
| 273 |
+
In qualified function calls, the function is named by an *id-expression*
|
| 274 |
+
preceded by an `->` or `.` operator. Since the construct `A->B` is
|
| 275 |
+
generally equivalent to `(*A).B`, the rest of [[over]] assumes, without
|
| 276 |
+
loss of generality, that all member function calls have been normalized
|
| 277 |
+
to the form that uses an object and the `.` operator. Furthermore,
|
| 278 |
+
[[over]] assumes that the *postfix-expression* that is the left operand
|
| 279 |
+
of the `.` operator has type “cv `T`” where `T` denotes a class.[^2]
|
| 280 |
+
|
| 281 |
+
The function declarations found by name lookup [[class.member.lookup]]
|
| 282 |
+
constitute the set of candidate functions. The argument list is the
|
| 283 |
+
*expression-list* in the call augmented by the addition of the left
|
| 284 |
+
operand of the `.` operator in the normalized member function call as
|
| 285 |
+
the implied object argument [[over.match.funcs]].
|
| 286 |
+
|
| 287 |
+
In unqualified function calls, the function is named by a
|
| 288 |
+
*primary-expression*. The function declarations found by name lookup
|
| 289 |
+
[[basic.lookup]] constitute the set of candidate functions. Because of
|
| 290 |
+
the rules for name lookup, the set of candidate functions consists
|
| 291 |
+
either entirely of non-member functions or entirely of member functions
|
| 292 |
+
of some class `T`. In the former case or if the *primary-expression* is
|
| 293 |
+
the address of an overload set, the argument list is the same as the
|
| 294 |
+
*expression-list* in the call. Otherwise, the argument list is the
|
| 295 |
+
*expression-list* in the call augmented by the addition of an implied
|
| 296 |
+
object argument as in a qualified function call. If the current class
|
| 297 |
+
is, or is derived from, `T`, and the keyword `this` [[expr.prim.this]]
|
| 298 |
+
refers to it, then the implied object argument is `(*this)`. Otherwise,
|
| 299 |
+
a contrived object of type `T` becomes the implied object argument;[^3]
|
| 300 |
+
|
| 301 |
+
if overload resolution selects a non-static member function, the call is
|
| 302 |
+
ill-formed.
|
| 303 |
+
|
| 304 |
+
[*Example 1*:
|
| 305 |
+
|
| 306 |
+
``` cpp
|
| 307 |
+
struct C {
|
| 308 |
+
void a();
|
| 309 |
+
void b() {
|
| 310 |
+
a(); // OK, (*this).a()
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
void c(this const C&); // #1
|
| 314 |
+
void c()&; // #2
|
| 315 |
+
static void c(int = 0); // #3
|
| 316 |
+
|
| 317 |
+
void d() {
|
| 318 |
+
c(); // error: ambiguous between #2 and #3
|
| 319 |
+
(C::c)(); // error: as above
|
| 320 |
+
(&(C::c))(); // error: cannot resolve address of overloaded this->C::c[over.over]
|
| 321 |
+
(&C::c)(C{}); // selects #1
|
| 322 |
+
(&C::c)(*this); // error: selects #2, and is ill-formed[over.match.call.general]
|
| 323 |
+
(&C::c)(); // selects #3
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
void f(this const C&);
|
| 327 |
+
void g() const {
|
| 328 |
+
f(); // OK, (*this).f()
|
| 329 |
+
f(*this); // error: no viable candidate for (*this).f(*this)
|
| 330 |
+
this->f(); // OK
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
static void h() {
|
| 334 |
+
f(); // error: contrived object argument, but overload resolution
|
| 335 |
+
// picked a non-static member function
|
| 336 |
+
f(C{}); // error: no viable candidate
|
| 337 |
+
C{}.f(); // OK
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
void k(this int);
|
| 341 |
+
operator int() const;
|
| 342 |
+
void m(this const C& c) {
|
| 343 |
+
c.k(); // OK
|
| 344 |
+
}
|
| 345 |
+
};
|
| 346 |
+
```
|
| 347 |
+
|
| 348 |
+
— *end example*]
|
| 349 |
|
| 350 |
##### Call to object of class type <a id="over.call.object">[[over.call.object]]</a>
|
| 351 |
|
| 352 |
If the *postfix-expression* `E` in the function call syntax evaluates to
|
| 353 |
a class object of type “cv `T`”, then the set of candidate functions
|
| 354 |
includes at least the function call operators of `T`. The function call
|
| 355 |
+
operators of `T` are the results of a search for the name `operator()`
|
| 356 |
+
in the scope of `T`.
|
| 357 |
|
| 358 |
In addition, for each non-explicit conversion function declared in `T`
|
| 359 |
of the form
|
| 360 |
|
| 361 |
``` bnf
|
|
|
|
| 377 |
|
| 378 |
is also considered as a candidate function. Similarly, surrogate call
|
| 379 |
functions are added to the set of candidate functions for each
|
| 380 |
non-explicit conversion function declared in a base class of `T`
|
| 381 |
provided the function is not hidden within `T` by another intervening
|
| 382 |
+
declaration.[^4]
|
| 383 |
|
| 384 |
The argument list submitted to overload resolution consists of the
|
| 385 |
argument expressions present in the function call syntax preceded by the
|
| 386 |
implied object argument `(E)`.
|
| 387 |
|
| 388 |
+
[*Note 3*: When comparing the call against the function call operators,
|
| 389 |
+
the implied object argument is compared against the object parameter of
|
| 390 |
+
the function call operator. When comparing the call against a surrogate
|
| 391 |
+
call function, the implied object argument is compared against the first
|
| 392 |
+
parameter of the surrogate call function. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
|
| 394 |
+
[*Example 2*:
|
| 395 |
|
| 396 |
``` cpp
|
| 397 |
int f1(int);
|
| 398 |
int f2(float);
|
| 399 |
typedef int (*fp1)(int);
|
|
|
|
| 439 |
```
|
| 440 |
|
| 441 |
— *end example*]
|
| 442 |
|
| 443 |
If either operand has a type that is a class or an enumeration, a
|
| 444 |
+
user-defined operator function can be declared that implements this
|
| 445 |
operator or a user-defined conversion can be necessary to convert the
|
| 446 |
operand to a type that is appropriate for a built-in operator. In this
|
| 447 |
case, overload resolution is used to determine which operator function
|
| 448 |
or built-in operator is to be invoked to implement the operator.
|
| 449 |
Therefore, the operator notation is first transformed to the equivalent
|
|
|
|
| 469 |
operand of type *cv2* `T2`, four sets of candidate functions, designated
|
| 470 |
*member candidates*, *non-member candidates*, *built-in candidates*, and
|
| 471 |
*rewritten candidates*, are constructed as follows:
|
| 472 |
|
| 473 |
- If `T1` is a complete class type or a class currently being defined,
|
| 474 |
+
the set of member candidates is the result of a search for `operator@`
|
| 475 |
+
in the scope of `T1`; otherwise, the set of member candidates is
|
| 476 |
+
empty.
|
| 477 |
+
- For the operators `=`, `[]`, or `->`, the set of non-member candidates
|
| 478 |
+
is empty; otherwise, it includes the result of unqualified lookup for
|
| 479 |
+
`operator@` in the rewritten function call
|
| 480 |
+
[[basic.lookup.unqual]], [[basic.lookup.argdep]], ignoring all member
|
| 481 |
+
functions. However, if no operand has a class type, only those
|
| 482 |
+
non-member functions in the lookup set that have a first parameter of
|
| 483 |
+
type `T1` or “reference to cv `T1`”, when `T1` is an enumeration type,
|
| 484 |
+
or (if there is a right operand) a second parameter of type `T2` or
|
| 485 |
“reference to cv `T2`”, when `T2` is an enumeration type, are
|
| 486 |
candidate functions.
|
| 487 |
- For the operator `,`, the unary operator `&`, or the operator `->`,
|
| 488 |
the built-in candidates set is empty. For all other operators, the
|
| 489 |
built-in candidates include all of the candidate operator functions
|
|
|
|
| 491 |
- have the same operator name, and
|
| 492 |
- accept the same number of operands, and
|
| 493 |
- accept operand types to which the given operand or operands can be
|
| 494 |
converted according to [[over.best.ics]], and
|
| 495 |
- do not have the same parameter-type-list as any non-member candidate
|
| 496 |
+
or rewritten non-member candidate that is not a function template
|
| 497 |
+
specialization.
|
| 498 |
- The rewritten candidate set is determined as follows:
|
| 499 |
- For the relational [[expr.rel]] operators, the rewritten candidates
|
| 500 |
include all non-rewritten candidates for the expression `x <=> y`.
|
| 501 |
- For the relational [[expr.rel]] and three-way comparison
|
| 502 |
[[expr.spaceship]] operators, the rewritten candidates also include
|
| 503 |
a synthesized candidate, with the order of the two parameters
|
| 504 |
reversed, for each non-rewritten candidate for the expression
|
| 505 |
`y <=> x`.
|
| 506 |
- For the `!=` operator [[expr.eq]], the rewritten candidates include
|
| 507 |
+
all non-rewritten candidates for the expression `x == y` that are
|
| 508 |
+
rewrite targets with first operand `x` (see below).
|
| 509 |
- For the equality operators, the rewritten candidates also include a
|
| 510 |
synthesized candidate, with the order of the two parameters
|
| 511 |
reversed, for each non-rewritten candidate for the expression
|
| 512 |
+
`y == x` that is a rewrite target with first operand `y`.
|
| 513 |
- For all other operators, the rewritten candidate set is empty.
|
| 514 |
|
| 515 |
\[*Note 2*: A candidate synthesized from a member candidate has its
|
| 516 |
+
object parameter as the second parameter, thus implicit conversions
|
| 517 |
+
are considered for the first, but not for the second,
|
| 518 |
parameter. — *end note*]
|
| 519 |
|
| 520 |
+
A non-template function or function template `F` named `operator==` is a
|
| 521 |
+
rewrite target with first operand `o` unless a search for the name
|
| 522 |
+
`operator!=` in the scope S from the instantiation context of the
|
| 523 |
+
operator expression finds a function or function template that would
|
| 524 |
+
correspond [[basic.scope.scope]] to `F` if its name were `operator==`,
|
| 525 |
+
where S is the scope of the class type of `o` if `F` is a class member,
|
| 526 |
+
and the namespace scope of which `F` is a member otherwise. A function
|
| 527 |
+
template specialization named `operator==` is a rewrite target if its
|
| 528 |
+
function template is a rewrite target.
|
| 529 |
+
|
| 530 |
+
[*Example 2*:
|
| 531 |
+
|
| 532 |
+
``` cpp
|
| 533 |
+
struct A {};
|
| 534 |
+
template<typename T> bool operator==(A, T); // #1
|
| 535 |
+
bool a1 = 0 == A(); // OK, calls reversed #1
|
| 536 |
+
template<typename T> bool operator!=(A, T);
|
| 537 |
+
bool a2 = 0 == A(); // error, #1 is not a rewrite target
|
| 538 |
+
|
| 539 |
+
struct B {
|
| 540 |
+
bool operator==(const B&); // #2
|
| 541 |
+
};
|
| 542 |
+
struct C : B {
|
| 543 |
+
C();
|
| 544 |
+
C(B);
|
| 545 |
+
bool operator!=(const B&); // #3
|
| 546 |
+
};
|
| 547 |
+
bool c1 = B() == C(); // OK, calls #2; reversed #2 is not a candidate
|
| 548 |
+
// because search for operator!= in C finds #3
|
| 549 |
+
bool c2 = C() == B(); // error: ambiguous between #2 found when searching C and
|
| 550 |
+
// reversed #2 found when searching B
|
| 551 |
+
|
| 552 |
+
struct D {};
|
| 553 |
+
template<typename T> bool operator==(D, T); // #4
|
| 554 |
+
inline namespace N {
|
| 555 |
+
template<typename T> bool operator!=(D, T); // #5
|
| 556 |
+
}
|
| 557 |
+
bool d1 = 0 == D(); // OK, calls reversed #4; #5 does not forbid #4 as a rewrite target
|
| 558 |
+
```
|
| 559 |
+
|
| 560 |
+
— *end example*]
|
| 561 |
+
|
| 562 |
For the built-in assignment operators, conversions of the left operand
|
| 563 |
are restricted as follows:
|
| 564 |
|
| 565 |
- no temporaries are introduced to hold the left operand, and
|
| 566 |
- no user-defined conversions are applied to the left operand to achieve
|
|
|
|
| 573 |
the built-in candidates, and the rewritten candidates for that operator
|
| 574 |
`@`.
|
| 575 |
|
| 576 |
The argument list contains all of the operands of the operator. The best
|
| 577 |
function from the set of candidate functions is selected according to
|
| 578 |
+
[[over.match.viable]] and [[over.match.best]].[^5]
|
| 579 |
|
| 580 |
+
[*Example 3*:
|
| 581 |
|
| 582 |
``` cpp
|
| 583 |
struct A {
|
| 584 |
operator int();
|
| 585 |
};
|
|
|
|
| 617 |
conversion sequence of a user-defined conversion sequence
|
| 618 |
[[over.ics.user]] is not applied. Then the operator is treated as the
|
| 619 |
corresponding built-in operator and interpreted according to
|
| 620 |
[[expr.compound]].
|
| 621 |
|
| 622 |
+
[*Example 4*:
|
| 623 |
|
| 624 |
``` cpp
|
| 625 |
struct X {
|
| 626 |
operator double();
|
| 627 |
};
|
|
|
|
| 637 |
— *end example*]
|
| 638 |
|
| 639 |
The second operand of operator `->` is ignored in selecting an
|
| 640 |
`operator->` function, and is not an argument when the `operator->`
|
| 641 |
function is called. When `operator->` returns, the operator `->` is
|
| 642 |
+
applied to the value returned, with the original second operand.[^6]
|
| 643 |
|
| 644 |
If the operator is the operator `,`, the unary operator `&`, or the
|
| 645 |
operator `->`, and there are no viable functions, then the operator is
|
| 646 |
assumed to be the built-in operator and interpreted according to
|
| 647 |
[[expr.compound]].
|
|
|
|
| 663 |
|
| 664 |
A a;
|
| 665 |
|
| 666 |
void B::f() {
|
| 667 |
operator+ (a,a); // error: global operator hidden by member
|
| 668 |
+
a + a; // OK, calls global operator+
|
| 669 |
}
|
| 670 |
```
|
| 671 |
|
| 672 |
— *end note*]
|
| 673 |
|
|
|
|
| 701 |
with `T` a class type, the candidate functions are selected as follows:
|
| 702 |
|
| 703 |
- The converting constructors [[class.conv.ctor]] of `T` are candidate
|
| 704 |
functions.
|
| 705 |
- When the type of the initializer expression is a class type “cv `S`”,
|
| 706 |
+
conversion functions are considered. The permissible types for
|
| 707 |
+
non-explicit conversion functions are `T` and any class derived from
|
| 708 |
+
`T`. When initializing a temporary object [[class.mem]] to be bound to
|
| 709 |
+
the first parameter of a constructor where the parameter is of type
|
| 710 |
+
“reference to *cv2* `T`” and the constructor is called with a single
|
| 711 |
+
argument in the context of direct-initialization of an object of type
|
| 712 |
+
“*cv3* `T`”, the permissible types for explicit conversion functions
|
| 713 |
+
are the same; otherwise there are none.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 714 |
|
| 715 |
In both cases, the argument list has one argument, which is the
|
| 716 |
initializer expression.
|
| 717 |
|
| 718 |
[*Note 2*: This argument will be compared against the first parameter
|
| 719 |
+
of the constructors and against the object parameter of the conversion
|
| 720 |
+
functions. — *end note*]
|
| 721 |
|
| 722 |
#### Initialization by conversion function <a id="over.match.conv">[[over.match.conv]]</a>
|
| 723 |
|
| 724 |
Under the conditions specified in [[dcl.init]], as part of an
|
| 725 |
initialization of an object of non-class type, a conversion function can
|
| 726 |
be invoked to convert an initializer expression of class type to the
|
| 727 |
type of the object being initialized. Overload resolution is used to
|
| 728 |
+
select the conversion function to be invoked. Assuming that “cv `T`” is
|
| 729 |
+
the type of the object being initialized, the candidate functions are
|
| 730 |
+
selected as follows:
|
|
|
|
| 731 |
|
| 732 |
+
- The permissible types for non-explicit conversion functions are those
|
| 733 |
+
that can be converted to type `T` via a standard conversion sequence
|
| 734 |
+
[[over.ics.scs]]. For direct-initialization, the permissible types for
|
| 735 |
+
explicit conversion functions are those that can be converted to type
|
| 736 |
+
`T` with a (possibly trivial) qualification conversion [[conv.qual]];
|
| 737 |
+
otherwise there are none.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 738 |
|
| 739 |
The argument list has one argument, which is the initializer expression.
|
| 740 |
|
| 741 |
+
[*Note 1*: This argument will be compared against the object parameter
|
| 742 |
+
of the conversion functions. — *end note*]
|
| 743 |
|
| 744 |
#### Initialization by conversion function for direct reference binding <a id="over.match.ref">[[over.match.ref]]</a>
|
| 745 |
|
| 746 |
Under the conditions specified in [[dcl.init.ref]], a reference can be
|
| 747 |
bound directly to the result of applying a conversion function to an
|
| 748 |
initializer expression. Overload resolution is used to select the
|
| 749 |
conversion function to be invoked. Assuming that “reference to *cv1*
|
| 750 |
+
`T`” is the type of the reference being initialized, the candidate
|
|
|
|
| 751 |
functions are selected as follows:
|
| 752 |
|
| 753 |
+
- Let R be a set of types including
|
| 754 |
+
- “lvalue reference to *cv2* `T2`” (when initializing an lvalue
|
| 755 |
+
reference or an rvalue reference to function) and
|
| 756 |
+
- “*cv2* `T2`” and “rvalue reference to *cv2* `T2`” (when initializing
|
| 757 |
+
an rvalue reference or an lvalue reference to function)
|
| 758 |
+
|
| 759 |
+
for any `T2`. The permissible types for non-explicit conversion
|
| 760 |
+
functions are the members of R where “*cv1* `T`” is
|
| 761 |
+
reference-compatible [[dcl.init.ref]] with “*cv2* `T2`”. For
|
| 762 |
+
direct-initialization, the permissible types for explicit conversion
|
| 763 |
+
functions are the members of R where `T2` can be converted to type `T`
|
| 764 |
+
with a (possibly trivial) qualification conversion [[conv.qual]];
|
| 765 |
+
otherwise there are none.
|
|
|
|
|
|
|
| 766 |
|
| 767 |
The argument list has one argument, which is the initializer expression.
|
| 768 |
|
| 769 |
+
[*Note 1*: This argument will be compared against the object parameter
|
| 770 |
+
of the conversion functions. — *end note*]
|
| 771 |
|
| 772 |
#### Initialization by list-initialization <a id="over.match.list">[[over.match.list]]</a>
|
| 773 |
|
| 774 |
When objects of non-aggregate class type `T` are list-initialized such
|
| 775 |
that [[dcl.init.list]] specifies that overload resolution is performed
|
|
|
|
| 788 |
consists of the elements of the initializer list.
|
| 789 |
|
| 790 |
In copy-list-initialization, if an explicit constructor is chosen, the
|
| 791 |
initialization is ill-formed.
|
| 792 |
|
| 793 |
+
[*Note 1*: This differs from other situations
|
| 794 |
+
[[over.match.ctor]], [[over.match.copy]], where only converting
|
| 795 |
+
constructors are considered for copy-initialization. This restriction
|
| 796 |
+
only applies if this initialization is part of the final result of
|
| 797 |
+
overload resolution. — *end note*]
|
| 798 |
|
| 799 |
#### Class template argument deduction <a id="over.match.class.deduct">[[over.match.class.deduct]]</a>
|
| 800 |
|
| 801 |
When resolving a placeholder for a deduced class type
|
| 802 |
[[dcl.type.class.deduct]] where the *template-name* names a primary
|
|
|
|
| 835 |
of the *braced-init-list*, or of the *expression-list*. For each xᵢ, let
|
| 836 |
eᵢ be the corresponding aggregate element of `C` or of one of its
|
| 837 |
(possibly recursive) subaggregates that would be initialized by xᵢ
|
| 838 |
[[dcl.init.aggr]] if
|
| 839 |
|
| 840 |
+
- brace elision is not considered for any aggregate element that has
|
| 841 |
+
- a dependent non-array type,
|
| 842 |
+
- an array type with a value-dependent bound, or
|
| 843 |
+
- an array type with a dependent array element type and xᵢ is a string
|
| 844 |
+
literal; and
|
| 845 |
- each non-trailing aggregate element that is a pack expansion is
|
| 846 |
assumed to correspond to no elements of the initializer list, and
|
| 847 |
- a trailing aggregate element that is a pack expansion is assumed to
|
| 848 |
correspond to all remaining elements of the initializer list (if any).
|
| 849 |
|
| 850 |
If there is no such aggregate element eᵢ for any xᵢ, the aggregate
|
| 851 |
deduction candidate is not added to the set. The aggregate deduction
|
| 852 |
candidate is derived as above from a hypothetical constructor
|
| 853 |
`C`(`T₁`, …, `Tₙ`), where
|
| 854 |
|
| 855 |
+
- if eᵢ is of array type and xᵢ is a *braced-init-list*, `Tᵢ` is an
|
| 856 |
+
rvalue reference to the declared type of eᵢ, and
|
| 857 |
+
- if eᵢ is of array type and xᵢ is a *string-literal*, `Tᵢ` is an lvalue
|
| 858 |
+
reference to the const-qualified declared type of eᵢ, and
|
| 859 |
- otherwise, `Tᵢ` is the declared type of eᵢ,
|
| 860 |
|
| 861 |
except that additional parameter packs of the form `Pⱼ` `...` are
|
| 862 |
inserted into the parameter list in their original aggregate element
|
| 863 |
position corresponding to each non-trailing aggregate element of type
|
| 864 |
`Pⱼ` that was skipped because it was a parameter pack, and the trailing
|
| 865 |
sequence of parameters corresponding to a trailing aggregate element
|
| 866 |
that is a pack expansion (if any) is replaced by a single parameter of
|
| 867 |
+
the form `Tₙ` `...`. In addition, if `C` is defined and inherits
|
| 868 |
+
constructors [[namespace.udecl]] from a direct base class denoted in the
|
| 869 |
+
*base-specifier-list* by a *class-or-decltype* `B`, let `A` be an alias
|
| 870 |
+
template whose template parameter list is that of `C` and whose
|
| 871 |
+
*defining-type-id* is `B`. If `A` is a deducible template
|
| 872 |
+
[[dcl.type.simple]], the set contains the guides of `A` with the return
|
| 873 |
+
type `R` of each guide replaced with `typename CC<R>::type` given a
|
| 874 |
+
class template
|
| 875 |
+
|
| 876 |
+
``` cpp
|
| 877 |
+
template <typename> class CC;
|
| 878 |
+
```
|
| 879 |
+
|
| 880 |
+
whose primary template is not defined and with a single partial
|
| 881 |
+
specialization whose template parameter list is that of `A` and whose
|
| 882 |
+
template argument list is a specialization of `A` with the template
|
| 883 |
+
argument list of `A` [[temp.dep.type]] having a member typedef `type`
|
| 884 |
+
designating a template specialization with the template argument list of
|
| 885 |
+
`A` but with `C` as the template.
|
| 886 |
+
|
| 887 |
+
[*Note 1*: Equivalently, the template parameter list of the
|
| 888 |
+
specialization is that of `C`, the template argument list of the
|
| 889 |
+
specialization is `B`, and the member typedef names `C` with the
|
| 890 |
+
template argument list of `C`. — *end note*]
|
| 891 |
+
|
| 892 |
+
[*Example 1*:
|
| 893 |
+
|
| 894 |
+
``` cpp
|
| 895 |
+
template <typename T> struct B {
|
| 896 |
+
B(T);
|
| 897 |
+
};
|
| 898 |
+
template <typename T> struct C : public B<T> {
|
| 899 |
+
using B<T>::B;
|
| 900 |
+
};
|
| 901 |
+
template <typename T> struct D : public B<T> {};
|
| 902 |
+
|
| 903 |
+
C c(42); // OK, deduces C<int>
|
| 904 |
+
D d(42); // error: deduction failed, no inherited deduction guides
|
| 905 |
+
B(int) -> B<char>;
|
| 906 |
+
C c2(42); // OK, deduces C<char>
|
| 907 |
+
|
| 908 |
+
template <typename T> struct E : public B<int> {
|
| 909 |
+
using B<int>::B;
|
| 910 |
+
};
|
| 911 |
+
|
| 912 |
+
E e(42); // error: deduction failed, arguments of E cannot be deduced from introduced guides
|
| 913 |
+
|
| 914 |
+
template <typename T, typename U, typename V> struct F {
|
| 915 |
+
F(T, U, V);
|
| 916 |
+
};
|
| 917 |
+
template <typename T, typename U> struct G : F<U, T, int> {
|
| 918 |
+
using G::F::F;
|
| 919 |
+
}
|
| 920 |
+
|
| 921 |
+
G g(true, 'a', 1); // OK, deduces G<char, bool>
|
| 922 |
+
|
| 923 |
+
template<class T, std::size_t N>
|
| 924 |
+
struct H {
|
| 925 |
+
T array[N];
|
| 926 |
+
};
|
| 927 |
+
template<class T, std::size_t N>
|
| 928 |
+
struct I {
|
| 929 |
+
volatile T array[N];
|
| 930 |
+
};
|
| 931 |
+
template<std::size_t N>
|
| 932 |
+
struct J {
|
| 933 |
+
unsigned char array[N];
|
| 934 |
+
};
|
| 935 |
+
|
| 936 |
+
H h = { "abc" }; // OK, deduces H<char, 4> (not T = const char)
|
| 937 |
+
I i = { "def" }; // OK, deduces I<char, 4>
|
| 938 |
+
J j = { "ghi" }; // error: cannot bind reference to array of unsigned char to array of char in deduction
|
| 939 |
+
```
|
| 940 |
+
|
| 941 |
+
— *end example*]
|
| 942 |
|
| 943 |
When resolving a placeholder for a deduced class type
|
| 944 |
[[dcl.type.simple]] where the *template-name* names an alias template
|
| 945 |
`A`, the *defining-type-id* of `A` must be of the form
|
| 946 |
|
|
|
|
| 952 |
functions or function templates formed as follows. For each function or
|
| 953 |
function template `f` in the guides of the template named by the
|
| 954 |
*simple-template-id* of the *defining-type-id*, the template arguments
|
| 955 |
of the return type of `f` are deduced from the *defining-type-id* of `A`
|
| 956 |
according to the process in [[temp.deduct.type]] with the exception that
|
| 957 |
+
deduction does not fail if not all template arguments are deduced. If
|
| 958 |
+
deduction fails for another reason, proceed with an empty set of deduced
|
| 959 |
+
template arguments. Let `g` denote the result of substituting these
|
| 960 |
+
deductions into `f`. If substitution succeeds, form a function or
|
| 961 |
+
function template `f'` with the following properties and add it to the
|
| 962 |
+
set of guides of `A`:
|
| 963 |
|
| 964 |
- The function type of `f'` is the function type of `g`.
|
| 965 |
- If `f` is a function template, `f'` is a function template whose
|
| 966 |
template parameter list consists of all the template parameters of `A`
|
| 967 |
(including their default template arguments) that appear in the above
|
|
|
|
| 971 |
function template.
|
| 972 |
- The associated constraints [[temp.constr.decl]] are the conjunction of
|
| 973 |
the associated constraints of `g` and a constraint that is satisfied
|
| 974 |
if and only if the arguments of `A` are deducible (see below) from the
|
| 975 |
return type.
|
| 976 |
+
- If `f` is a copy deduction candidate, then `f'` is considered to be so
|
| 977 |
+
as well.
|
| 978 |
+
- If `f` was generated from a *deduction-guide* [[temp.deduct.guide]],
|
| 979 |
+
then `f'` is considered to be so as well.
|
| 980 |
- The *explicit-specifier* of `f'` is the *explicit-specifier* of `g`
|
| 981 |
(if any).
|
| 982 |
|
| 983 |
The arguments of a template `A` are said to be deducible from a type `T`
|
| 984 |
if, given a class template
|
|
|
|
| 1016 |
*deduction-guide* that had an *explicit-specifier*, each such notional
|
| 1017 |
constructor is considered to have that same *explicit-specifier*. All
|
| 1018 |
such notional constructors are considered to be public members of the
|
| 1019 |
hypothetical class type.
|
| 1020 |
|
| 1021 |
+
[*Example 2*:
|
| 1022 |
|
| 1023 |
``` cpp
|
| 1024 |
template <class T> struct A {
|
| 1025 |
explicit A(const T&, ...) noexcept; // #1
|
| 1026 |
A(T&&, ...); // #2
|
|
|
|
| 1098 |
F f3 = {Types<X, Y, Z>{}, X{}, W{}}; // error: conflicting types deduced; operator Y not considered
|
| 1099 |
```
|
| 1100 |
|
| 1101 |
— *end example*]
|
| 1102 |
|
| 1103 |
+
[*Example 3*:
|
| 1104 |
|
| 1105 |
``` cpp
|
| 1106 |
template <class T, class U> struct C {
|
| 1107 |
C(T, U); // #1
|
| 1108 |
};
|
|
|
|
| 1129 |
template <class> class AA;
|
| 1130 |
template <class V> class AA<A<V>> { };
|
| 1131 |
template <class T> concept deduces_A = requires { sizeof(AA<T>); };
|
| 1132 |
|
| 1133 |
// f1 is formed from the constructor #1 of C, generating the following function template
|
| 1134 |
+
template<class T, class U>
|
| 1135 |
auto f1(T, U) -> C<T, U>;
|
| 1136 |
|
| 1137 |
// Deducing arguments for C<T, U> from C<V *, V*> deduces T as V * and U as V *;
|
| 1138 |
// f1' is obtained by transforming f1 as described by the above procedure.
|
| 1139 |
template<class V> requires deduces_A<C<V *, V *>>
|
|
|
|
| 1177 |
function parameters other than the ranking of conversion sequences.
|
| 1178 |
|
| 1179 |
First, to be a viable function, a candidate function shall have enough
|
| 1180 |
parameters to agree in number with the arguments in the list.
|
| 1181 |
|
| 1182 |
+
- If there are m arguments in the list, all candidate functions having
|
| 1183 |
+
exactly m parameters are viable.
|
| 1184 |
+
- A candidate function having fewer than m parameters is viable only if
|
| 1185 |
+
it has an ellipsis in its parameter list [[dcl.fct]]. For the purposes
|
| 1186 |
+
of overload resolution, any argument for which there is no
|
| 1187 |
corresponding parameter is considered to “match the ellipsis”
|
| 1188 |
[[over.ics.ellipsis]].
|
| 1189 |
+
- A candidate function having more than m parameters is viable only if
|
| 1190 |
all parameters following the mᵗʰ have default arguments
|
| 1191 |
[[dcl.fct.default]]. For the purposes of overload resolution, the
|
| 1192 |
+
parameter list is truncated on the right, so that there are exactly m
|
| 1193 |
+
parameters.
|
| 1194 |
|
| 1195 |
Second, for a function to be viable, if it has associated constraints
|
| 1196 |
[[temp.constr.decl]], those constraints shall be satisfied
|
| 1197 |
[[temp.constr.constr]].
|
| 1198 |
|
| 1199 |
Third, for `F` to be a viable function, there shall exist for each
|
| 1200 |
argument an implicit conversion sequence [[over.best.ics]] that converts
|
| 1201 |
that argument to the corresponding parameter of `F`. If the parameter
|
| 1202 |
has reference type, the implicit conversion sequence includes the
|
| 1203 |
operation of binding the reference, and the fact that an lvalue
|
| 1204 |
+
reference to non-`const` cannot bind to an rvalue and that an rvalue
|
| 1205 |
+
reference cannot bind to an lvalue can affect the viability of the
|
| 1206 |
function (see [[over.ics.ref]]).
|
| 1207 |
|
| 1208 |
### Best viable function <a id="over.match.best">[[over.match.best]]</a>
|
| 1209 |
|
| 1210 |
+
#### General <a id="over.match.best.general">[[over.match.best.general]]</a>
|
| 1211 |
|
| 1212 |
+
Define ICSⁱ(`F`) as the implicit conversion sequence that converts the
|
| 1213 |
+
iᵗʰ argument in the list to the type of the iᵗʰ parameter of viable
|
| 1214 |
+
function `F`. [[over.best.ics]] defines the implicit conversion
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1215 |
sequences and [[over.ics.rank]] defines what it means for one implicit
|
| 1216 |
conversion sequence to be a better conversion sequence or worse
|
| 1217 |
conversion sequence than another.
|
| 1218 |
|
| 1219 |
+
Given these definitions, a viable function `F₁` is defined to be a
|
| 1220 |
+
*better* function than another viable function `F₂` if for all arguments
|
| 1221 |
+
i, ICSⁱ(`F₁`) is not a worse conversion sequence than ICSⁱ(`F₂`), and
|
| 1222 |
+
then
|
| 1223 |
|
| 1224 |
+
- for some argument j, ICSʲ(`F₁`) is a better conversion sequence than
|
| 1225 |
+
ICSʲ(`F₂`), or, if not that,
|
| 1226 |
- the context is an initialization by user-defined conversion (see
|
| 1227 |
[[dcl.init]], [[over.match.conv]], and [[over.match.ref]]) and the
|
| 1228 |
+
standard conversion sequence from the return type of `F₁` to the
|
| 1229 |
destination type (i.e., the type of the entity being initialized) is a
|
| 1230 |
better conversion sequence than the standard conversion sequence from
|
| 1231 |
+
the return type of `F₂` to the destination type
|
| 1232 |
\[*Example 1*:
|
| 1233 |
``` cpp
|
| 1234 |
struct A {
|
| 1235 |
A();
|
| 1236 |
operator int();
|
|
|
|
| 1273 |
parameter-type-lists, and `F1` is more constrained than `F2` according
|
| 1274 |
to the partial ordering of constraints described in
|
| 1275 |
[[temp.constr.order]], or if not that,
|
| 1276 |
- `F1` is a constructor for a class `D`, `F2` is a constructor for a
|
| 1277 |
base class `B` of `D`, and for all arguments the corresponding
|
| 1278 |
+
parameters of `F1` and `F2` have the same type
|
| 1279 |
\[*Example 3*:
|
| 1280 |
``` cpp
|
| 1281 |
struct A {
|
| 1282 |
A(int = 0);
|
| 1283 |
};
|
|
|
|
| 1317 |
bool b = 1 < S(); // calls #2
|
| 1318 |
```
|
| 1319 |
|
| 1320 |
— *end example*]
|
| 1321 |
or, if not that
|
| 1322 |
+
- `F1` and `F2` are generated from class template argument deduction
|
| 1323 |
+
[[over.match.class.deduct]] for a class `D`, and `F2` is generated
|
| 1324 |
+
from inheriting constructors from a base class of `D` while `F1` is
|
| 1325 |
+
not, and for each explicit function argument, the corresponding
|
| 1326 |
+
parameters of `F1` and `F2` are either both ellipses or have the same
|
| 1327 |
+
type, or, if not that,
|
| 1328 |
- `F1` is generated from a *deduction-guide* [[over.match.class.deduct]]
|
| 1329 |
and `F2` is not, or, if not that,
|
| 1330 |
- `F1` is the copy deduction candidate [[over.match.class.deduct]] and
|
| 1331 |
`F2` is not, or, if not that,
|
| 1332 |
- `F1` is generated from a non-template constructor and `F2` is
|
|
|
|
| 1359 |
|
| 1360 |
— *end example*]
|
| 1361 |
|
| 1362 |
If there is exactly one viable function that is a better function than
|
| 1363 |
all other viable functions, then it is the one selected by overload
|
| 1364 |
+
resolution; otherwise the call is ill-formed.[^7]
|
| 1365 |
|
| 1366 |
[*Example 7*:
|
| 1367 |
|
| 1368 |
``` cpp
|
| 1369 |
void Fcn(const int*, short);
|
|
|
|
| 1378 |
|
| 1379 |
Fcn(&i, 1L); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
| 1380 |
// and 1L → short and 1L → int are indistinguishable
|
| 1381 |
|
| 1382 |
Fcn(&i, 'c'); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
| 1383 |
+
// and 'c' → int is better than 'c' → short
|
| 1384 |
}
|
| 1385 |
```
|
| 1386 |
|
| 1387 |
— *end example*]
|
| 1388 |
|
| 1389 |
If the best viable function resolves to a function for which multiple
|
| 1390 |
+
declarations were found, and if any two of these declarations inhabit
|
| 1391 |
+
different scopes and specify a default argument that made the function
|
| 1392 |
+
viable, the program is ill-formed.
|
|
|
|
| 1393 |
|
| 1394 |
[*Example 8*:
|
| 1395 |
|
| 1396 |
``` cpp
|
| 1397 |
namespace A {
|
|
|
|
| 1412 |
|
| 1413 |
— *end example*]
|
| 1414 |
|
| 1415 |
#### Implicit conversion sequences <a id="over.best.ics">[[over.best.ics]]</a>
|
| 1416 |
|
| 1417 |
+
##### General <a id="over.best.ics.general">[[over.best.ics.general]]</a>
|
| 1418 |
+
|
| 1419 |
An *implicit conversion sequence* is a sequence of conversions used to
|
| 1420 |
convert an argument in a function call to the type of the corresponding
|
| 1421 |
parameter of the function being called. The sequence of conversions is
|
| 1422 |
an implicit conversion as defined in [[conv]], which means it is
|
| 1423 |
governed by the rules for initialization of an object or reference by a
|
| 1424 |
+
single expression [[dcl.init]], [[dcl.init.ref]].
|
| 1425 |
|
| 1426 |
Implicit conversion sequences are concerned only with the type,
|
| 1427 |
cv-qualification, and value category of the argument and how these are
|
| 1428 |
converted to match the corresponding properties of the parameter.
|
| 1429 |
|
|
|
|
| 1443 |
- an ellipsis conversion sequence [[over.ics.ellipsis]].
|
| 1444 |
|
| 1445 |
However, if the target is
|
| 1446 |
|
| 1447 |
- the first parameter of a constructor or
|
| 1448 |
+
- the object parameter of a user-defined conversion function
|
| 1449 |
|
| 1450 |
and the constructor or user-defined conversion function is a candidate
|
| 1451 |
by
|
| 1452 |
|
| 1453 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
|
|
|
| 1504 |
|
| 1505 |
When the parameter has a class type and the argument expression has the
|
| 1506 |
same type, the implicit conversion sequence is an identity conversion.
|
| 1507 |
When the parameter has a class type and the argument expression has a
|
| 1508 |
derived class type, the implicit conversion sequence is a
|
| 1509 |
+
derived-to-base conversion from the derived class to the base class. A
|
| 1510 |
+
derived-to-base conversion has Conversion rank [[over.ics.scs]].
|
| 1511 |
|
| 1512 |
[*Note 4*: There is no such standard conversion; this derived-to-base
|
| 1513 |
conversion exists only in the description of implicit conversion
|
| 1514 |
sequences. — *end note*]
|
| 1515 |
|
| 1516 |
+
When the parameter is the implicit object parameter of a static member
|
| 1517 |
+
function, the implicit conversion sequence is a standard conversion
|
| 1518 |
+
sequence that is neither better nor worse than any other standard
|
| 1519 |
+
conversion sequence.
|
| 1520 |
|
| 1521 |
In all contexts, when converting to the implicit object parameter or
|
| 1522 |
when converting to the left operand of an assignment operation only
|
| 1523 |
standard conversion sequences are allowed.
|
| 1524 |
|
| 1525 |
+
[*Note 5*: When converting to the explicit object parameter, if any,
|
| 1526 |
+
user-defined conversion sequences are allowed. — *end note*]
|
| 1527 |
+
|
| 1528 |
If no conversions are required to match an argument to a parameter type,
|
| 1529 |
the implicit conversion sequence is the standard conversion sequence
|
| 1530 |
consisting of the identity conversion [[over.ics.scs]].
|
| 1531 |
|
| 1532 |
If no sequence of conversions can be found to convert an argument to a
|
|
|
|
| 1539 |
the purpose of ranking implicit conversion sequences as described in
|
| 1540 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 1541 |
user-defined conversion sequence that is indistinguishable from any
|
| 1542 |
other user-defined conversion sequence.
|
| 1543 |
|
| 1544 |
+
[*Note 6*:
|
| 1545 |
|
| 1546 |
This rule prevents a function from becoming non-viable because of an
|
| 1547 |
ambiguous conversion sequence for one of its parameters.
|
| 1548 |
|
| 1549 |
[*Example 3*:
|
|
|
|
| 1577 |
|
| 1578 |
summarizes the conversions defined in [[conv]] and partitions them into
|
| 1579 |
four disjoint categories: Lvalue Transformation, Qualification
|
| 1580 |
Adjustment, Promotion, and Conversion.
|
| 1581 |
|
| 1582 |
+
[*Note 7*: These categories are orthogonal with respect to value
|
| 1583 |
category, cv-qualification, and data representation: the Lvalue
|
| 1584 |
Transformations do not change the cv-qualification or data
|
| 1585 |
representation of the type; the Qualification Adjustments do not change
|
| 1586 |
the value category or data representation of the type; and the
|
| 1587 |
Promotions and Conversions do not change the value category or
|
| 1588 |
cv-qualification of the type. — *end note*]
|
| 1589 |
|
| 1590 |
+
[*Note 8*: As described in [[conv]], a standard conversion sequence
|
| 1591 |
either is the Identity conversion by itself (that is, no conversion) or
|
| 1592 |
consists of one to three conversions from the other four categories. If
|
| 1593 |
there are two or more conversions in the sequence, the conversions are
|
| 1594 |
applied in the canonical order: **Lvalue Transformation**, **Promotion**
|
| 1595 |
or **Conversion**, **Qualification Adjustment**. — *end note*]
|
|
|
|
| 1617 |
A *user-defined conversion sequence* consists of an initial standard
|
| 1618 |
conversion sequence followed by a user-defined conversion [[class.conv]]
|
| 1619 |
followed by a second standard conversion sequence. If the user-defined
|
| 1620 |
conversion is specified by a constructor [[class.conv.ctor]], the
|
| 1621 |
initial standard conversion sequence converts the source type to the
|
| 1622 |
+
type of the first parameter of that constructor. If the user-defined
|
| 1623 |
conversion is specified by a conversion function [[class.conv.fct]], the
|
| 1624 |
initial standard conversion sequence converts the source type to the
|
| 1625 |
+
type of the object parameter of that conversion function.
|
| 1626 |
|
| 1627 |
The second standard conversion sequence converts the result of the
|
| 1628 |
user-defined conversion to the target type for the sequence; any
|
| 1629 |
reference binding is included in the second standard conversion
|
| 1630 |
sequence. Since an implicit conversion sequence is an initialization,
|
|
|
|
| 1652 |
|
| 1653 |
When a parameter of reference type binds directly [[dcl.init.ref]] to an
|
| 1654 |
argument expression, the implicit conversion sequence is the identity
|
| 1655 |
conversion, unless the argument expression has a type that is a derived
|
| 1656 |
class of the parameter type, in which case the implicit conversion
|
| 1657 |
+
sequence is a derived-to-base conversion [[over.best.ics]].
|
| 1658 |
|
| 1659 |
[*Example 4*:
|
| 1660 |
|
| 1661 |
``` cpp
|
| 1662 |
struct A {};
|
|
|
|
| 1668 |
|
| 1669 |
— *end example*]
|
| 1670 |
|
| 1671 |
If the parameter binds directly to the result of applying a conversion
|
| 1672 |
function to the argument expression, the implicit conversion sequence is
|
| 1673 |
+
a user-defined conversion sequence [[over.ics.user]] whose second
|
| 1674 |
+
standard conversion sequence is either an identity conversion or, if the
|
| 1675 |
conversion function returns an entity of a type that is a derived class
|
| 1676 |
of the parameter type, a derived-to-base conversion.
|
| 1677 |
|
| 1678 |
When a parameter of reference type is not bound directly to an argument
|
| 1679 |
expression, the conversion sequence is the one required to convert the
|
|
|
|
| 1687 |
[[over.match.funcs]], an implicit conversion sequence cannot be formed
|
| 1688 |
if it requires binding an lvalue reference other than a reference to a
|
| 1689 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 1690 |
an lvalue other than a function lvalue.
|
| 1691 |
|
| 1692 |
+
[*Note 9*: This means, for example, that a candidate function cannot be
|
| 1693 |
a viable function if it has a non-`const` lvalue reference parameter
|
| 1694 |
(other than the implicit object parameter) and the corresponding
|
| 1695 |
argument would require a temporary to be created to initialize the
|
| 1696 |
lvalue reference (see [[dcl.init.ref]]). — *end note*]
|
| 1697 |
|
|
|
|
| 1719 |
initialized from the initializer list according to the rules for
|
| 1720 |
aggregate initialization [[dcl.init.aggr]], in which case the implicit
|
| 1721 |
conversion sequence is a user-defined conversion sequence whose second
|
| 1722 |
standard conversion sequence is an identity conversion.
|
| 1723 |
|
| 1724 |
+
[*Note 10*:
|
| 1725 |
|
| 1726 |
Aggregate initialization does not require that the members are declared
|
| 1727 |
in designation order. If, after overload resolution, the order does not
|
| 1728 |
match for the selected overload, the initialization of the parameter
|
| 1729 |
will be ill-formed [[dcl.init.list]].
|
|
|
|
| 1752 |
Otherwise, if the parameter type is an aggregate class `X` and the
|
| 1753 |
initializer list has a single element of type cv `U`, where `U` is `X`
|
| 1754 |
or a class derived from `X`, the implicit conversion sequence is the one
|
| 1755 |
required to convert the element to the parameter type.
|
| 1756 |
|
| 1757 |
+
Otherwise, if the parameter type is a character array[^8]
|
| 1758 |
+
|
| 1759 |
+
and the initializer list has a single element that is an
|
| 1760 |
+
appropriately-typed *string-literal* [[dcl.init.string]], the implicit
|
| 1761 |
+
conversion sequence is the identity conversion.
|
| 1762 |
|
| 1763 |
Otherwise, if the parameter type is `std::initializer_list<X>` and all
|
| 1764 |
the elements of the initializer list can be implicitly converted to `X`,
|
| 1765 |
the implicit conversion sequence is the worst conversion necessary to
|
| 1766 |
convert an element of the list to `X`, or if the initializer list has no
|
|
|
|
| 1770 |
|
| 1771 |
[*Example 7*:
|
| 1772 |
|
| 1773 |
``` cpp
|
| 1774 |
void f(std::initializer_list<int>);
|
| 1775 |
+
f( {} ); // OK, f(initializer_list<int>) identity conversion
|
| 1776 |
+
f( {1,2,3} ); // OK, f(initializer_list<int>) identity conversion
|
| 1777 |
+
f( {'a','b'} ); // OK, f(initializer_list<int>) integral promotion
|
| 1778 |
f( {1.0} ); // error: narrowing
|
| 1779 |
|
| 1780 |
struct A {
|
| 1781 |
A(std::initializer_list<double>); // #1
|
| 1782 |
A(std::initializer_list<complex<double>>); // #2
|
|
|
|
| 1787 |
void g(A);
|
| 1788 |
g({ "foo", "bar" }); // OK, uses #3
|
| 1789 |
|
| 1790 |
typedef int IA[3];
|
| 1791 |
void h(const IA&);
|
| 1792 |
+
h({ 1, 2, 3 }); // OK, identity conversion
|
| 1793 |
```
|
| 1794 |
|
| 1795 |
— *end example*]
|
| 1796 |
|
| 1797 |
Otherwise, if the parameter type is “array of `N` `X`” or “array of
|
|
|
|
| 1809 |
- If `C` is not an initializer-list constructor and the initializer list
|
| 1810 |
has a single element of type cv `U`, where `U` is `X` or a class
|
| 1811 |
derived from `X`, the implicit conversion sequence has Exact Match
|
| 1812 |
rank if `U` is `X`, or Conversion rank if `U` is derived from `X`.
|
| 1813 |
- Otherwise, the implicit conversion sequence is a user-defined
|
| 1814 |
+
conversion sequence whose second standard conversion sequence is an
|
| 1815 |
identity conversion.
|
| 1816 |
|
| 1817 |
If multiple constructors are viable but none is better than the others,
|
| 1818 |
the implicit conversion sequence is the ambiguous conversion sequence.
|
| 1819 |
User-defined conversions are allowed for conversion of the initializer
|
|
|
|
| 1825 |
``` cpp
|
| 1826 |
struct A {
|
| 1827 |
A(std::initializer_list<int>);
|
| 1828 |
};
|
| 1829 |
void f(A);
|
| 1830 |
+
f( {'a', 'b'} ); // OK, f(A(std::initializer_list<int>)) user-defined conversion
|
| 1831 |
|
| 1832 |
struct B {
|
| 1833 |
B(int, double);
|
| 1834 |
};
|
| 1835 |
void g(B);
|
| 1836 |
+
g( {'a', 'b'} ); // OK, g(B(int, double)) user-defined conversion
|
| 1837 |
g( {1.0, 1.0} ); // error: narrowing
|
| 1838 |
|
| 1839 |
void f(B);
|
| 1840 |
f( {'a', 'b'} ); // error: ambiguous f(A) or f(B)
|
| 1841 |
|
| 1842 |
struct C {
|
| 1843 |
C(std::string);
|
| 1844 |
};
|
| 1845 |
void h(C);
|
| 1846 |
+
h({"foo"}); // OK, h(C(std::string("foo")))
|
| 1847 |
|
| 1848 |
struct D {
|
| 1849 |
D(A, C);
|
| 1850 |
};
|
| 1851 |
void i(D);
|
| 1852 |
+
i({ {1,2}, {"bar"} }); // OK, i(D(A(std::initializer_list<int>{1,2\), C(std::string("bar"))))}
|
| 1853 |
```
|
| 1854 |
|
| 1855 |
— *end example*]
|
| 1856 |
|
| 1857 |
Otherwise, if the parameter has an aggregate type which can be
|
| 1858 |
initialized from the initializer list according to the rules for
|
| 1859 |
aggregate initialization [[dcl.init.aggr]], the implicit conversion
|
| 1860 |
+
sequence is a user-defined conversion sequence whose second standard
|
| 1861 |
+
conversion sequence is an identity conversion.
|
| 1862 |
|
| 1863 |
[*Example 9*:
|
| 1864 |
|
| 1865 |
``` cpp
|
| 1866 |
struct A {
|
| 1867 |
int m1;
|
| 1868 |
double m2;
|
| 1869 |
};
|
| 1870 |
|
| 1871 |
void f(A);
|
| 1872 |
+
f( {'a', 'b'} ); // OK, f(A(int,double)) user-defined conversion
|
| 1873 |
f( {1.0} ); // error: narrowing
|
| 1874 |
```
|
| 1875 |
|
| 1876 |
— *end example*]
|
| 1877 |
|
| 1878 |
Otherwise, if the parameter is a reference, see [[over.ics.ref]].
|
| 1879 |
|
| 1880 |
+
[*Note 11*: The rules in this subclause will apply for initializing the
|
| 1881 |
underlying temporary for the reference. — *end note*]
|
| 1882 |
|
| 1883 |
[*Example 10*:
|
| 1884 |
|
| 1885 |
``` cpp
|
|
|
|
| 1887 |
int m1;
|
| 1888 |
double m2;
|
| 1889 |
};
|
| 1890 |
|
| 1891 |
void f(const A&);
|
| 1892 |
+
f( {'a', 'b'} ); // OK, f(A(int,double)) user-defined conversion
|
| 1893 |
f( {1.0} ); // error: narrowing
|
| 1894 |
|
| 1895 |
void g(const double &);
|
| 1896 |
g({1}); // same conversion as int to double
|
| 1897 |
```
|
|
|
|
| 1904 |
initializer list, the implicit conversion sequence is the one required
|
| 1905 |
to convert the element to the parameter type;
|
| 1906 |
\[*Example 11*:
|
| 1907 |
``` cpp
|
| 1908 |
void f(int);
|
| 1909 |
+
f( {'a'} ); // OK, same conversion as char to int
|
| 1910 |
f( {1.0} ); // error: narrowing
|
| 1911 |
```
|
| 1912 |
|
| 1913 |
— *end example*]
|
| 1914 |
- if the initializer list has no elements, the implicit conversion
|
| 1915 |
sequence is the identity conversion.
|
| 1916 |
\[*Example 12*:
|
| 1917 |
``` cpp
|
| 1918 |
void f(int);
|
| 1919 |
+
f( { } ); // OK, identity conversion
|
| 1920 |
```
|
| 1921 |
|
| 1922 |
— *end example*]
|
| 1923 |
|
| 1924 |
In all cases other than those enumerated above, no conversion is
|
|
|
|
| 2107 |
- A conversion that does not convert a pointer or a pointer to member to
|
| 2108 |
`bool` is better than one that does.
|
| 2109 |
- A conversion that promotes an enumeration whose underlying type is
|
| 2110 |
fixed to its underlying type is better than one that promotes to the
|
| 2111 |
promoted underlying type, if the two are different.
|
| 2112 |
+
- A conversion in either direction between floating-point type `FP1` and
|
| 2113 |
+
floating-point type `FP2` is better than a conversion in the same
|
| 2114 |
+
direction between `FP1` and arithmetic type `T3` if
|
| 2115 |
+
- the floating-point conversion rank [[conv.rank]] of `FP1` is equal
|
| 2116 |
+
to the rank of `FP2`, and
|
| 2117 |
+
- `T3` is not a floating-point type, or `T3` is a floating-point type
|
| 2118 |
+
whose rank is not equal to the rank of `FP1`, or the floating-point
|
| 2119 |
+
conversion subrank [[conv.rank]] of `FP2` is greater than the
|
| 2120 |
+
subrank of `T3`.
|
| 2121 |
+
\[*Example 8*:
|
| 2122 |
+
``` cpp
|
| 2123 |
+
int f(std::float32_t);
|
| 2124 |
+
int f(std::float64_t);
|
| 2125 |
+
int f(long long);
|
| 2126 |
+
float x;
|
| 2127 |
+
std::float16_t y;
|
| 2128 |
+
int i = f(x); // calls f(std::float32_t) on implementations where
|
| 2129 |
+
// float and std::float32_t have equal conversion ranks
|
| 2130 |
+
int j = f(y); // error: ambiguous, no equal conversion rank
|
| 2131 |
+
```
|
| 2132 |
+
|
| 2133 |
+
— *end example*]
|
| 2134 |
- If class `B` is derived directly or indirectly from class `A`,
|
| 2135 |
conversion of `B*` to `A*` is better than conversion of `B*` to
|
| 2136 |
`void*`, and conversion of `A*` to `void*` is better than conversion
|
| 2137 |
of `B*` to `void*`.
|
| 2138 |
- If class `B` is derived directly or indirectly from class `A` and
|
| 2139 |
class `C` is derived directly or indirectly from `B`,
|
| 2140 |
- conversion of `C*` to `B*` is better than conversion of `C*` to
|
| 2141 |
`A*`,
|
| 2142 |
+
\[*Example 9*:
|
| 2143 |
``` cpp
|
| 2144 |
struct A {};
|
| 2145 |
struct B : public A {};
|
| 2146 |
struct C : public B {};
|
| 2147 |
C* pc;
|