- tmp/tmps4t1xhjv/{from.md → to.md} +516 -244
tmp/tmps4t1xhjv/{from.md → to.md}
RENAMED
|
@@ -5,14 +5,16 @@ 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 implicit object parameter, and
|
| 10 |
-
certain other properties of the candidate function.
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
Overload resolution selects the function to call in seven distinct
|
| 16 |
contexts within the language:
|
| 17 |
|
| 18 |
- invocation of a function named in the function call syntax (
|
|
@@ -21,16 +23,16 @@ contexts within the language:
|
|
| 21 |
conversion function, a reference-to-pointer-to-function conversion
|
| 22 |
function, or a reference-to-function conversion function on a class
|
| 23 |
object named in the function call syntax ([[over.call.object]]);
|
| 24 |
- invocation of the operator referenced in an expression (
|
| 25 |
[[over.match.oper]]);
|
| 26 |
-
- invocation of a constructor for direct-initialization (
|
| 27 |
-
of a class object ([[over.match.ctor]]);
|
| 28 |
- invocation of a user-defined conversion for copy-initialization (
|
| 29 |
[[dcl.init]]) of a class object ([[over.match.copy]]);
|
| 30 |
- invocation of a conversion function for initialization of an object of
|
| 31 |
-
a
|
| 32 |
[[over.match.conv]]); and
|
| 33 |
- invocation of a conversion function for conversion to a glvalue or
|
| 34 |
class prvalue to which a reference ([[dcl.init.ref]]) will be
|
| 35 |
directly bound ([[over.match.ref]]).
|
| 36 |
|
|
@@ -80,55 +82,56 @@ object parameter, if present, is always the first parameter and the
|
|
| 80 |
implied object argument, if present, is always the first argument.
|
| 81 |
|
| 82 |
For non-static member functions, the type of the implicit object
|
| 83 |
parameter is
|
| 84 |
|
| 85 |
-
- “lvalue reference to
|
| 86 |
*ref-qualifier* or with the `&` *ref-qualifier*
|
| 87 |
-
- “rvalue reference to
|
| 88 |
*ref-qualifier*
|
| 89 |
|
| 90 |
-
where `X` is the class of which the function is a member and
|
| 91 |
-
cv-qualification on the member function declaration.
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
class
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
During overload resolution, the implied object argument is
|
| 107 |
indistinguishable from other arguments. The implicit object parameter,
|
| 108 |
-
however, retains its identity since
|
| 109 |
-
|
|
|
|
| 110 |
|
| 111 |
-
-
|
| 112 |
-
implicit object parameter; and
|
| 113 |
-
- no user-defined conversions can be applied to achieve a type match
|
| 114 |
-
with it.
|
| 115 |
-
|
| 116 |
-
For non-static member functions declared without a *ref-qualifier*, an
|
| 117 |
-
additional rule applies:
|
| 118 |
-
|
| 119 |
-
- even if the implicit object parameter is not `const`-qualified, an
|
| 120 |
rvalue can be bound to the parameter as long as in all other respects
|
| 121 |
the argument can be converted to the type of the implicit object
|
| 122 |
-
parameter. The fact that such an argument is an rvalue
|
| 123 |
-
the ranking of implicit conversion sequences (
|
|
|
|
| 124 |
|
| 125 |
Because other than in list-initialization only one user-defined
|
| 126 |
conversion is allowed in an implicit conversion sequence, special rules
|
| 127 |
apply when selecting the best user-defined conversion (
|
| 128 |
[[over.match.best]], [[over.best.ics]]).
|
| 129 |
|
|
|
|
|
|
|
| 130 |
``` cpp
|
| 131 |
class T {
|
| 132 |
public:
|
| 133 |
T();
|
| 134 |
};
|
|
@@ -138,10 +141,12 @@ public:
|
|
| 138 |
C(int);
|
| 139 |
};
|
| 140 |
T a = 1; // ill-formed: T(C(1)) not tried
|
| 141 |
```
|
| 142 |
|
|
|
|
|
|
|
| 143 |
In each case where a candidate is a function template, candidate
|
| 144 |
function template specializations are generated using template argument
|
| 145 |
deduction ([[temp.over]], [[temp.deduct]]). Those candidates are then
|
| 146 |
handled as candidate functions in the usual way.[^2] A given name can
|
| 147 |
refer to one or more function templates and also to a set of overloaded
|
|
@@ -169,12 +174,13 @@ class type, overload resolution is applied as specified in
|
|
| 169 |
|
| 170 |
If the *postfix-expression* denotes the address of a set of overloaded
|
| 171 |
functions and/or function templates, overload resolution is applied
|
| 172 |
using that set as described above. If the function selected by overload
|
| 173 |
resolution is a non-static member function, the program is ill-formed.
|
| 174 |
-
|
| 175 |
-
|
|
|
|
| 176 |
|
| 177 |
##### Call to named function <a id="over.call.func">[[over.call.func]]</a>
|
| 178 |
|
| 179 |
Of interest in [[over.call.func]] are only those function calls in
|
| 180 |
which the *postfix-expression* ultimately contains a name that denotes
|
|
@@ -197,12 +203,12 @@ In qualified function calls, the name to be resolved is an
|
|
| 197 |
construct `A->B` is generally equivalent to `(*A).B`, the rest of
|
| 198 |
Clause [[over]] assumes, without loss of generality, that all member
|
| 199 |
function calls have been normalized to the form that uses an object and
|
| 200 |
the `.` operator. Furthermore, Clause [[over]] assumes that the
|
| 201 |
*postfix-expression* that is the left operand of the `.` operator has
|
| 202 |
-
type “
|
| 203 |
-
|
| 204 |
following the rules for looking up names in classes (
|
| 205 |
[[class.member.lookup]]). The function declarations found by that lookup
|
| 206 |
constitute the set of candidate functions. The argument list is the
|
| 207 |
*expression-list* in the call augmented by the addition of the left
|
| 208 |
operand of the `.` operator in the normalized member function call as
|
|
@@ -228,32 +234,33 @@ and overload resolution selects one of the non-static member functions
|
|
| 228 |
of `T`, the call is ill-formed.
|
| 229 |
|
| 230 |
##### Call to object of class type <a id="over.call.object">[[over.call.object]]</a>
|
| 231 |
|
| 232 |
If the *primary-expression* `E` in the function call syntax evaluates to
|
| 233 |
-
a class object of type “
|
| 234 |
includes at least the function call operators of `T`. The function call
|
| 235 |
operators of `T` are obtained by ordinary lookup of the name
|
| 236 |
`operator()` in the context of `(E).operator()`.
|
| 237 |
|
| 238 |
In addition, for each non-explicit conversion function declared in `T`
|
| 239 |
of the form
|
| 240 |
|
| 241 |
``` bnf
|
| 242 |
-
'operator' conversion-type-id '( )' cv-qualifier ref-qualifierₒₚₜ
|
| 243 |
```
|
| 244 |
|
| 245 |
where *cv-qualifier* is the same cv-qualification as, or a greater
|
| 246 |
-
cv-qualification than,
|
| 247 |
-
type “pointer to function of (`
|
| 248 |
-
“reference to pointer to function of (`
|
| 249 |
-
the type “reference to function of (`
|
| 250 |
*surrogate call function* with the unique name *call-function* and
|
| 251 |
having the form
|
| 252 |
|
| 253 |
``` bnf
|
| 254 |
-
'R' call-function '(' conversion-type-id
|
|
|
|
| 255 |
```
|
| 256 |
|
| 257 |
is also considered as a candidate function. Similarly, surrogate call
|
| 258 |
functions are added to the set of candidate functions for each
|
| 259 |
non-explicit conversion function declared in a base class of `T`
|
|
@@ -267,82 +274,92 @@ then be invoked with the arguments of the call. If the conversion
|
|
| 267 |
function cannot be called (e.g., because of an ambiguity), the program
|
| 268 |
is ill-formed.
|
| 269 |
|
| 270 |
The argument list submitted to overload resolution consists of the
|
| 271 |
argument expressions present in the function call syntax preceded by the
|
| 272 |
-
implied object argument `(E)`.
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
``` cpp
|
| 283 |
int f1(int);
|
| 284 |
int f2(float);
|
| 285 |
typedef int (*fp1)(int);
|
| 286 |
typedef int (*fp2)(float);
|
| 287 |
struct A {
|
| 288 |
operator fp1() { return f1; }
|
| 289 |
operator fp2() { return f2; }
|
| 290 |
} a;
|
| 291 |
-
int i = a(1);
|
| 292 |
-
// conversion function
|
| 293 |
```
|
| 294 |
|
|
|
|
|
|
|
| 295 |
#### Operators in expressions <a id="over.match.oper">[[over.match.oper]]</a>
|
| 296 |
|
| 297 |
If no operand of an operator in an expression has a type that is a class
|
| 298 |
or an enumeration, the operator is assumed to be a built-in operator and
|
| 299 |
-
interpreted according to Clause [[expr]].
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
``` cpp
|
| 307 |
struct String {
|
| 308 |
String (const String&);
|
| 309 |
String (const char*);
|
| 310 |
operator const char* ();
|
| 311 |
};
|
| 312 |
String operator + (const String&, const String&);
|
| 313 |
|
| 314 |
-
void f(
|
| 315 |
-
const char* p= "one" + "two"; // ill-formed because neither
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
// class or enumeration types exist that
|
| 319 |
-
// would perform the operation.
|
| 320 |
}
|
| 321 |
```
|
| 322 |
|
|
|
|
|
|
|
| 323 |
If either operand has a type that is a class or an enumeration, a
|
| 324 |
user-defined operator function might be declared that implements this
|
| 325 |
operator or a user-defined conversion can be necessary to convert the
|
| 326 |
operand to a type that is appropriate for a built-in operator. In this
|
| 327 |
case, overload resolution is used to determine which operator function
|
| 328 |
or built-in operator is to be invoked to implement the operator.
|
| 329 |
Therefore, the operator notation is first transformed to the equivalent
|
| 330 |
function-call notation as summarized in Table [[tab:over.rel.op.func]]
|
| 331 |
(where `@` denotes one of the operators covered in the specified
|
| 332 |
-
subclause).
|
|
|
|
| 333 |
|
| 334 |
**Table: Relationship between operator and function call notation** <a id="tab:over.rel.op.func">[tab:over.rel.op.func]</a>
|
| 335 |
|
| 336 |
| Subclause | Expression | As member function | As non-member function |
|
| 337 |
-
| ------------ | ---------- | ------------------ | ---------------------- |
|
| 338 |
-
| (a)
|
| 339 |
-
| (a, b)
|
| 340 |
-
| [[over.ass]] | a=b
|
| 341 |
-
| [[over.sub]] | a[b]
|
| 342 |
-
| [[over.ref]] | a->
|
| 343 |
-
| (a, 0)
|
| 344 |
|
| 345 |
|
| 346 |
For a unary operator `@` with an operand of a type whose cv-unqualified
|
| 347 |
version is `T1`, and for a binary operator `@` with a left operand of a
|
| 348 |
type whose cv-unqualified version is `T1` and a right operand of a type
|
|
@@ -358,14 +375,14 @@ candidates*, are constructed as follows:
|
|
| 358 |
lookup of `operator@` in the context of the expression according to
|
| 359 |
the usual rules for name lookup in unqualified function calls (
|
| 360 |
[[basic.lookup.argdep]]) except that all member functions are ignored.
|
| 361 |
However, if no operand has a class type, only those non-member
|
| 362 |
functions in the lookup set that have a first parameter of type `T1`
|
| 363 |
-
or “reference to
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
- For the operator `,`, the unary operator `&`, or the operator `->`,
|
| 368 |
the built-in candidates set is empty. For all other operators, the
|
| 369 |
built-in candidates include all of the candidate operator functions
|
| 370 |
defined in [[over.built]] that, compared to the given operator,
|
| 371 |
- have the same operator name, and
|
|
@@ -389,10 +406,12 @@ the member candidates, the non-member candidates, and the built-in
|
|
| 389 |
candidates. The argument list contains all of the operands of the
|
| 390 |
operator. The best function from the set of candidate functions is
|
| 391 |
selected according to [[over.match.viable]] and
|
| 392 |
[[over.match.best]].[^6]
|
| 393 |
|
|
|
|
|
|
|
| 394 |
``` cpp
|
| 395 |
struct A {
|
| 396 |
operator int();
|
| 397 |
};
|
| 398 |
A operator+(const A&, const A&);
|
|
@@ -400,18 +419,22 @@ void m() {
|
|
| 400 |
A a, b;
|
| 401 |
a + b; // operator+(a, b) chosen over int(a) + int(b)
|
| 402 |
}
|
| 403 |
```
|
| 404 |
|
|
|
|
|
|
|
| 405 |
If a built-in candidate is selected by overload resolution, the operands
|
| 406 |
of class type are converted to the types of the corresponding parameters
|
| 407 |
of the selected operation function, except that the second standard
|
| 408 |
conversion sequence of a user-defined conversion sequence (
|
| 409 |
[[over.ics.user]]) is not applied. Then the operator is treated as the
|
| 410 |
corresponding built-in operator and interpreted according to Clause
|
| 411 |
[[expr]].
|
| 412 |
|
|
|
|
|
|
|
| 413 |
``` cpp
|
| 414 |
struct X {
|
| 415 |
operator double();
|
| 416 |
};
|
| 417 |
|
|
@@ -421,20 +444,24 @@ struct Y {
|
|
| 421 |
|
| 422 |
int *a = Y() + 100.0; // error: pointer arithmetic requires integral operand
|
| 423 |
int *b = Y() + X(); // error: pointer arithmetic requires integral operand
|
| 424 |
```
|
| 425 |
|
|
|
|
|
|
|
| 426 |
The second operand of operator `->` is ignored in selecting an
|
| 427 |
`operator->` function, and is not an argument when the `operator->`
|
| 428 |
function is called. When `operator->` returns, the operator `->` is
|
| 429 |
applied to the value returned, with the original second operand.[^7]
|
| 430 |
|
| 431 |
If the operator is the operator `,`, the unary operator `&`, or the
|
| 432 |
operator `->`, and there are no viable functions, then the operator is
|
| 433 |
assumed to be the built-in operator and interpreted according to Clause
|
| 434 |
[[expr]].
|
| 435 |
|
|
|
|
|
|
|
| 436 |
The lookup rules for operators in expressions are different than the
|
| 437 |
lookup rules for operator function names in a function call, as shown in
|
| 438 |
the following example:
|
| 439 |
|
| 440 |
``` cpp
|
|
@@ -452,64 +479,72 @@ void B::f() {
|
|
| 452 |
operator+ (a,a); // error: global operator hidden by member
|
| 453 |
a + a; // OK: calls global operator+
|
| 454 |
}
|
| 455 |
```
|
| 456 |
|
|
|
|
|
|
|
| 457 |
#### Initialization by constructor <a id="over.match.ctor">[[over.match.ctor]]</a>
|
| 458 |
|
| 459 |
-
When objects of class type are direct-initialized ([[dcl.init]]),
|
| 460 |
copy-initialized from an expression of the same or a derived class
|
| 461 |
-
type ([[dcl.init]]),
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
the candidate functions are all the
|
|
|
|
|
|
|
| 465 |
[[class.conv.ctor]]) of that class. The argument list is the
|
| 466 |
*expression-list* or *assignment-expression* of the *initializer*.
|
| 467 |
|
| 468 |
#### Copy-initialization of class by user-defined conversion <a id="over.match.copy">[[over.match.copy]]</a>
|
| 469 |
|
| 470 |
Under the conditions specified in [[dcl.init]], as part of a
|
| 471 |
copy-initialization of an object of class type, a user-defined
|
| 472 |
conversion can be invoked to convert an initializer expression to the
|
| 473 |
type of the object being initialized. Overload resolution is used to
|
| 474 |
-
select the user-defined conversion to be invoked.
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
|
|
|
|
|
|
| 480 |
|
| 481 |
- The converting constructors ([[class.conv.ctor]]) of `T` are
|
| 482 |
candidate functions.
|
| 483 |
-
- When the type of the initializer expression is a class type “
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
possibly cv-qualified `T`
|
| 488 |
-
argument in the context of direct-initialization of an object
|
| 489 |
-
“*cv2* `T`”, explicit conversion functions are also
|
| 490 |
-
that are not hidden within `S` and yield a type
|
| 491 |
-
version is the same type as `T` or is a derived
|
| 492 |
-
candidate functions. Conversion functions that
|
| 493 |
-
`X`” return lvalues or xvalues, depending on the
|
| 494 |
-
type `X` and are therefore considered to yield
|
| 495 |
-
selecting candidate functions.
|
| 496 |
|
| 497 |
In both cases, the argument list has one argument, which is the
|
| 498 |
-
initializer expression.
|
| 499 |
-
|
| 500 |
-
|
|
|
|
|
|
|
| 501 |
|
| 502 |
#### Initialization by conversion function <a id="over.match.conv">[[over.match.conv]]</a>
|
| 503 |
|
| 504 |
Under the conditions specified in [[dcl.init]], as part of an
|
| 505 |
-
initialization of an object of
|
| 506 |
be invoked to convert an initializer expression of class type to the
|
| 507 |
type of the object being initialized. Overload resolution is used to
|
| 508 |
select the conversion function to be invoked. Assuming that “*cv1* `T`”
|
| 509 |
-
is the type of the object being initialized, and “
|
| 510 |
-
|
| 511 |
functions are selected as follows:
|
| 512 |
|
| 513 |
- The conversion functions of `S` and its base classes are considered.
|
| 514 |
Those non-explicit conversion functions that are not hidden within `S`
|
| 515 |
and yield type `T` or a type that can be converted to type `T` via a
|
|
@@ -524,46 +559,50 @@ functions are selected as follows:
|
|
| 524 |
return lvalues or xvalues, depending on the type of reference, of type
|
| 525 |
“*cv2* `X`” and are therefore considered to yield `X` for this process
|
| 526 |
of selecting candidate functions.
|
| 527 |
|
| 528 |
The argument list has one argument, which is the initializer expression.
|
| 529 |
-
|
| 530 |
-
the
|
|
|
|
| 531 |
|
| 532 |
#### Initialization by conversion function for direct reference binding <a id="over.match.ref">[[over.match.ref]]</a>
|
| 533 |
|
| 534 |
Under the conditions specified in [[dcl.init.ref]], a reference can be
|
| 535 |
bound directly to a glvalue or class prvalue that is the result of
|
| 536 |
applying a conversion function to an initializer expression. Overload
|
| 537 |
resolution is used to select the conversion function to be invoked.
|
| 538 |
-
Assuming that “*cv1* `T`” is the
|
| 539 |
-
initialized, and “
|
| 540 |
-
with `S` a class type, the candidate functions are selected
|
|
|
|
| 541 |
|
| 542 |
- The conversion functions of `S` and its base classes are considered.
|
| 543 |
Those non-explicit conversion functions that are not hidden within `S`
|
| 544 |
and yield type “lvalue reference to *cv2* `T2`” (when initializing an
|
| 545 |
-
lvalue reference or an rvalue reference to function) or “ `T2`”
|
| 546 |
-
“rvalue reference to `T2`” (when initializing an rvalue
|
| 547 |
-
an lvalue reference to function), where “*cv1* `T`” is
|
| 548 |
reference-compatible ([[dcl.init.ref]]) with “*cv2* `T2`”, are
|
| 549 |
candidate functions. For direct-initialization, those explicit
|
| 550 |
conversion functions that are not hidden within `S` and yield type
|
| 551 |
“lvalue reference to *cv2* `T2`” or “*cv2* `T2`” or “rvalue reference
|
| 552 |
-
to *cv2* `T2`
|
| 553 |
can be converted to type `T` with a qualification conversion (
|
| 554 |
[[conv.qual]]), are also candidate functions.
|
| 555 |
|
| 556 |
The argument list has one argument, which is the initializer expression.
|
| 557 |
-
|
| 558 |
-
the
|
|
|
|
| 559 |
|
| 560 |
#### Initialization by list-initialization <a id="over.match.list">[[over.match.list]]</a>
|
| 561 |
|
| 562 |
-
When objects of non-aggregate class type `T` are list-initialized
|
| 563 |
-
[[dcl.init.list]]
|
| 564 |
-
|
|
|
|
| 565 |
|
| 566 |
- Initially, the candidate functions are the initializer-list
|
| 567 |
constructors ([[dcl.init.list]]) of the class `T` and the argument
|
| 568 |
list consists of the initializer list as a single argument.
|
| 569 |
- If no viable initializer-list constructor is found, overload
|
|
@@ -572,14 +611,89 @@ phases:
|
|
| 572 |
the elements of the initializer list.
|
| 573 |
|
| 574 |
If the initializer list has no elements and `T` has a default
|
| 575 |
constructor, the first phase is omitted. In copy-list-initialization, if
|
| 576 |
an `explicit` constructor is chosen, the initialization is ill-formed.
|
| 577 |
-
|
|
|
|
| 578 |
[[over.match.copy]]), where only converting constructors are considered
|
| 579 |
for copy-initialization. This restriction only applies if this
|
| 580 |
-
initialization is part of the final result of overload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
|
| 582 |
### Viable functions <a id="over.match.viable">[[over.match.viable]]</a>
|
| 583 |
|
| 584 |
From the set of candidate functions constructed for a given context (
|
| 585 |
[[over.match.funcs]]), a set of viable functions is chosen, from which
|
|
@@ -615,14 +729,14 @@ function (see [[over.ics.ref]]).
|
|
| 615 |
|
| 616 |
### Best viable function <a id="over.match.best">[[over.match.best]]</a>
|
| 617 |
|
| 618 |
Define ICS*i*(`F`) as follows:
|
| 619 |
|
| 620 |
-
-
|
| 621 |
ICS*1*(`F`) is neither better nor worse than ICS*1*(`G`) for any
|
| 622 |
function `G`, and, symmetrically, ICS*1*(`G`) is neither better nor
|
| 623 |
-
worse than ICS*1*(`F`)[^9]
|
| 624 |
- let ICS*i*(`F`) denote the implicit conversion sequence that converts
|
| 625 |
the *i*-th argument in the list to the type of the *i*-th parameter of
|
| 626 |
viable function `F`. [[over.best.ics]] defines the implicit conversion
|
| 627 |
sequences and [[over.ics.rank]] defines what it means for one implicit
|
| 628 |
conversion sequence to be a better conversion sequence or worse
|
|
@@ -638,30 +752,32 @@ and then
|
|
| 638 |
- the context is an initialization by user-defined conversion (see
|
| 639 |
[[dcl.init]], [[over.match.conv]], and [[over.match.ref]]) and the
|
| 640 |
standard conversion sequence from the return type of `F1` to the
|
| 641 |
destination type (i.e., the type of the entity being initialized) is a
|
| 642 |
better conversion sequence than the standard conversion sequence from
|
| 643 |
-
the return type of `F2` to the destination type
|
|
|
|
| 644 |
``` cpp
|
| 645 |
struct A {
|
| 646 |
A();
|
| 647 |
operator int();
|
| 648 |
operator double();
|
| 649 |
} a;
|
| 650 |
-
int i = a;
|
| 651 |
-
|
| 652 |
-
// a conversion to int
|
| 653 |
float x = a; // ambiguous: both possibilities require conversions,
|
| 654 |
// and neither is better than the other
|
| 655 |
```
|
| 656 |
|
|
|
|
| 657 |
or, if not that,
|
| 658 |
- the context is an initialization by conversion function for direct
|
| 659 |
reference binding ([[over.match.ref]]) of a reference to function
|
| 660 |
type, the return type of `F1` is the same kind of reference (i.e.
|
| 661 |
lvalue or rvalue) as the reference being initialized, and the return
|
| 662 |
type of `F2` is not
|
|
|
|
| 663 |
``` cpp
|
| 664 |
template <class T> struct A {
|
| 665 |
operator T&(); // #1
|
| 666 |
operator T&&(); // #2
|
| 667 |
};
|
|
@@ -669,50 +785,87 @@ and then
|
|
| 669 |
A<Fn> a;
|
| 670 |
Fn& lf = a; // calls #1
|
| 671 |
Fn&& rf = a; // calls #2
|
| 672 |
```
|
| 673 |
|
|
|
|
| 674 |
or, if not that,
|
| 675 |
- `F1` is not a function template specialization and `F2` is a function
|
| 676 |
template specialization, or, if not that,
|
| 677 |
- `F1` and `F2` are function template specializations, and the function
|
| 678 |
template for `F1` is more specialized than the template for `F2`
|
| 679 |
according to the partial ordering rules described in
|
| 680 |
-
[[temp.func.order]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
|
| 682 |
If there is exactly one viable function that is a better function than
|
| 683 |
all other viable functions, then it is the one selected by overload
|
| 684 |
-
resolution; otherwise the call is ill-formed[^10]
|
|
|
|
|
|
|
| 685 |
|
| 686 |
``` cpp
|
| 687 |
void Fcn(const int*, short);
|
| 688 |
void Fcn(int*, int);
|
| 689 |
|
| 690 |
int i;
|
| 691 |
short s = 0;
|
| 692 |
|
| 693 |
void f() {
|
| 694 |
-
Fcn(&i, s);
|
| 695 |
-
// &i → int* is better than &i → const int*
|
| 696 |
// but s → short is also better than s → int
|
| 697 |
|
| 698 |
-
Fcn(&i, 1L);
|
| 699 |
-
// &i → int* is better than &i → const int*
|
| 700 |
// and 1L → short and 1L → int are indistinguishable
|
| 701 |
|
| 702 |
-
Fcn(&i,'c');
|
| 703 |
-
// &i → int* is better than &i → const int*
|
| 704 |
// and c → int is better than c → short
|
| 705 |
}
|
| 706 |
```
|
| 707 |
|
|
|
|
|
|
|
| 708 |
If the best viable function resolves to a function for which multiple
|
| 709 |
declarations were found, and if at least two of these declarations — or
|
| 710 |
the declarations they refer to in the case of *using-declaration*s —
|
| 711 |
specify a default argument that made the function viable, the program is
|
| 712 |
ill-formed.
|
| 713 |
|
|
|
|
|
|
|
| 714 |
``` cpp
|
| 715 |
namespace A {
|
| 716 |
extern "C" void f(int = 5);
|
| 717 |
}
|
| 718 |
namespace B {
|
|
@@ -726,10 +879,12 @@ void use() {
|
|
| 726 |
f(3); // OK, default argument was not used for viability
|
| 727 |
f(); // Error: found default argument twice
|
| 728 |
}
|
| 729 |
```
|
| 730 |
|
|
|
|
|
|
|
| 731 |
#### Implicit conversion sequences <a id="over.best.ics">[[over.best.ics]]</a>
|
| 732 |
|
| 733 |
An *implicit conversion sequence* is a sequence of conversions used to
|
| 734 |
convert an argument in a function call to the type of the corresponding
|
| 735 |
parameter of the function being called. The sequence of conversions is
|
|
@@ -738,16 +893,16 @@ governed by the rules for initialization of an object or reference by a
|
|
| 738 |
single expression ([[dcl.init]], [[dcl.init.ref]]).
|
| 739 |
|
| 740 |
Implicit conversion sequences are concerned only with the type,
|
| 741 |
cv-qualification, and value category of the argument and how these are
|
| 742 |
converted to match the corresponding properties of the parameter. Other
|
| 743 |
-
properties, such as the lifetime, storage class, alignment,
|
| 744 |
-
accessibility of the argument
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
argument
|
| 748 |
-
analysis.
|
| 749 |
|
| 750 |
A well-formed implicit conversion sequence is one of the following
|
| 751 |
forms:
|
| 752 |
|
| 753 |
- a *standard conversion sequence* ([[over.ics.scs]]),
|
|
@@ -765,17 +920,21 @@ by
|
|
| 765 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
| 766 |
step of a class copy-initialization,
|
| 767 |
- [[over.match.copy]], [[over.match.conv]], or [[over.match.ref]] (in
|
| 768 |
all cases), or
|
| 769 |
- the second phase of [[over.match.list]] when the initializer list has
|
| 770 |
-
exactly one element
|
| 771 |
-
|
| 772 |
-
|
| 773 |
|
| 774 |
-
user-defined conversion sequences are not considered.
|
| 775 |
-
|
| 776 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 777 |
|
| 778 |
``` cpp
|
| 779 |
struct Y { Y(int); };
|
| 780 |
struct A { operator int(); };
|
| 781 |
Y y1 = A(); // error: A::operator int() is not a candidate
|
|
@@ -784,83 +943,120 @@ struct Y { Y(int); };
|
|
| 784 |
struct B { operator X(); };
|
| 785 |
B b;
|
| 786 |
X x({b}); // error: B::operator X() is not a candidate
|
| 787 |
```
|
| 788 |
|
|
|
|
|
|
|
| 789 |
For the case where the parameter type is a reference, see
|
| 790 |
[[over.ics.ref]].
|
| 791 |
|
| 792 |
When the parameter type is not a reference, the implicit conversion
|
| 793 |
sequence models a copy-initialization of the parameter from the argument
|
| 794 |
expression. The implicit conversion sequence is the one required to
|
| 795 |
convert the argument expression to a prvalue of the type of the
|
| 796 |
-
parameter.
|
|
|
|
|
|
|
| 797 |
conversion defined for the purposes of Clause [[over]]; the actual
|
| 798 |
initialization is defined in terms of constructors and is not a
|
| 799 |
-
conversion.
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 804 |
When the parameter has a class type and the argument expression has the
|
| 805 |
same type, the implicit conversion sequence is an identity conversion.
|
| 806 |
When the parameter has a class type and the argument expression has a
|
| 807 |
derived class type, the implicit conversion sequence is a
|
| 808 |
derived-to-base Conversion from the derived class to the base class.
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
|
|
|
|
|
|
|
|
|
| 812 |
|
| 813 |
In all contexts, when converting to the implicit object parameter or
|
| 814 |
when converting to the left operand of an assignment operation only
|
| 815 |
-
standard conversion sequences
|
| 816 |
-
result are allowed.
|
| 817 |
|
| 818 |
If no conversions are required to match an argument to a parameter type,
|
| 819 |
the implicit conversion sequence is the standard conversion sequence
|
| 820 |
consisting of the identity conversion ([[over.ics.scs]]).
|
| 821 |
|
| 822 |
If no sequence of conversions can be found to convert an argument to a
|
| 823 |
-
parameter type
|
| 824 |
-
conversion sequence cannot be formed.
|
| 825 |
|
| 826 |
If several different sequences of conversions exist that each convert
|
| 827 |
the argument to the parameter type, the implicit conversion sequence
|
| 828 |
associated with the parameter is defined to be the unique conversion
|
| 829 |
sequence designated the *ambiguous conversion sequence*. For the purpose
|
| 830 |
of ranking implicit conversion sequences as described in
|
| 831 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 832 |
-
user-defined sequence that is indistinguishable from any
|
| 833 |
-
user-defined conversion sequence
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 837 |
|
| 838 |
The three forms of implicit conversion sequences mentioned above are
|
| 839 |
defined in the following subclauses.
|
| 840 |
|
| 841 |
##### Standard conversion sequences <a id="over.ics.scs">[[over.ics.scs]]</a>
|
| 842 |
|
| 843 |
Table [[tab:over.conversions]] summarizes the conversions defined in
|
| 844 |
Clause [[conv]] and partitions them into four disjoint categories:
|
| 845 |
Lvalue Transformation, Qualification Adjustment, Promotion, and
|
| 846 |
-
Conversion.
|
|
|
|
|
|
|
| 847 |
category, cv-qualification, and data representation: the Lvalue
|
| 848 |
Transformations do not change the cv-qualification or data
|
| 849 |
representation of the type; the Qualification Adjustments do not change
|
| 850 |
the value category or data representation of the type; and the
|
| 851 |
Promotions and Conversions do not change the value category or
|
| 852 |
-
cv-qualification of the type.
|
| 853 |
|
| 854 |
-
As described in Clause [[conv]], a standard conversion
|
| 855 |
-
either the Identity conversion by itself (that is, no
|
| 856 |
-
consists of one to three conversions from the other four
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
sequence, the conversions are applied in the canonical order: **Lvalue
|
| 860 |
Transformation**, **Promotion** or **Conversion**, **Qualification
|
| 861 |
-
Adjustment**.
|
| 862 |
|
| 863 |
Each conversion in Table [[tab:over.conversions]] also has an
|
| 864 |
associated rank (Exact Match, Promotion, or Conversion). These are used
|
| 865 |
to rank standard conversion sequences ([[over.ics.rank]]). The rank of
|
| 866 |
a conversion sequence is determined by considering the rank of each
|
|
@@ -921,70 +1117,88 @@ When a parameter of reference type binds directly ([[dcl.init.ref]]) to
|
|
| 921 |
an argument expression, the implicit conversion sequence is the identity
|
| 922 |
conversion, unless the argument expression has a type that is a derived
|
| 923 |
class of the parameter type, in which case the implicit conversion
|
| 924 |
sequence is a derived-to-base Conversion ([[over.best.ics]]).
|
| 925 |
|
|
|
|
|
|
|
| 926 |
``` cpp
|
| 927 |
struct A {};
|
| 928 |
struct B : public A {} b;
|
| 929 |
int f(A&);
|
| 930 |
int f(B&);
|
| 931 |
-
int i = f(b);
|
| 932 |
-
// f(A&), a conversion
|
| 933 |
```
|
| 934 |
|
|
|
|
|
|
|
| 935 |
If the parameter binds directly to the result of applying a conversion
|
| 936 |
function to the argument expression, the implicit conversion sequence is
|
| 937 |
a user-defined conversion sequence ([[over.ics.user]]), with the second
|
| 938 |
standard conversion sequence either an identity conversion or, if the
|
| 939 |
conversion function returns an entity of a type that is a derived class
|
| 940 |
of the parameter type, a derived-to-base Conversion.
|
| 941 |
|
| 942 |
When a parameter of reference type is not bound directly to an argument
|
| 943 |
expression, the conversion sequence is the one required to convert the
|
| 944 |
-
argument expression to the
|
| 945 |
-
|
| 946 |
-
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
conversion.
|
| 950 |
|
| 951 |
Except for an implicit object parameter, for which see
|
| 952 |
[[over.match.funcs]], a standard conversion sequence cannot be formed if
|
| 953 |
it requires binding an lvalue reference other than a reference to a
|
| 954 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 955 |
-
an lvalue other than a function lvalue.
|
| 956 |
-
|
| 957 |
-
|
| 958 |
-
|
| 959 |
-
|
|
|
|
|
|
|
| 960 |
|
| 961 |
Other restrictions on binding a reference to a particular argument that
|
| 962 |
are not based on the types of the reference and the argument do not
|
| 963 |
-
affect the formation of a standard conversion sequence, however.
|
| 964 |
-
|
| 965 |
-
|
| 966 |
-
|
| 967 |
-
|
| 968 |
-
|
| 969 |
-
|
| 970 |
-
|
|
|
|
|
|
|
| 971 |
|
| 972 |
##### List-initialization sequence <a id="over.ics.list">[[over.ics.list]]</a>
|
| 973 |
|
| 974 |
When an argument is an initializer list ([[dcl.init.list]]), it is not
|
| 975 |
an expression and special rules apply for converting it to a parameter
|
| 976 |
type.
|
| 977 |
|
| 978 |
-
If the parameter type is `
|
| 979 |
-
|
| 980 |
-
conversion sequence is the
|
| 981 |
-
|
| 982 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 983 |
conversion even in the context of a call to an initializer-list
|
| 984 |
constructor.
|
| 985 |
|
|
|
|
|
|
|
| 986 |
``` cpp
|
| 987 |
void f(std::initializer_list<int>);
|
| 988 |
f( {} ); // OK: f(initializer_list<int>) identity conversion
|
| 989 |
f( {1,2,3} ); // OK: f(initializer_list<int>) identity conversion
|
| 990 |
f( {'a','b'} ); // OK: f(initializer_list<int>) integral promotion
|
|
@@ -1003,27 +1217,38 @@ g({ "foo", "bar" }); // OK, uses #3
|
|
| 1003 |
typedef int IA[3];
|
| 1004 |
void h(const IA&);
|
| 1005 |
h({ 1, 2, 3 }); // OK: identity conversion
|
| 1006 |
```
|
| 1007 |
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
-
|
|
|
|
| 1014 |
|
| 1015 |
Otherwise, if the parameter is a non-aggregate class `X` and overload
|
| 1016 |
-
resolution per [[over.match.list]] chooses a single best constructor
|
| 1017 |
-
`X` to perform the initialization of an object of type `X` from
|
| 1018 |
-
argument initializer list
|
| 1019 |
-
|
| 1020 |
-
|
| 1021 |
-
|
| 1022 |
-
|
| 1023 |
-
|
| 1024 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1025 |
|
| 1026 |
``` cpp
|
| 1027 |
struct A {
|
| 1028 |
A(std::initializer_list<int>);
|
| 1029 |
};
|
|
@@ -1051,16 +1276,20 @@ struct D {
|
|
| 1051 |
};
|
| 1052 |
void i(D);
|
| 1053 |
i({ {1,2}, {"bar"} }); // OK: i(D(A(std::initializer_list<int>{1,2\), C(std::string("bar"))))}
|
| 1054 |
```
|
| 1055 |
|
|
|
|
|
|
|
| 1056 |
Otherwise, if the parameter has an aggregate type which can be
|
| 1057 |
initialized from the initializer list according to the rules for
|
| 1058 |
aggregate initialization ([[dcl.init.aggr]]), the implicit conversion
|
| 1059 |
sequence is a user-defined conversion sequence with the second standard
|
| 1060 |
conversion sequence an identity conversion.
|
| 1061 |
|
|
|
|
|
|
|
| 1062 |
``` cpp
|
| 1063 |
struct A {
|
| 1064 |
int m1;
|
| 1065 |
double m2;
|
| 1066 |
};
|
|
@@ -1068,13 +1297,18 @@ struct A {
|
|
| 1068 |
void f(A);
|
| 1069 |
f( {'a', 'b'} ); // OK: f(A(int,double)) user-defined conversion
|
| 1070 |
f( {1.0} ); // error: narrowing
|
| 1071 |
```
|
| 1072 |
|
| 1073 |
-
|
| 1074 |
-
|
| 1075 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1076 |
|
| 1077 |
``` cpp
|
| 1078 |
struct A {
|
| 1079 |
int m1;
|
| 1080 |
double m2;
|
|
@@ -1086,33 +1320,41 @@ f( {1.0} ); // error: narrowing
|
|
| 1086 |
|
| 1087 |
void g(const double &);
|
| 1088 |
g({1}); // same conversion as int to double
|
| 1089 |
```
|
| 1090 |
|
|
|
|
|
|
|
| 1091 |
Otherwise, if the parameter type is not a class:
|
| 1092 |
|
| 1093 |
-
- if the initializer list has one element
|
| 1094 |
-
|
| 1095 |
-
type;
|
|
|
|
| 1096 |
``` cpp
|
| 1097 |
void f(int);
|
| 1098 |
f( {'a'} ); // OK: same conversion as char to int
|
| 1099 |
f( {1.0} ); // error: narrowing
|
| 1100 |
```
|
|
|
|
|
|
|
| 1101 |
- if the initializer list has no elements, the implicit conversion
|
| 1102 |
sequence is the identity conversion.
|
|
|
|
| 1103 |
``` cpp
|
| 1104 |
void f(int);
|
| 1105 |
f( { } ); // OK: identity conversion
|
| 1106 |
```
|
| 1107 |
|
|
|
|
|
|
|
| 1108 |
In all cases other than those enumerated above, no conversion is
|
| 1109 |
possible.
|
| 1110 |
|
| 1111 |
#### Ranking implicit conversion sequences <a id="over.ics.rank">[[over.ics.rank]]</a>
|
| 1112 |
|
| 1113 |
-
|
| 1114 |
sequences based on the relationships *better conversion sequence* and
|
| 1115 |
*better conversion*. If an implicit conversion sequence S1 is defined by
|
| 1116 |
these rules to be a better conversion sequence than S2, then it is also
|
| 1117 |
the case that S2 is a *worse conversion sequence* than S1. If conversion
|
| 1118 |
sequence S1 is neither better than nor worse than conversion sequence
|
|
@@ -1129,10 +1371,31 @@ defined in [[over.best.ics]])
|
|
| 1129 |
[[over.ics.ellipsis]]).
|
| 1130 |
|
| 1131 |
Two implicit conversion sequences of the same form are indistinguishable
|
| 1132 |
conversion sequences unless one of the following rules applies:
|
| 1133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1134 |
- Standard conversion sequence `S1` is a better conversion sequence than
|
| 1135 |
standard conversion sequence `S2` if
|
| 1136 |
- `S1` is a proper subsequence of `S2` (comparing the conversion
|
| 1137 |
sequences in the canonical form defined by [[over.ics.scs]],
|
| 1138 |
excluding any Lvalue Transformation; the identity conversion
|
|
@@ -1142,11 +1405,12 @@ conversion sequences unless one of the following rules applies:
|
|
| 1142 |
have the same rank and are distinguishable by the rules in the
|
| 1143 |
paragraph below, or, if not that,
|
| 1144 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and neither
|
| 1145 |
refers to an implicit object parameter of a non-static member
|
| 1146 |
function declared without a *ref-qualifier*, and `S1` binds an
|
| 1147 |
-
rvalue reference to an rvalue and `S2` binds an lvalue reference
|
|
|
|
| 1148 |
``` cpp
|
| 1149 |
int i;
|
| 1150 |
int f1();
|
| 1151 |
int&& f2();
|
| 1152 |
int g(const int&);
|
|
@@ -1168,41 +1432,47 @@ conversion sequences unless one of the following rules applies:
|
|
| 1168 |
a << 'c'; // calls A::operator<<(int)
|
| 1169 |
A().p(); // calls A::p()&&
|
| 1170 |
a.p(); // calls A::p()&
|
| 1171 |
```
|
| 1172 |
|
|
|
|
| 1173 |
or, if not that,
|
| 1174 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and `S1`
|
| 1175 |
binds an lvalue reference to a function lvalue and `S2` binds an
|
| 1176 |
-
rvalue reference to a function lvalue
|
|
|
|
| 1177 |
``` cpp
|
| 1178 |
int f(void(&)()); // #1
|
| 1179 |
int f(void(&&)()); // #2
|
| 1180 |
void g();
|
| 1181 |
int i1 = f(g); // calls #1
|
| 1182 |
```
|
| 1183 |
|
|
|
|
| 1184 |
or, if not that,
|
| 1185 |
- `S1`
|
| 1186 |
and `S2` differ only in their qualification conversion and yield
|
| 1187 |
similar types `T1` and `T2` ([[conv.qual]]), respectively, and the
|
| 1188 |
cv-qualification signature of type `T1` is a proper subset of the
|
| 1189 |
-
cv-qualification signature of type `T2`
|
|
|
|
| 1190 |
``` cpp
|
| 1191 |
int f(const volatile int *);
|
| 1192 |
int f(const int *);
|
| 1193 |
int i;
|
| 1194 |
int j = f(&i); // calls f(const int*)
|
| 1195 |
```
|
| 1196 |
|
|
|
|
| 1197 |
or, if not that,
|
| 1198 |
- `S1`
|
| 1199 |
and `S2` are reference bindings ([[dcl.init.ref]]), and the types
|
| 1200 |
to which the references refer are the same type except for top-level
|
| 1201 |
cv-qualifiers, and the type to which the reference initialized by
|
| 1202 |
`S2` refers is more cv-qualified than the type to which the
|
| 1203 |
reference initialized by `S1` refers.
|
|
|
|
| 1204 |
``` cpp
|
| 1205 |
int f(const int &);
|
| 1206 |
int f(int &);
|
| 1207 |
int g(const int &);
|
| 1208 |
int g(int);
|
|
@@ -1218,31 +1488,30 @@ conversion sequences unless one of the following rules applies:
|
|
| 1218 |
void g(const X& a, X b) {
|
| 1219 |
a.f(); // calls X::f() const
|
| 1220 |
b.f(); // calls X::f()
|
| 1221 |
}
|
| 1222 |
```
|
|
|
|
|
|
|
| 1223 |
- User-defined conversion sequence `U1` is a better conversion sequence
|
| 1224 |
than another user-defined conversion sequence `U2` if they contain the
|
| 1225 |
same user-defined conversion function or constructor or they
|
| 1226 |
initialize the same class in an aggregate initialization and in either
|
| 1227 |
case the second standard conversion sequence of `U1` is better than
|
| 1228 |
the second standard conversion sequence of `U2`.
|
|
|
|
| 1229 |
``` cpp
|
| 1230 |
struct A {
|
| 1231 |
operator short();
|
| 1232 |
} a;
|
| 1233 |
int f(int);
|
| 1234 |
int f(float);
|
| 1235 |
int i = f(a); // calls f(int), because short → int is
|
| 1236 |
// better than short → float.
|
| 1237 |
```
|
| 1238 |
-
|
| 1239 |
-
|
| 1240 |
-
- `L1` converts to `std::initializer_list<X>` for some `X` and `L2`
|
| 1241 |
-
does not, or, if not that,
|
| 1242 |
-
- `L1` converts to type “array of `N1` `T`”, `L2` converts to type
|
| 1243 |
-
“array of `N2` `T`”, and `N1` is smaller than `N2`.
|
| 1244 |
|
| 1245 |
Standard conversion sequences are ordered by their ranks: an Exact Match
|
| 1246 |
is a better conversion than a Promotion, which is a better conversion
|
| 1247 |
than a Conversion. Two conversion sequences with the same rank are
|
| 1248 |
indistinguishable unless one of the following rules applies:
|
|
@@ -1258,19 +1527,22 @@ indistinguishable unless one of the following rules applies:
|
|
| 1258 |
of `B*` to `void*`.
|
| 1259 |
- If class `B` is derived directly or indirectly from class `A` and
|
| 1260 |
class `C` is derived directly or indirectly from `B`,
|
| 1261 |
- conversion of `C*` to `B*` is better than conversion of `C*` to
|
| 1262 |
`A*`,
|
|
|
|
| 1263 |
``` cpp
|
| 1264 |
struct A {};
|
| 1265 |
struct B : public A {};
|
| 1266 |
struct C : public B {};
|
| 1267 |
C* pc;
|
| 1268 |
int f(A*);
|
| 1269 |
int f(B*);
|
| 1270 |
int i = f(pc); // calls f(B*)
|
| 1271 |
```
|
|
|
|
|
|
|
| 1272 |
- binding of an expression of type `C` to a reference to type `B` is
|
| 1273 |
better than binding an expression of type `C` to a reference to type
|
| 1274 |
`A`,
|
| 1275 |
- conversion of `A::*` to `B::*` is better than conversion of `A::*`
|
| 1276 |
to `C::*`,
|
|
@@ -1282,11 +1554,11 @@ indistinguishable unless one of the following rules applies:
|
|
| 1282 |
`A`,
|
| 1283 |
- conversion of `B::*` to `C::*` is better than conversion of `A::*`
|
| 1284 |
to `C::*`, and
|
| 1285 |
- conversion of `B` to `A` is better than conversion of `C` to `A`.
|
| 1286 |
|
| 1287 |
-
Compared conversion sequences will have different source
|
| 1288 |
-
the context of comparing the second standard conversion
|
| 1289 |
-
initialization by user-defined conversion (see
|
| 1290 |
-
in all other contexts, the source types will be
|
| 1291 |
-
target types will be different.
|
| 1292 |
|
|
|
|
| 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 implicit object parameter, and
|
| 10 |
+
certain other properties of the candidate function.
|
| 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*]
|
| 16 |
|
| 17 |
Overload resolution selects the function to call in seven distinct
|
| 18 |
contexts within the language:
|
| 19 |
|
| 20 |
- invocation of a function named in the function call syntax (
|
|
|
|
| 23 |
conversion function, a reference-to-pointer-to-function conversion
|
| 24 |
function, or a reference-to-function conversion function on a class
|
| 25 |
object named in the function call syntax ([[over.call.object]]);
|
| 26 |
- invocation of the operator referenced in an expression (
|
| 27 |
[[over.match.oper]]);
|
| 28 |
+
- invocation of a constructor for default- or direct-initialization (
|
| 29 |
+
[[dcl.init]]) of a class object ([[over.match.ctor]]);
|
| 30 |
- invocation of a user-defined conversion for copy-initialization (
|
| 31 |
[[dcl.init]]) of a class object ([[over.match.copy]]);
|
| 32 |
- invocation of a conversion function for initialization of an object of
|
| 33 |
+
a non-class type from an expression of class type (
|
| 34 |
[[over.match.conv]]); and
|
| 35 |
- invocation of a conversion function for conversion to a glvalue or
|
| 36 |
class prvalue to which a reference ([[dcl.init.ref]]) will be
|
| 37 |
directly bound ([[over.match.ref]]).
|
| 38 |
|
|
|
|
| 82 |
implied object argument, if present, is always the first argument.
|
| 83 |
|
| 84 |
For non-static member functions, the type of the implicit object
|
| 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 `&&`
|
| 90 |
*ref-qualifier*
|
| 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, the function is considered to be a member of
|
| 100 |
+
the class of the implied object argument for the purpose of defining the
|
| 101 |
+
type of the implicit object parameter. For non-conversion functions
|
| 102 |
+
introduced by a *using-declaration* into a derived class, the function
|
| 103 |
+
is considered to be a member of the derived class for the purpose of
|
| 104 |
+
defining the type of the implicit object parameter. For static member
|
| 105 |
+
functions, the implicit object parameter is considered to match any
|
| 106 |
+
object (since if the function is selected, the object is discarded).
|
| 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 non-static member functions
|
| 117 |
+
declared without a *ref-qualifier*, an additional rule applies:
|
| 118 |
|
| 119 |
+
- even if the implicit object parameter is not const-qualified, an
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
rvalue can be bound to the parameter as long as in all other respects
|
| 121 |
the argument can be converted to the type of the implicit object
|
| 122 |
+
parameter. \[*Note 2*: The fact that such an argument is an rvalue
|
| 123 |
+
does not affect 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 {
|
| 135 |
public:
|
| 136 |
T();
|
| 137 |
};
|
|
|
|
| 141 |
C(int);
|
| 142 |
};
|
| 143 |
T a = 1; // ill-formed: T(C(1)) not tried
|
| 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 ([[temp.over]], [[temp.deduct]]). Those candidates are then
|
| 151 |
handled as candidate functions in the usual way.[^2] A given name can
|
| 152 |
refer to one or more function templates and also to a set of overloaded
|
|
|
|
| 174 |
|
| 175 |
If the *postfix-expression* denotes the address of a set of overloaded
|
| 176 |
functions and/or function templates, overload resolution is applied
|
| 177 |
using that set as described above. If the function selected by overload
|
| 178 |
resolution is a non-static member function, the program is ill-formed.
|
| 179 |
+
|
| 180 |
+
[*Note 1*: The resolution of the address of an overload set in other
|
| 181 |
+
contexts is described in [[over.over]]. — *end note*]
|
| 182 |
|
| 183 |
##### Call to named function <a id="over.call.func">[[over.call.func]]</a>
|
| 184 |
|
| 185 |
Of interest in [[over.call.func]] are only those function calls in
|
| 186 |
which the *postfix-expression* ultimately contains a name that denotes
|
|
|
|
| 203 |
construct `A->B` is generally equivalent to `(*A).B`, the rest of
|
| 204 |
Clause [[over]] assumes, without loss of generality, that all member
|
| 205 |
function calls have been normalized to the form that uses an object and
|
| 206 |
the `.` operator. Furthermore, Clause [[over]] assumes that the
|
| 207 |
*postfix-expression* that is the left operand of the `.` operator has
|
| 208 |
+
type “cv `T`” where `T` denotes a class[^3]. Under this assumption, the
|
| 209 |
+
*id-expression* in the call is looked up as a member function of `T`
|
| 210 |
following the rules for looking up names in classes (
|
| 211 |
[[class.member.lookup]]). The function declarations found by that lookup
|
| 212 |
constitute the set of candidate functions. The argument list is the
|
| 213 |
*expression-list* in the call augmented by the addition of the left
|
| 214 |
operand of the `.` operator in the normalized member function call as
|
|
|
|
| 234 |
of `T`, the call is ill-formed.
|
| 235 |
|
| 236 |
##### Call to object of class type <a id="over.call.object">[[over.call.object]]</a>
|
| 237 |
|
| 238 |
If the *primary-expression* `E` in the function call syntax evaluates to
|
| 239 |
+
a class object of type “cv `T`”, then the set of candidate functions
|
| 240 |
includes at least the function call operators of `T`. The function call
|
| 241 |
operators of `T` are obtained by ordinary lookup of the name
|
| 242 |
`operator()` in the context of `(E).operator()`.
|
| 243 |
|
| 244 |
In addition, for each non-explicit conversion function declared in `T`
|
| 245 |
of the form
|
| 246 |
|
| 247 |
``` bnf
|
| 248 |
+
'operator' conversion-type-id '( )' cv-qualifier ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ ';'
|
| 249 |
```
|
| 250 |
|
| 251 |
where *cv-qualifier* is the same cv-qualification as, or a greater
|
| 252 |
+
cv-qualification than, cv, and where *conversion-type-id* denotes the
|
| 253 |
+
type “pointer to function of (`P₁`, …, `Pₙ`) returning `R`”, or the type
|
| 254 |
+
“reference to pointer to function of (`P₁`, …, `Pₙ`) returning `R`”, or
|
| 255 |
+
the type “reference to function of (`P₁`, …, `Pₙ`) returning `R`”, a
|
| 256 |
*surrogate call function* with the unique name *call-function* and
|
| 257 |
having the form
|
| 258 |
|
| 259 |
``` bnf
|
| 260 |
+
'R' call-function '(' conversion-type-id \ %
|
| 261 |
+
'F, P₁ a₁, …, Pₙ aₙ)' '{ return F (a₁, …, aₙ); }'
|
| 262 |
```
|
| 263 |
|
| 264 |
is also considered as a candidate function. Similarly, surrogate call
|
| 265 |
functions are added to the set of candidate functions for each
|
| 266 |
non-explicit conversion function declared in a base class of `T`
|
|
|
|
| 274 |
function cannot be called (e.g., because of an ambiguity), the program
|
| 275 |
is ill-formed.
|
| 276 |
|
| 277 |
The argument list submitted to overload resolution consists of the
|
| 278 |
argument expressions present in the function call syntax preceded by the
|
| 279 |
+
implied object argument `(E)`.
|
| 280 |
+
|
| 281 |
+
[*Note 2*: When comparing the call against the function call operators,
|
| 282 |
+
the implied object argument is compared against the implicit object
|
| 283 |
+
parameter of the function call operator. When comparing the call against
|
| 284 |
+
a surrogate call function, the implied object argument is compared
|
| 285 |
+
against the first parameter of the surrogate call function. The
|
| 286 |
+
conversion function from which the surrogate call function was derived
|
| 287 |
+
will be used in the conversion sequence for that parameter since it
|
| 288 |
+
converts the implied object argument to the appropriate function pointer
|
| 289 |
+
or reference required by that first parameter. — *end note*]
|
| 290 |
+
|
| 291 |
+
[*Example 1*:
|
| 292 |
|
| 293 |
``` cpp
|
| 294 |
int f1(int);
|
| 295 |
int f2(float);
|
| 296 |
typedef int (*fp1)(int);
|
| 297 |
typedef int (*fp2)(float);
|
| 298 |
struct A {
|
| 299 |
operator fp1() { return f1; }
|
| 300 |
operator fp2() { return f2; }
|
| 301 |
} a;
|
| 302 |
+
int i = a(1); // calls f1 via pointer returned from conversion function
|
|
|
|
| 303 |
```
|
| 304 |
|
| 305 |
+
— *end example*]
|
| 306 |
+
|
| 307 |
#### Operators in expressions <a id="over.match.oper">[[over.match.oper]]</a>
|
| 308 |
|
| 309 |
If no operand of an operator in an expression has a type that is a class
|
| 310 |
or an enumeration, the operator is assumed to be a built-in operator and
|
| 311 |
+
interpreted according to Clause [[expr]].
|
| 312 |
+
|
| 313 |
+
[*Note 1*: Because `.`, `.*`, and `::` cannot be overloaded, these
|
| 314 |
+
operators are always built-in operators interpreted according to Clause
|
| 315 |
+
[[expr]]. `?:` cannot be overloaded, but the rules in this subclause are
|
| 316 |
+
used to determine the conversions to be applied to the second and third
|
| 317 |
+
operands when they have class or enumeration type (
|
| 318 |
+
[[expr.cond]]). — *end note*]
|
| 319 |
+
|
| 320 |
+
[*Example 1*:
|
| 321 |
|
| 322 |
``` cpp
|
| 323 |
struct String {
|
| 324 |
String (const String&);
|
| 325 |
String (const char*);
|
| 326 |
operator const char* ();
|
| 327 |
};
|
| 328 |
String operator + (const String&, const String&);
|
| 329 |
|
| 330 |
+
void f() {
|
| 331 |
+
const char* p= "one" + "two"; // ill-formed because neither operand has class or enumeration type
|
| 332 |
+
int I = 1 + 1; // always evaluates to 2 even if class or enumeration types exist
|
| 333 |
+
// that would perform the operation.
|
|
|
|
|
|
|
| 334 |
}
|
| 335 |
```
|
| 336 |
|
| 337 |
+
— *end example*]
|
| 338 |
+
|
| 339 |
If either operand has a type that is a class or an enumeration, a
|
| 340 |
user-defined operator function might be declared that implements this
|
| 341 |
operator or a user-defined conversion can be necessary to convert the
|
| 342 |
operand to a type that is appropriate for a built-in operator. In this
|
| 343 |
case, overload resolution is used to determine which operator function
|
| 344 |
or built-in operator is to be invoked to implement the operator.
|
| 345 |
Therefore, the operator notation is first transformed to the equivalent
|
| 346 |
function-call notation as summarized in Table [[tab:over.rel.op.func]]
|
| 347 |
(where `@` denotes one of the operators covered in the specified
|
| 348 |
+
subclause). However, the operands are sequenced in the order prescribed
|
| 349 |
+
for the built-in operator (Clause [[expr]]).
|
| 350 |
|
| 351 |
**Table: Relationship between operator and function call notation** <a id="tab:over.rel.op.func">[tab:over.rel.op.func]</a>
|
| 352 |
|
| 353 |
| Subclause | Expression | As member function | As non-member function |
|
| 354 |
+
| ------------ | ---------- | ------------------- | ---------------------- |
|
| 355 |
+
| (a)} |
|
| 356 |
+
| (a, b)} |
|
| 357 |
+
| [[over.ass]] | `a=b` | `(a).operator= (b)` | |
|
| 358 |
+
| [[over.sub]] | `a[b]` | `(a).operator[](b)` | |
|
| 359 |
+
| [[over.ref]] | `a->` | `(a).operator->( )` | |
|
| 360 |
+
| (a, 0)} |
|
| 361 |
|
| 362 |
|
| 363 |
For a unary operator `@` with an operand of a type whose cv-unqualified
|
| 364 |
version is `T1`, and for a binary operator `@` with a left operand of a
|
| 365 |
type whose cv-unqualified version is `T1` and a right operand of a type
|
|
|
|
| 375 |
lookup of `operator@` in the context of the expression according to
|
| 376 |
the usual rules for name lookup in unqualified function calls (
|
| 377 |
[[basic.lookup.argdep]]) except that all member functions are ignored.
|
| 378 |
However, if no operand has a class type, only those non-member
|
| 379 |
functions in the lookup set that have a first parameter of type `T1`
|
| 380 |
+
or “reference to cv `T1`”, when `T1` is an enumeration type, or (if
|
| 381 |
+
there is a right operand) a second parameter of type `T2` or
|
| 382 |
+
“reference to cv `T2`”, when `T2` is an enumeration type, are
|
| 383 |
+
candidate functions.
|
| 384 |
- For the operator `,`, the unary operator `&`, or the operator `->`,
|
| 385 |
the built-in candidates set is empty. For all other operators, the
|
| 386 |
built-in candidates include all of the candidate operator functions
|
| 387 |
defined in [[over.built]] that, compared to the given operator,
|
| 388 |
- have the same operator name, and
|
|
|
|
| 406 |
candidates. The argument list contains all of the operands of the
|
| 407 |
operator. The best function from the set of candidate functions is
|
| 408 |
selected according to [[over.match.viable]] and
|
| 409 |
[[over.match.best]].[^6]
|
| 410 |
|
| 411 |
+
[*Example 2*:
|
| 412 |
+
|
| 413 |
``` cpp
|
| 414 |
struct A {
|
| 415 |
operator int();
|
| 416 |
};
|
| 417 |
A operator+(const A&, const A&);
|
|
|
|
| 419 |
A a, b;
|
| 420 |
a + b; // operator+(a, b) chosen over int(a) + int(b)
|
| 421 |
}
|
| 422 |
```
|
| 423 |
|
| 424 |
+
— *end example*]
|
| 425 |
+
|
| 426 |
If a built-in candidate is selected by overload resolution, the operands
|
| 427 |
of class type are converted to the types of the corresponding parameters
|
| 428 |
of the selected operation function, except that the second standard
|
| 429 |
conversion sequence of a user-defined conversion sequence (
|
| 430 |
[[over.ics.user]]) is not applied. Then the operator is treated as the
|
| 431 |
corresponding built-in operator and interpreted according to Clause
|
| 432 |
[[expr]].
|
| 433 |
|
| 434 |
+
[*Example 3*:
|
| 435 |
+
|
| 436 |
``` cpp
|
| 437 |
struct X {
|
| 438 |
operator double();
|
| 439 |
};
|
| 440 |
|
|
|
|
| 444 |
|
| 445 |
int *a = Y() + 100.0; // error: pointer arithmetic requires integral operand
|
| 446 |
int *b = Y() + X(); // error: pointer arithmetic requires integral operand
|
| 447 |
```
|
| 448 |
|
| 449 |
+
— *end example*]
|
| 450 |
+
|
| 451 |
The second operand of operator `->` is ignored in selecting an
|
| 452 |
`operator->` function, and is not an argument when the `operator->`
|
| 453 |
function is called. When `operator->` returns, the operator `->` is
|
| 454 |
applied to the value returned, with the original second operand.[^7]
|
| 455 |
|
| 456 |
If the operator is the operator `,`, the unary operator `&`, or the
|
| 457 |
operator `->`, and there are no viable functions, then the operator is
|
| 458 |
assumed to be the built-in operator and interpreted according to Clause
|
| 459 |
[[expr]].
|
| 460 |
|
| 461 |
+
[*Note 2*:
|
| 462 |
+
|
| 463 |
The lookup rules for operators in expressions are different than the
|
| 464 |
lookup rules for operator function names in a function call, as shown in
|
| 465 |
the following example:
|
| 466 |
|
| 467 |
``` cpp
|
|
|
|
| 479 |
operator+ (a,a); // error: global operator hidden by member
|
| 480 |
a + a; // OK: calls global operator+
|
| 481 |
}
|
| 482 |
```
|
| 483 |
|
| 484 |
+
— *end note*]
|
| 485 |
+
|
| 486 |
#### Initialization by constructor <a id="over.match.ctor">[[over.match.ctor]]</a>
|
| 487 |
|
| 488 |
+
When objects of class type are direct-initialized ([[dcl.init]]),
|
| 489 |
copy-initialized from an expression of the same or a derived class
|
| 490 |
+
type ([[dcl.init]]), or default-initialized ([[dcl.init]]), overload
|
| 491 |
+
resolution selects the constructor. For direct-initialization or
|
| 492 |
+
default-initialization that is not in the context of
|
| 493 |
+
copy-initialization, the candidate functions are all the constructors of
|
| 494 |
+
the class of the object being initialized. For copy-initialization, the
|
| 495 |
+
candidate functions are all the converting constructors (
|
| 496 |
[[class.conv.ctor]]) of that class. The argument list is the
|
| 497 |
*expression-list* or *assignment-expression* of the *initializer*.
|
| 498 |
|
| 499 |
#### Copy-initialization of class by user-defined conversion <a id="over.match.copy">[[over.match.copy]]</a>
|
| 500 |
|
| 501 |
Under the conditions specified in [[dcl.init]], as part of a
|
| 502 |
copy-initialization of an object of class type, a user-defined
|
| 503 |
conversion can be invoked to convert an initializer expression to the
|
| 504 |
type of the object being initialized. Overload resolution is used to
|
| 505 |
+
select the user-defined conversion to be invoked.
|
| 506 |
+
|
| 507 |
+
[*Note 1*: The conversion performed for indirect binding to a reference
|
| 508 |
+
to a possibly cv-qualified class type is determined in terms of a
|
| 509 |
+
corresponding non-reference copy-initialization. — *end note*]
|
| 510 |
+
|
| 511 |
+
Assuming that “*cv1* `T`” is the type of the object being initialized,
|
| 512 |
+
with `T` a class type, the candidate functions are selected as follows:
|
| 513 |
|
| 514 |
- The converting constructors ([[class.conv.ctor]]) of `T` are
|
| 515 |
candidate functions.
|
| 516 |
+
- When the type of the initializer expression is a class type “cv `S`”,
|
| 517 |
+
the non-explicit conversion functions of `S` and its base classes are
|
| 518 |
+
considered. When initializing a temporary to be bound to the first
|
| 519 |
+
parameter of a constructor where the parameter is of type “reference
|
| 520 |
+
to possibly cv-qualified `T`” and the constructor is called with a
|
| 521 |
+
single argument in the context of direct-initialization of an object
|
| 522 |
+
of type “*cv2* `T`”, explicit conversion functions are also
|
| 523 |
+
considered. Those that are not hidden within `S` and yield a type
|
| 524 |
+
whose cv-unqualified version is the same type as `T` or is a derived
|
| 525 |
+
class thereof are candidate functions. Conversion functions that
|
| 526 |
+
return “reference to `X`” return lvalues or xvalues, depending on the
|
| 527 |
+
type of reference, of type `X` and are therefore considered to yield
|
| 528 |
+
`X` for this process of selecting candidate functions.
|
| 529 |
|
| 530 |
In both cases, the argument list has one argument, which is the
|
| 531 |
+
initializer expression.
|
| 532 |
+
|
| 533 |
+
[*Note 2*: This argument will be compared against the first parameter
|
| 534 |
+
of the constructors and against the implicit object parameter of the
|
| 535 |
+
conversion functions. — *end note*]
|
| 536 |
|
| 537 |
#### Initialization by conversion function <a id="over.match.conv">[[over.match.conv]]</a>
|
| 538 |
|
| 539 |
Under the conditions specified in [[dcl.init]], as part of an
|
| 540 |
+
initialization of an object of non-class type, a conversion function can
|
| 541 |
be invoked to convert an initializer expression of class type to the
|
| 542 |
type of the object being initialized. Overload resolution is used to
|
| 543 |
select the conversion function to be invoked. Assuming that “*cv1* `T`”
|
| 544 |
+
is the type of the object being initialized, and “cv `S`” is the type of
|
| 545 |
+
the initializer expression, with `S` a class type, the candidate
|
| 546 |
functions are selected as follows:
|
| 547 |
|
| 548 |
- The conversion functions of `S` and its base classes are considered.
|
| 549 |
Those non-explicit conversion functions that are not hidden within `S`
|
| 550 |
and yield type `T` or a type that can be converted to type `T` via a
|
|
|
|
| 559 |
return lvalues or xvalues, depending on the type of reference, of type
|
| 560 |
“*cv2* `X`” and are therefore considered to yield `X` for this process
|
| 561 |
of selecting candidate functions.
|
| 562 |
|
| 563 |
The argument list has one argument, which is the initializer expression.
|
| 564 |
+
|
| 565 |
+
[*Note 1*: This argument will be compared against the implicit object
|
| 566 |
+
parameter of the conversion functions. — *end note*]
|
| 567 |
|
| 568 |
#### Initialization by conversion function for direct reference binding <a id="over.match.ref">[[over.match.ref]]</a>
|
| 569 |
|
| 570 |
Under the conditions specified in [[dcl.init.ref]], a reference can be
|
| 571 |
bound directly to a glvalue or class prvalue that is the result of
|
| 572 |
applying a conversion function to an initializer expression. Overload
|
| 573 |
resolution is used to select the conversion function to be invoked.
|
| 574 |
+
Assuming that “reference to *cv1* `T`” is the type of the reference
|
| 575 |
+
being initialized, and “cv `S`” is the type of the initializer
|
| 576 |
+
expression, with `S` a class type, the candidate functions are selected
|
| 577 |
+
as follows:
|
| 578 |
|
| 579 |
- The conversion functions of `S` and its base classes are considered.
|
| 580 |
Those non-explicit conversion functions that are not hidden within `S`
|
| 581 |
and yield type “lvalue reference to *cv2* `T2`” (when initializing an
|
| 582 |
+
lvalue reference or an rvalue reference to function) or “*cv2* `T2`”
|
| 583 |
+
or “rvalue reference to *cv2* `T2`” (when initializing an rvalue
|
| 584 |
+
reference or an lvalue reference to function), where “*cv1* `T`” is
|
| 585 |
reference-compatible ([[dcl.init.ref]]) with “*cv2* `T2`”, are
|
| 586 |
candidate functions. For direct-initialization, those explicit
|
| 587 |
conversion functions that are not hidden within `S` and yield type
|
| 588 |
“lvalue reference to *cv2* `T2`” or “*cv2* `T2`” or “rvalue reference
|
| 589 |
+
to *cv2* `T2`”, respectively, where `T2` is the same type as `T` or
|
| 590 |
can be converted to type `T` with a qualification conversion (
|
| 591 |
[[conv.qual]]), are also candidate functions.
|
| 592 |
|
| 593 |
The argument list has one argument, which is the initializer expression.
|
| 594 |
+
|
| 595 |
+
[*Note 1*: This argument will be compared against the implicit object
|
| 596 |
+
parameter of the conversion functions. — *end note*]
|
| 597 |
|
| 598 |
#### Initialization by list-initialization <a id="over.match.list">[[over.match.list]]</a>
|
| 599 |
|
| 600 |
+
When objects of non-aggregate class type `T` are list-initialized such
|
| 601 |
+
that [[dcl.init.list]] specifies that overload resolution is performed
|
| 602 |
+
according to the rules in this section, overload resolution selects the
|
| 603 |
+
constructor in two phases:
|
| 604 |
|
| 605 |
- Initially, the candidate functions are the initializer-list
|
| 606 |
constructors ([[dcl.init.list]]) of the class `T` and the argument
|
| 607 |
list consists of the initializer list as a single argument.
|
| 608 |
- If no viable initializer-list constructor is found, overload
|
|
|
|
| 611 |
the elements of the initializer list.
|
| 612 |
|
| 613 |
If the initializer list has no elements and `T` has a default
|
| 614 |
constructor, the first phase is omitted. In copy-list-initialization, if
|
| 615 |
an `explicit` constructor is chosen, the initialization is ill-formed.
|
| 616 |
+
|
| 617 |
+
[*Note 1*: This differs from other situations ([[over.match.ctor]],
|
| 618 |
[[over.match.copy]]), where only converting constructors are considered
|
| 619 |
for copy-initialization. This restriction only applies if this
|
| 620 |
+
initialization is part of the final result of overload
|
| 621 |
+
resolution. — *end note*]
|
| 622 |
+
|
| 623 |
+
#### Class template argument deduction <a id="over.match.class.deduct">[[over.match.class.deduct]]</a>
|
| 624 |
+
|
| 625 |
+
A set of functions and function templates is formed comprising:
|
| 626 |
+
|
| 627 |
+
- For each constructor of the primary class template designated by the
|
| 628 |
+
*template-name*, if the template is defined, a function template with
|
| 629 |
+
the following properties:
|
| 630 |
+
- The template parameters are the template parameters of the class
|
| 631 |
+
template followed by the template parameters (including default
|
| 632 |
+
template arguments) of the constructor, if any.
|
| 633 |
+
- The types of the function parameters are those of the constructor.
|
| 634 |
+
- The return type is the class template specialization designated by
|
| 635 |
+
the *template-name* and template arguments corresponding to the
|
| 636 |
+
template parameters obtained from the class template.
|
| 637 |
+
- If the primary class template `C` is not defined or does not declare
|
| 638 |
+
any constructors, an additional function template derived as above
|
| 639 |
+
from a hypothetical constructor `C()`.
|
| 640 |
+
- An additional function template derived as above from a hypothetical
|
| 641 |
+
constructor `C(C)`, called the *copy deduction candidate*.
|
| 642 |
+
- For each *deduction-guide*, a function or function template with the
|
| 643 |
+
following properties:
|
| 644 |
+
- The template parameters, if any, and function parameters are those
|
| 645 |
+
of the *deduction-guide*.
|
| 646 |
+
- The return type is the *simple-template-id* of the
|
| 647 |
+
*deduction-guide*.
|
| 648 |
+
|
| 649 |
+
Initialization and overload resolution are performed as described in
|
| 650 |
+
[[dcl.init]] and [[over.match.ctor]], [[over.match.copy]], or
|
| 651 |
+
[[over.match.list]] (as appropriate for the type of initialization
|
| 652 |
+
performed) for an object of a hypothetical class type, where the
|
| 653 |
+
selected functions and function templates are considered to be the
|
| 654 |
+
constructors of that class type for the purpose of forming an overload
|
| 655 |
+
set, and the initializer is provided by the context in which class
|
| 656 |
+
template argument deduction was performed. Each such notional
|
| 657 |
+
constructor is considered to be explicit if the function or function
|
| 658 |
+
template was generated from a constructor or *deduction-guide* that was
|
| 659 |
+
declared `explicit`. All such notional constructors are considered to be
|
| 660 |
+
public members of the hypothetical class type.
|
| 661 |
+
|
| 662 |
+
[*Example 1*:
|
| 663 |
+
|
| 664 |
+
``` cpp
|
| 665 |
+
template <class T> struct A {
|
| 666 |
+
explicit A(const T&, ...) noexcept; // #1
|
| 667 |
+
A(T&&, ...); // #2
|
| 668 |
+
};
|
| 669 |
+
|
| 670 |
+
int i;
|
| 671 |
+
A a1 = { i, i }; // error: explicit constructor #1 selected in copy-list-initialization during deduction,
|
| 672 |
+
// cannot deduce from non-forwarding rvalue reference in #2
|
| 673 |
+
|
| 674 |
+
A a2{i, i}; // OK, #1 deduces to A<int> and also initializes
|
| 675 |
+
A a3{0, i}; // OK, #2 deduces to A<int> and also initializes
|
| 676 |
+
A a4 = {0, i}; // OK, #2 deduces to A<int> and also initializes
|
| 677 |
+
|
| 678 |
+
template <class T> A(const T&, const T&) -> A<T&>; // #3
|
| 679 |
+
template <class T> explicit A(T&&, T&&) -> A<T>; // #4
|
| 680 |
+
|
| 681 |
+
A a5 = {0, 1}; // error: explicit deduction guide #4 selected in copy-list-initialization during deduction
|
| 682 |
+
A a6{0,1}; // OK, #4 deduces to A<int> and #2 initializes
|
| 683 |
+
A a7 = {0, i}; // error: #3 deduces to A<int&>, #1 and #2 declare same constructor
|
| 684 |
+
A a8{0,i}; // error: #3 deduces to A<int&>, #1 and #2 declare same constructor
|
| 685 |
+
|
| 686 |
+
template <class T> struct B {
|
| 687 |
+
template <class U> using TA = T;
|
| 688 |
+
template <class U> B(U, TA<U>);
|
| 689 |
+
};
|
| 690 |
+
|
| 691 |
+
B b{(int*)0, (char*)0}; // OK, deduces B<char*>
|
| 692 |
+
```
|
| 693 |
+
|
| 694 |
+
— *end example*]
|
| 695 |
|
| 696 |
### Viable functions <a id="over.match.viable">[[over.match.viable]]</a>
|
| 697 |
|
| 698 |
From the set of candidate functions constructed for a given context (
|
| 699 |
[[over.match.funcs]]), a set of viable functions is chosen, from which
|
|
|
|
| 729 |
|
| 730 |
### Best viable function <a id="over.match.best">[[over.match.best]]</a>
|
| 731 |
|
| 732 |
Define ICS*i*(`F`) as follows:
|
| 733 |
|
| 734 |
+
- If `F` is a static member function, ICS*1*(`F`) is defined such that
|
| 735 |
ICS*1*(`F`) is neither better nor worse than ICS*1*(`G`) for any
|
| 736 |
function `G`, and, symmetrically, ICS*1*(`G`) is neither better nor
|
| 737 |
+
worse than ICS*1*(`F`);[^9] otherwise,
|
| 738 |
- let ICS*i*(`F`) denote the implicit conversion sequence that converts
|
| 739 |
the *i*-th argument in the list to the type of the *i*-th parameter of
|
| 740 |
viable function `F`. [[over.best.ics]] defines the implicit conversion
|
| 741 |
sequences and [[over.ics.rank]] defines what it means for one implicit
|
| 742 |
conversion sequence to be a better conversion sequence or worse
|
|
|
|
| 752 |
- the context is an initialization by user-defined conversion (see
|
| 753 |
[[dcl.init]], [[over.match.conv]], and [[over.match.ref]]) and the
|
| 754 |
standard conversion sequence from the return type of `F1` to the
|
| 755 |
destination type (i.e., the type of the entity being initialized) is a
|
| 756 |
better conversion sequence than the standard conversion sequence from
|
| 757 |
+
the return type of `F2` to the destination type
|
| 758 |
+
\[*Example 1*:
|
| 759 |
``` cpp
|
| 760 |
struct A {
|
| 761 |
A();
|
| 762 |
operator int();
|
| 763 |
operator double();
|
| 764 |
} a;
|
| 765 |
+
int i = a; // a.operator int() followed by no conversion is better than
|
| 766 |
+
// a.operator double() followed by a conversion to int
|
|
|
|
| 767 |
float x = a; // ambiguous: both possibilities require conversions,
|
| 768 |
// and neither is better than the other
|
| 769 |
```
|
| 770 |
|
| 771 |
+
— *end example*]
|
| 772 |
or, if not that,
|
| 773 |
- the context is an initialization by conversion function for direct
|
| 774 |
reference binding ([[over.match.ref]]) of a reference to function
|
| 775 |
type, the return type of `F1` is the same kind of reference (i.e.
|
| 776 |
lvalue or rvalue) as the reference being initialized, and the return
|
| 777 |
type of `F2` is not
|
| 778 |
+
\[*Example 2*:
|
| 779 |
``` cpp
|
| 780 |
template <class T> struct A {
|
| 781 |
operator T&(); // #1
|
| 782 |
operator T&&(); // #2
|
| 783 |
};
|
|
|
|
| 785 |
A<Fn> a;
|
| 786 |
Fn& lf = a; // calls #1
|
| 787 |
Fn&& rf = a; // calls #2
|
| 788 |
```
|
| 789 |
|
| 790 |
+
— *end example*]
|
| 791 |
or, if not that,
|
| 792 |
- `F1` is not a function template specialization and `F2` is a function
|
| 793 |
template specialization, or, if not that,
|
| 794 |
- `F1` and `F2` are function template specializations, and the function
|
| 795 |
template for `F1` is more specialized than the template for `F2`
|
| 796 |
according to the partial ordering rules described in
|
| 797 |
+
[[temp.func.order]], or, if not that,
|
| 798 |
+
- `F1` is generated from a *deduction-guide* (
|
| 799 |
+
[[over.match.class.deduct]]) and `F2` is not, or, if not that,
|
| 800 |
+
- `F1` is the copy deduction candidate ([[over.match.class.deduct]])
|
| 801 |
+
and `F2` is not, or, if not that,
|
| 802 |
+
- `F1` is generated from a non-template constructor and `F2` is
|
| 803 |
+
generated from a constructor template.
|
| 804 |
+
\[*Example 3*:
|
| 805 |
+
``` cpp
|
| 806 |
+
template <class T> struct A {
|
| 807 |
+
using value_type = T;
|
| 808 |
+
A(value_type); // #1
|
| 809 |
+
A(const A&); // #2
|
| 810 |
+
A(T, T, int); // #3
|
| 811 |
+
template<class U>
|
| 812 |
+
A(int, T, U); // #4
|
| 813 |
+
// #5 is the copy deduction candidate, A(A)
|
| 814 |
+
};
|
| 815 |
+
|
| 816 |
+
A x(1, 2, 3); // uses #3, generated from a non-template constructor
|
| 817 |
+
|
| 818 |
+
template <class T>
|
| 819 |
+
A(T) -> A<T>; // #6, less specialized than #5
|
| 820 |
+
|
| 821 |
+
A a(42); // uses #6 to deduce A<int> and #1 to initialize
|
| 822 |
+
A b = a; // uses #5 to deduce A<int> and #2 to initialize
|
| 823 |
+
|
| 824 |
+
template <class T>
|
| 825 |
+
A(A<T>) -> A<A<T>>; // #7, as specialized as #5
|
| 826 |
+
|
| 827 |
+
A b2 = a; // uses #7 to deduce A<A<int>> and #1 to initialize
|
| 828 |
+
```
|
| 829 |
+
|
| 830 |
+
— *end example*]
|
| 831 |
|
| 832 |
If there is exactly one viable function that is a better function than
|
| 833 |
all other viable functions, then it is the one selected by overload
|
| 834 |
+
resolution; otherwise the call is ill-formed.[^10]
|
| 835 |
+
|
| 836 |
+
[*Example 4*:
|
| 837 |
|
| 838 |
``` cpp
|
| 839 |
void Fcn(const int*, short);
|
| 840 |
void Fcn(int*, int);
|
| 841 |
|
| 842 |
int i;
|
| 843 |
short s = 0;
|
| 844 |
|
| 845 |
void f() {
|
| 846 |
+
Fcn(&i, s); // is ambiguous because &i → int* is better than &i → const int*
|
|
|
|
| 847 |
// but s → short is also better than s → int
|
| 848 |
|
| 849 |
+
Fcn(&i, 1L); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
|
|
|
| 850 |
// and 1L → short and 1L → int are indistinguishable
|
| 851 |
|
| 852 |
+
Fcn(&i, 'c'); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
|
|
|
| 853 |
// and c → int is better than c → short
|
| 854 |
}
|
| 855 |
```
|
| 856 |
|
| 857 |
+
— *end example*]
|
| 858 |
+
|
| 859 |
If the best viable function resolves to a function for which multiple
|
| 860 |
declarations were found, and if at least two of these declarations — or
|
| 861 |
the declarations they refer to in the case of *using-declaration*s —
|
| 862 |
specify a default argument that made the function viable, the program is
|
| 863 |
ill-formed.
|
| 864 |
|
| 865 |
+
[*Example 5*:
|
| 866 |
+
|
| 867 |
``` cpp
|
| 868 |
namespace A {
|
| 869 |
extern "C" void f(int = 5);
|
| 870 |
}
|
| 871 |
namespace B {
|
|
|
|
| 879 |
f(3); // OK, default argument was not used for viability
|
| 880 |
f(); // Error: found default argument twice
|
| 881 |
}
|
| 882 |
```
|
| 883 |
|
| 884 |
+
— *end example*]
|
| 885 |
+
|
| 886 |
#### Implicit conversion sequences <a id="over.best.ics">[[over.best.ics]]</a>
|
| 887 |
|
| 888 |
An *implicit conversion sequence* is a sequence of conversions used to
|
| 889 |
convert an argument in a function call to the type of the corresponding
|
| 890 |
parameter of the function being called. The sequence of conversions is
|
|
|
|
| 893 |
single expression ([[dcl.init]], [[dcl.init.ref]]).
|
| 894 |
|
| 895 |
Implicit conversion sequences are concerned only with the type,
|
| 896 |
cv-qualification, and value category of the argument and how these are
|
| 897 |
converted to match the corresponding properties of the parameter. Other
|
| 898 |
+
properties, such as the lifetime, storage class, alignment,
|
| 899 |
+
accessibility of the argument, whether the argument is a bit-field, and
|
| 900 |
+
whether a function is deleted ([[dcl.fct.def.delete]]), are ignored.
|
| 901 |
+
So, although an implicit conversion sequence can be defined for a given
|
| 902 |
+
argument-parameter pair, the conversion from the argument to the
|
| 903 |
+
parameter might still be ill-formed in the final analysis.
|
| 904 |
|
| 905 |
A well-formed implicit conversion sequence is one of the following
|
| 906 |
forms:
|
| 907 |
|
| 908 |
- a *standard conversion sequence* ([[over.ics.scs]]),
|
|
|
|
| 920 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
| 921 |
step of a class copy-initialization,
|
| 922 |
- [[over.match.copy]], [[over.match.conv]], or [[over.match.ref]] (in
|
| 923 |
all cases), or
|
| 924 |
- the second phase of [[over.match.list]] when the initializer list has
|
| 925 |
+
exactly one element that is itself an initializer list, and the target
|
| 926 |
+
is the first parameter of a constructor of class `X`, and the
|
| 927 |
+
conversion is to `X` or reference to cv `X`,
|
| 928 |
|
| 929 |
+
user-defined conversion sequences are not considered.
|
| 930 |
+
|
| 931 |
+
[*Note 1*: These rules prevent more than one user-defined conversion
|
| 932 |
+
from being applied during overload resolution, thereby avoiding infinite
|
| 933 |
+
recursion. — *end note*]
|
| 934 |
+
|
| 935 |
+
[*Example 1*:
|
| 936 |
|
| 937 |
``` cpp
|
| 938 |
struct Y { Y(int); };
|
| 939 |
struct A { operator int(); };
|
| 940 |
Y y1 = A(); // error: A::operator int() is not a candidate
|
|
|
|
| 943 |
struct B { operator X(); };
|
| 944 |
B b;
|
| 945 |
X x({b}); // error: B::operator X() is not a candidate
|
| 946 |
```
|
| 947 |
|
| 948 |
+
— *end example*]
|
| 949 |
+
|
| 950 |
For the case where the parameter type is a reference, see
|
| 951 |
[[over.ics.ref]].
|
| 952 |
|
| 953 |
When the parameter type is not a reference, the implicit conversion
|
| 954 |
sequence models a copy-initialization of the parameter from the argument
|
| 955 |
expression. The implicit conversion sequence is the one required to
|
| 956 |
convert the argument expression to a prvalue of the type of the
|
| 957 |
+
parameter.
|
| 958 |
+
|
| 959 |
+
[*Note 2*: When the parameter has a class type, this is a conceptual
|
| 960 |
conversion defined for the purposes of Clause [[over]]; the actual
|
| 961 |
initialization is defined in terms of constructors and is not a
|
| 962 |
+
conversion. — *end note*]
|
| 963 |
+
|
| 964 |
+
Any difference in top-level cv-qualification is subsumed by the
|
| 965 |
+
initialization itself and does not constitute a conversion.
|
| 966 |
+
|
| 967 |
+
[*Example 2*: A parameter of type `A` can be initialized from an
|
| 968 |
+
argument of type `const A`. The implicit conversion sequence for that
|
| 969 |
+
case is the identity sequence; it contains no “conversion” from
|
| 970 |
+
`const A` to `A`. — *end example*]
|
| 971 |
+
|
| 972 |
When the parameter has a class type and the argument expression has the
|
| 973 |
same type, the implicit conversion sequence is an identity conversion.
|
| 974 |
When the parameter has a class type and the argument expression has a
|
| 975 |
derived class type, the implicit conversion sequence is a
|
| 976 |
derived-to-base Conversion from the derived class to the base class.
|
| 977 |
+
|
| 978 |
+
[*Note 3*: There is no such standard conversion; this derived-to-base
|
| 979 |
+
Conversion exists only in the description of implicit conversion
|
| 980 |
+
sequences. — *end note*]
|
| 981 |
+
|
| 982 |
+
A derived-to-base Conversion has Conversion rank ([[over.ics.scs]]).
|
| 983 |
|
| 984 |
In all contexts, when converting to the implicit object parameter or
|
| 985 |
when converting to the left operand of an assignment operation only
|
| 986 |
+
standard conversion sequences are allowed.
|
|
|
|
| 987 |
|
| 988 |
If no conversions are required to match an argument to a parameter type,
|
| 989 |
the implicit conversion sequence is the standard conversion sequence
|
| 990 |
consisting of the identity conversion ([[over.ics.scs]]).
|
| 991 |
|
| 992 |
If no sequence of conversions can be found to convert an argument to a
|
| 993 |
+
parameter type, an implicit conversion sequence cannot be formed.
|
|
|
|
| 994 |
|
| 995 |
If several different sequences of conversions exist that each convert
|
| 996 |
the argument to the parameter type, the implicit conversion sequence
|
| 997 |
associated with the parameter is defined to be the unique conversion
|
| 998 |
sequence designated the *ambiguous conversion sequence*. For the purpose
|
| 999 |
of ranking implicit conversion sequences as described in
|
| 1000 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 1001 |
+
user-defined conversion sequence that is indistinguishable from any
|
| 1002 |
+
other user-defined conversion sequence.
|
| 1003 |
+
|
| 1004 |
+
[*Note 4*:
|
| 1005 |
+
|
| 1006 |
+
This rule prevents a function from becoming non-viable because of an
|
| 1007 |
+
ambiguous conversion sequence for one of its parameters.
|
| 1008 |
+
|
| 1009 |
+
[*Example 3*:
|
| 1010 |
+
|
| 1011 |
+
``` cpp
|
| 1012 |
+
class B;
|
| 1013 |
+
class A { A (B&);};
|
| 1014 |
+
class B { operator A (); };
|
| 1015 |
+
class C { C (B&); };
|
| 1016 |
+
void f(A) { }
|
| 1017 |
+
void f(C) { }
|
| 1018 |
+
B b;
|
| 1019 |
+
f(b); // ill-formed: ambiguous because there is a conversion b → C (via constructor)
|
| 1020 |
+
// and an (ambiguous) conversion b → A (via constructor or conversion function)
|
| 1021 |
+
void f(B) { }
|
| 1022 |
+
f(b); // OK, unambiguous
|
| 1023 |
+
```
|
| 1024 |
+
|
| 1025 |
+
— *end example*]
|
| 1026 |
+
|
| 1027 |
+
— *end note*]
|
| 1028 |
+
|
| 1029 |
+
If a function that uses the ambiguous conversion sequence is selected as
|
| 1030 |
+
the best viable function, the call will be ill-formed because the
|
| 1031 |
+
conversion of one of the arguments in the call is ambiguous.
|
| 1032 |
|
| 1033 |
The three forms of implicit conversion sequences mentioned above are
|
| 1034 |
defined in the following subclauses.
|
| 1035 |
|
| 1036 |
##### Standard conversion sequences <a id="over.ics.scs">[[over.ics.scs]]</a>
|
| 1037 |
|
| 1038 |
Table [[tab:over.conversions]] summarizes the conversions defined in
|
| 1039 |
Clause [[conv]] and partitions them into four disjoint categories:
|
| 1040 |
Lvalue Transformation, Qualification Adjustment, Promotion, and
|
| 1041 |
+
Conversion.
|
| 1042 |
+
|
| 1043 |
+
[*Note 5*: These categories are orthogonal with respect to value
|
| 1044 |
category, cv-qualification, and data representation: the Lvalue
|
| 1045 |
Transformations do not change the cv-qualification or data
|
| 1046 |
representation of the type; the Qualification Adjustments do not change
|
| 1047 |
the value category or data representation of the type; and the
|
| 1048 |
Promotions and Conversions do not change the value category or
|
| 1049 |
+
cv-qualification of the type. — *end note*]
|
| 1050 |
|
| 1051 |
+
[*Note 6*: As described in Clause [[conv]], a standard conversion
|
| 1052 |
+
sequence is either the Identity conversion by itself (that is, no
|
| 1053 |
+
conversion) or consists of one to three conversions from the other four
|
| 1054 |
+
categories. If there are two or more conversions in the sequence, the
|
| 1055 |
+
conversions are applied in the canonical order: **Lvalue
|
|
|
|
| 1056 |
Transformation**, **Promotion** or **Conversion**, **Qualification
|
| 1057 |
+
Adjustment**. — *end note*]
|
| 1058 |
|
| 1059 |
Each conversion in Table [[tab:over.conversions]] also has an
|
| 1060 |
associated rank (Exact Match, Promotion, or Conversion). These are used
|
| 1061 |
to rank standard conversion sequences ([[over.ics.rank]]). The rank of
|
| 1062 |
a conversion sequence is determined by considering the rank of each
|
|
|
|
| 1117 |
an argument expression, the implicit conversion sequence is the identity
|
| 1118 |
conversion, unless the argument expression has a type that is a derived
|
| 1119 |
class of the parameter type, in which case the implicit conversion
|
| 1120 |
sequence is a derived-to-base Conversion ([[over.best.ics]]).
|
| 1121 |
|
| 1122 |
+
[*Example 4*:
|
| 1123 |
+
|
| 1124 |
``` cpp
|
| 1125 |
struct A {};
|
| 1126 |
struct B : public A {} b;
|
| 1127 |
int f(A&);
|
| 1128 |
int f(B&);
|
| 1129 |
+
int i = f(b); // calls f(B&), an exact match, rather than f(A&), a conversion
|
|
|
|
| 1130 |
```
|
| 1131 |
|
| 1132 |
+
— *end example*]
|
| 1133 |
+
|
| 1134 |
If the parameter binds directly to the result of applying a conversion
|
| 1135 |
function to the argument expression, the implicit conversion sequence is
|
| 1136 |
a user-defined conversion sequence ([[over.ics.user]]), with the second
|
| 1137 |
standard conversion sequence either an identity conversion or, if the
|
| 1138 |
conversion function returns an entity of a type that is a derived class
|
| 1139 |
of the parameter type, a derived-to-base Conversion.
|
| 1140 |
|
| 1141 |
When a parameter of reference type is not bound directly to an argument
|
| 1142 |
expression, the conversion sequence is the one required to convert the
|
| 1143 |
+
argument expression to the referenced type according to
|
| 1144 |
+
[[over.best.ics]]. Conceptually, this conversion sequence corresponds to
|
| 1145 |
+
copy-initializing a temporary of the referenced type with the argument
|
| 1146 |
+
expression. Any difference in top-level cv-qualification is subsumed by
|
| 1147 |
+
the initialization itself and does not constitute a conversion.
|
|
|
|
| 1148 |
|
| 1149 |
Except for an implicit object parameter, for which see
|
| 1150 |
[[over.match.funcs]], a standard conversion sequence cannot be formed if
|
| 1151 |
it requires binding an lvalue reference other than a reference to a
|
| 1152 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 1153 |
+
an lvalue other than a function lvalue.
|
| 1154 |
+
|
| 1155 |
+
[*Note 7*: This means, for example, that a candidate function cannot be
|
| 1156 |
+
a viable function if it has a non-`const` lvalue reference parameter
|
| 1157 |
+
(other than the implicit object parameter) and the corresponding
|
| 1158 |
+
argument would require a temporary to be created to initialize the
|
| 1159 |
+
lvalue reference (see [[dcl.init.ref]]). — *end note*]
|
| 1160 |
|
| 1161 |
Other restrictions on binding a reference to a particular argument that
|
| 1162 |
are not based on the types of the reference and the argument do not
|
| 1163 |
+
affect the formation of a standard conversion sequence, however.
|
| 1164 |
+
|
| 1165 |
+
[*Example 5*: A function with an “lvalue reference to `int`” parameter
|
| 1166 |
+
can be a viable candidate even if the corresponding argument is an `int`
|
| 1167 |
+
bit-field. The formation of implicit conversion sequences treats the
|
| 1168 |
+
`int` bit-field as an `int` lvalue and finds an exact match with the
|
| 1169 |
+
parameter. If the function is selected by overload resolution, the call
|
| 1170 |
+
will nonetheless be ill-formed because of the prohibition on binding a
|
| 1171 |
+
non-`const` lvalue reference to a bit-field (
|
| 1172 |
+
[[dcl.init.ref]]). — *end example*]
|
| 1173 |
|
| 1174 |
##### List-initialization sequence <a id="over.ics.list">[[over.ics.list]]</a>
|
| 1175 |
|
| 1176 |
When an argument is an initializer list ([[dcl.init.list]]), it is not
|
| 1177 |
an expression and special rules apply for converting it to a parameter
|
| 1178 |
type.
|
| 1179 |
|
| 1180 |
+
If the parameter type is an aggregate class `X` and the initializer list
|
| 1181 |
+
has a single element of type cv `U`, where `U` is `X` or a class derived
|
| 1182 |
+
from `X`, the implicit conversion sequence is the one required to
|
| 1183 |
+
convert the element to the parameter type.
|
| 1184 |
+
|
| 1185 |
+
Otherwise, if the parameter type is a character array [^11] and the
|
| 1186 |
+
initializer list has a single element that is an appropriately-typed
|
| 1187 |
+
string literal ([[dcl.init.string]]), the implicit conversion sequence
|
| 1188 |
+
is the identity conversion.
|
| 1189 |
+
|
| 1190 |
+
Otherwise, if the parameter type is `std::initializer_list<X>` and all
|
| 1191 |
+
the elements of the initializer list can be implicitly converted to `X`,
|
| 1192 |
+
the implicit conversion sequence is the worst conversion necessary to
|
| 1193 |
+
convert an element of the list to `X`, or if the initializer list has no
|
| 1194 |
+
elements, the identity conversion. This conversion can be a user-defined
|
| 1195 |
conversion even in the context of a call to an initializer-list
|
| 1196 |
constructor.
|
| 1197 |
|
| 1198 |
+
[*Example 6*:
|
| 1199 |
+
|
| 1200 |
``` cpp
|
| 1201 |
void f(std::initializer_list<int>);
|
| 1202 |
f( {} ); // OK: f(initializer_list<int>) identity conversion
|
| 1203 |
f( {1,2,3} ); // OK: f(initializer_list<int>) identity conversion
|
| 1204 |
f( {'a','b'} ); // OK: f(initializer_list<int>) integral promotion
|
|
|
|
| 1217 |
typedef int IA[3];
|
| 1218 |
void h(const IA&);
|
| 1219 |
h({ 1, 2, 3 }); // OK: identity conversion
|
| 1220 |
```
|
| 1221 |
|
| 1222 |
+
— *end example*]
|
| 1223 |
+
|
| 1224 |
+
Otherwise, if the parameter type is “array of `N` `X`”, if there exists
|
| 1225 |
+
an implicit conversion sequence for each element of the array from the
|
| 1226 |
+
corresponding element of the initializer list (or from `{}` if there is
|
| 1227 |
+
no such element), the implicit conversion sequence is the worst such
|
| 1228 |
+
implicit conversion sequence.
|
| 1229 |
|
| 1230 |
Otherwise, if the parameter is a non-aggregate class `X` and overload
|
| 1231 |
+
resolution per [[over.match.list]] chooses a single best constructor
|
| 1232 |
+
`C` of `X` to perform the initialization of an object of type `X` from
|
| 1233 |
+
the argument initializer list:
|
| 1234 |
+
|
| 1235 |
+
- If `C` is not an initializer-list constructor and the initializer list
|
| 1236 |
+
has a single element of type cv `U`, where `U` is `X` or a class
|
| 1237 |
+
derived from `X`, the implicit conversion sequence has Exact Match
|
| 1238 |
+
rank if `U` is `X`, or Conversion rank if `U` is derived from `X`.
|
| 1239 |
+
- Otherwise, the implicit conversion sequence is a user-defined
|
| 1240 |
+
conversion sequence with the second standard conversion sequence an
|
| 1241 |
+
identity conversion.
|
| 1242 |
+
|
| 1243 |
+
If multiple constructors are viable but none is better than the others,
|
| 1244 |
+
the implicit conversion sequence is the ambiguous conversion sequence.
|
| 1245 |
+
User-defined conversions are allowed for conversion of the initializer
|
| 1246 |
+
list elements to the constructor parameter types except as noted in
|
| 1247 |
+
[[over.best.ics]].
|
| 1248 |
+
|
| 1249 |
+
[*Example 7*:
|
| 1250 |
|
| 1251 |
``` cpp
|
| 1252 |
struct A {
|
| 1253 |
A(std::initializer_list<int>);
|
| 1254 |
};
|
|
|
|
| 1276 |
};
|
| 1277 |
void i(D);
|
| 1278 |
i({ {1,2}, {"bar"} }); // OK: i(D(A(std::initializer_list<int>{1,2\), C(std::string("bar"))))}
|
| 1279 |
```
|
| 1280 |
|
| 1281 |
+
— *end example*]
|
| 1282 |
+
|
| 1283 |
Otherwise, if the parameter has an aggregate type which can be
|
| 1284 |
initialized from the initializer list according to the rules for
|
| 1285 |
aggregate initialization ([[dcl.init.aggr]]), the implicit conversion
|
| 1286 |
sequence is a user-defined conversion sequence with the second standard
|
| 1287 |
conversion sequence an identity conversion.
|
| 1288 |
|
| 1289 |
+
[*Example 8*:
|
| 1290 |
+
|
| 1291 |
``` cpp
|
| 1292 |
struct A {
|
| 1293 |
int m1;
|
| 1294 |
double m2;
|
| 1295 |
};
|
|
|
|
| 1297 |
void f(A);
|
| 1298 |
f( {'a', 'b'} ); // OK: f(A(int,double)) user-defined conversion
|
| 1299 |
f( {1.0} ); // error: narrowing
|
| 1300 |
```
|
| 1301 |
|
| 1302 |
+
— *end example*]
|
| 1303 |
+
|
| 1304 |
+
Otherwise, if the parameter is a reference, see [[over.ics.ref]].
|
| 1305 |
+
|
| 1306 |
+
[*Note 8*: The rules in this section will apply for initializing the
|
| 1307 |
+
underlying temporary for the reference. — *end note*]
|
| 1308 |
+
|
| 1309 |
+
[*Example 9*:
|
| 1310 |
|
| 1311 |
``` cpp
|
| 1312 |
struct A {
|
| 1313 |
int m1;
|
| 1314 |
double m2;
|
|
|
|
| 1320 |
|
| 1321 |
void g(const double &);
|
| 1322 |
g({1}); // same conversion as int to double
|
| 1323 |
```
|
| 1324 |
|
| 1325 |
+
— *end example*]
|
| 1326 |
+
|
| 1327 |
Otherwise, if the parameter type is not a class:
|
| 1328 |
|
| 1329 |
+
- if the initializer list has one element that is not itself an
|
| 1330 |
+
initializer list, the implicit conversion sequence is the one required
|
| 1331 |
+
to convert the element to the parameter type;
|
| 1332 |
+
\[*Example 10*:
|
| 1333 |
``` cpp
|
| 1334 |
void f(int);
|
| 1335 |
f( {'a'} ); // OK: same conversion as char to int
|
| 1336 |
f( {1.0} ); // error: narrowing
|
| 1337 |
```
|
| 1338 |
+
|
| 1339 |
+
— *end example*]
|
| 1340 |
- if the initializer list has no elements, the implicit conversion
|
| 1341 |
sequence is the identity conversion.
|
| 1342 |
+
\[*Example 11*:
|
| 1343 |
``` cpp
|
| 1344 |
void f(int);
|
| 1345 |
f( { } ); // OK: identity conversion
|
| 1346 |
```
|
| 1347 |
|
| 1348 |
+
— *end example*]
|
| 1349 |
+
|
| 1350 |
In all cases other than those enumerated above, no conversion is
|
| 1351 |
possible.
|
| 1352 |
|
| 1353 |
#### Ranking implicit conversion sequences <a id="over.ics.rank">[[over.ics.rank]]</a>
|
| 1354 |
|
| 1355 |
+
This subclause defines a partial ordering of implicit conversion
|
| 1356 |
sequences based on the relationships *better conversion sequence* and
|
| 1357 |
*better conversion*. If an implicit conversion sequence S1 is defined by
|
| 1358 |
these rules to be a better conversion sequence than S2, then it is also
|
| 1359 |
the case that S2 is a *worse conversion sequence* than S1. If conversion
|
| 1360 |
sequence S1 is neither better than nor worse than conversion sequence
|
|
|
|
| 1371 |
[[over.ics.ellipsis]]).
|
| 1372 |
|
| 1373 |
Two implicit conversion sequences of the same form are indistinguishable
|
| 1374 |
conversion sequences unless one of the following rules applies:
|
| 1375 |
|
| 1376 |
+
- List-initialization sequence `L1` is a better conversion sequence than
|
| 1377 |
+
list-initialization sequence `L2` if
|
| 1378 |
+
- `L1` converts to `std::initializer_list<X>` for some `X` and `L2`
|
| 1379 |
+
does not, or, if not that,
|
| 1380 |
+
- `L1` converts to type “array of `N1` `T`”, `L2` converts to type
|
| 1381 |
+
“array of `N2` `T`”, and `N1` is smaller than `N2`,
|
| 1382 |
+
|
| 1383 |
+
even if one of the other rules in this paragraph would otherwise
|
| 1384 |
+
apply.
|
| 1385 |
+
\[*Example 1*:
|
| 1386 |
+
``` cpp
|
| 1387 |
+
void f1(int); // #1
|
| 1388 |
+
void f1(std::initializer_list<long>); // #2
|
| 1389 |
+
void g1() { f1({42}); } // chooses #2
|
| 1390 |
+
|
| 1391 |
+
void f2(std::pair<const char*, const char*>); // #3
|
| 1392 |
+
void f2(std::initializer_list<std::string>); // #4
|
| 1393 |
+
void g2() { f2({"foo","bar"}); } // chooses #4
|
| 1394 |
+
```
|
| 1395 |
+
|
| 1396 |
+
— *end example*]
|
| 1397 |
- Standard conversion sequence `S1` is a better conversion sequence than
|
| 1398 |
standard conversion sequence `S2` if
|
| 1399 |
- `S1` is a proper subsequence of `S2` (comparing the conversion
|
| 1400 |
sequences in the canonical form defined by [[over.ics.scs]],
|
| 1401 |
excluding any Lvalue Transformation; the identity conversion
|
|
|
|
| 1405 |
have the same rank and are distinguishable by the rules in the
|
| 1406 |
paragraph below, or, if not that,
|
| 1407 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and neither
|
| 1408 |
refers to an implicit object parameter of a non-static member
|
| 1409 |
function declared without a *ref-qualifier*, and `S1` binds an
|
| 1410 |
+
rvalue reference to an rvalue and `S2` binds an lvalue reference
|
| 1411 |
+
\[*Example 2*:
|
| 1412 |
``` cpp
|
| 1413 |
int i;
|
| 1414 |
int f1();
|
| 1415 |
int&& f2();
|
| 1416 |
int g(const int&);
|
|
|
|
| 1432 |
a << 'c'; // calls A::operator<<(int)
|
| 1433 |
A().p(); // calls A::p()&&
|
| 1434 |
a.p(); // calls A::p()&
|
| 1435 |
```
|
| 1436 |
|
| 1437 |
+
— *end example*]
|
| 1438 |
or, if not that,
|
| 1439 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and `S1`
|
| 1440 |
binds an lvalue reference to a function lvalue and `S2` binds an
|
| 1441 |
+
rvalue reference to a function lvalue
|
| 1442 |
+
\[*Example 3*:
|
| 1443 |
``` cpp
|
| 1444 |
int f(void(&)()); // #1
|
| 1445 |
int f(void(&&)()); // #2
|
| 1446 |
void g();
|
| 1447 |
int i1 = f(g); // calls #1
|
| 1448 |
```
|
| 1449 |
|
| 1450 |
+
— *end example*]
|
| 1451 |
or, if not that,
|
| 1452 |
- `S1`
|
| 1453 |
and `S2` differ only in their qualification conversion and yield
|
| 1454 |
similar types `T1` and `T2` ([[conv.qual]]), respectively, and the
|
| 1455 |
cv-qualification signature of type `T1` is a proper subset of the
|
| 1456 |
+
cv-qualification signature of type `T2`
|
| 1457 |
+
\[*Example 4*:
|
| 1458 |
``` cpp
|
| 1459 |
int f(const volatile int *);
|
| 1460 |
int f(const int *);
|
| 1461 |
int i;
|
| 1462 |
int j = f(&i); // calls f(const int*)
|
| 1463 |
```
|
| 1464 |
|
| 1465 |
+
— *end example*]
|
| 1466 |
or, if not that,
|
| 1467 |
- `S1`
|
| 1468 |
and `S2` are reference bindings ([[dcl.init.ref]]), and the types
|
| 1469 |
to which the references refer are the same type except for top-level
|
| 1470 |
cv-qualifiers, and the type to which the reference initialized by
|
| 1471 |
`S2` refers is more cv-qualified than the type to which the
|
| 1472 |
reference initialized by `S1` refers.
|
| 1473 |
+
\[*Example 5*:
|
| 1474 |
``` cpp
|
| 1475 |
int f(const int &);
|
| 1476 |
int f(int &);
|
| 1477 |
int g(const int &);
|
| 1478 |
int g(int);
|
|
|
|
| 1488 |
void g(const X& a, X b) {
|
| 1489 |
a.f(); // calls X::f() const
|
| 1490 |
b.f(); // calls X::f()
|
| 1491 |
}
|
| 1492 |
```
|
| 1493 |
+
|
| 1494 |
+
— *end example*]
|
| 1495 |
- User-defined conversion sequence `U1` is a better conversion sequence
|
| 1496 |
than another user-defined conversion sequence `U2` if they contain the
|
| 1497 |
same user-defined conversion function or constructor or they
|
| 1498 |
initialize the same class in an aggregate initialization and in either
|
| 1499 |
case the second standard conversion sequence of `U1` is better than
|
| 1500 |
the second standard conversion sequence of `U2`.
|
| 1501 |
+
\[*Example 6*:
|
| 1502 |
``` cpp
|
| 1503 |
struct A {
|
| 1504 |
operator short();
|
| 1505 |
} a;
|
| 1506 |
int f(int);
|
| 1507 |
int f(float);
|
| 1508 |
int i = f(a); // calls f(int), because short → int is
|
| 1509 |
// better than short → float.
|
| 1510 |
```
|
| 1511 |
+
|
| 1512 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1513 |
|
| 1514 |
Standard conversion sequences are ordered by their ranks: an Exact Match
|
| 1515 |
is a better conversion than a Promotion, which is a better conversion
|
| 1516 |
than a Conversion. Two conversion sequences with the same rank are
|
| 1517 |
indistinguishable unless one of the following rules applies:
|
|
|
|
| 1527 |
of `B*` to `void*`.
|
| 1528 |
- If class `B` is derived directly or indirectly from class `A` and
|
| 1529 |
class `C` is derived directly or indirectly from `B`,
|
| 1530 |
- conversion of `C*` to `B*` is better than conversion of `C*` to
|
| 1531 |
`A*`,
|
| 1532 |
+
\[*Example 7*:
|
| 1533 |
``` cpp
|
| 1534 |
struct A {};
|
| 1535 |
struct B : public A {};
|
| 1536 |
struct C : public B {};
|
| 1537 |
C* pc;
|
| 1538 |
int f(A*);
|
| 1539 |
int f(B*);
|
| 1540 |
int i = f(pc); // calls f(B*)
|
| 1541 |
```
|
| 1542 |
+
|
| 1543 |
+
— *end example*]
|
| 1544 |
- binding of an expression of type `C` to a reference to type `B` is
|
| 1545 |
better than binding an expression of type `C` to a reference to type
|
| 1546 |
`A`,
|
| 1547 |
- conversion of `A::*` to `B::*` is better than conversion of `A::*`
|
| 1548 |
to `C::*`,
|
|
|
|
| 1554 |
`A`,
|
| 1555 |
- conversion of `B::*` to `C::*` is better than conversion of `A::*`
|
| 1556 |
to `C::*`, and
|
| 1557 |
- conversion of `B` to `A` is better than conversion of `C` to `A`.
|
| 1558 |
|
| 1559 |
+
\[*Note 1*: Compared conversion sequences will have different source
|
| 1560 |
+
types only in the context of comparing the second standard conversion
|
| 1561 |
+
sequence of an initialization by user-defined conversion (see
|
| 1562 |
+
[[over.match.best]]); in all other contexts, the source types will be
|
| 1563 |
+
the same and the target types will be different. — *end note*]
|
| 1564 |
|