tmp/tmpuuf9d83x/{from.md → to.md}
RENAMED
|
@@ -1,33 +1,29 @@
|
|
| 1 |
### Best viable function <a id="over.match.best">[[over.match.best]]</a>
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
worse than ICS*1*(`F`);[^8] otherwise,
|
| 9 |
-
- let ICS*i*(`F`) denote the implicit conversion sequence that converts
|
| 10 |
-
the *i*-th argument in the list to the type of the *i*-th parameter of
|
| 11 |
-
viable function `F`. [[over.best.ics]] defines the implicit conversion
|
| 12 |
sequences and [[over.ics.rank]] defines what it means for one implicit
|
| 13 |
conversion sequence to be a better conversion sequence or worse
|
| 14 |
conversion sequence than another.
|
| 15 |
|
| 16 |
-
Given these definitions, a viable function `
|
| 17 |
-
*better* function than another viable function `
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
- for some argument
|
| 22 |
-
|
| 23 |
- the context is an initialization by user-defined conversion (see
|
| 24 |
[[dcl.init]], [[over.match.conv]], and [[over.match.ref]]) and the
|
| 25 |
-
standard conversion sequence from the return type of `
|
| 26 |
destination type (i.e., the type of the entity being initialized) is a
|
| 27 |
better conversion sequence than the standard conversion sequence from
|
| 28 |
-
the return type of `
|
| 29 |
\[*Example 1*:
|
| 30 |
``` cpp
|
| 31 |
struct A {
|
| 32 |
A();
|
| 33 |
operator int();
|
|
@@ -70,11 +66,11 @@ and then
|
|
| 70 |
parameter-type-lists, and `F1` is more constrained than `F2` according
|
| 71 |
to the partial ordering of constraints described in
|
| 72 |
[[temp.constr.order]], or if not that,
|
| 73 |
- `F1` is a constructor for a class `D`, `F2` is a constructor for a
|
| 74 |
base class `B` of `D`, and for all arguments the corresponding
|
| 75 |
-
parameters of `F1` and `F2` have the same type
|
| 76 |
\[*Example 3*:
|
| 77 |
``` cpp
|
| 78 |
struct A {
|
| 79 |
A(int = 0);
|
| 80 |
};
|
|
@@ -114,10 +110,16 @@ and then
|
|
| 114 |
bool b = 1 < S(); // calls #2
|
| 115 |
```
|
| 116 |
|
| 117 |
— *end example*]
|
| 118 |
or, if not that
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
- `F1` is generated from a *deduction-guide* [[over.match.class.deduct]]
|
| 120 |
and `F2` is not, or, if not that,
|
| 121 |
- `F1` is the copy deduction candidate [[over.match.class.deduct]] and
|
| 122 |
`F2` is not, or, if not that,
|
| 123 |
- `F1` is generated from a non-template constructor and `F2` is
|
|
@@ -150,11 +152,11 @@ and then
|
|
| 150 |
|
| 151 |
— *end example*]
|
| 152 |
|
| 153 |
If there is exactly one viable function that is a better function than
|
| 154 |
all other viable functions, then it is the one selected by overload
|
| 155 |
-
resolution; otherwise the call is ill-formed.[^
|
| 156 |
|
| 157 |
[*Example 7*:
|
| 158 |
|
| 159 |
``` cpp
|
| 160 |
void Fcn(const int*, short);
|
|
@@ -169,21 +171,20 @@ void f() {
|
|
| 169 |
|
| 170 |
Fcn(&i, 1L); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
| 171 |
// and 1L → short and 1L → int are indistinguishable
|
| 172 |
|
| 173 |
Fcn(&i, 'c'); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
| 174 |
-
// and c → int is better than c → short
|
| 175 |
}
|
| 176 |
```
|
| 177 |
|
| 178 |
— *end example*]
|
| 179 |
|
| 180 |
If the best viable function resolves to a function for which multiple
|
| 181 |
-
declarations were found, and if
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
ill-formed.
|
| 185 |
|
| 186 |
[*Example 8*:
|
| 187 |
|
| 188 |
``` cpp
|
| 189 |
namespace A {
|
|
@@ -204,16 +205,18 @@ void use() {
|
|
| 204 |
|
| 205 |
— *end example*]
|
| 206 |
|
| 207 |
#### Implicit conversion sequences <a id="over.best.ics">[[over.best.ics]]</a>
|
| 208 |
|
|
|
|
|
|
|
| 209 |
An *implicit conversion sequence* is a sequence of conversions used to
|
| 210 |
convert an argument in a function call to the type of the corresponding
|
| 211 |
parameter of the function being called. The sequence of conversions is
|
| 212 |
an implicit conversion as defined in [[conv]], which means it is
|
| 213 |
governed by the rules for initialization of an object or reference by a
|
| 214 |
-
single expression
|
| 215 |
|
| 216 |
Implicit conversion sequences are concerned only with the type,
|
| 217 |
cv-qualification, and value category of the argument and how these are
|
| 218 |
converted to match the corresponding properties of the parameter.
|
| 219 |
|
|
@@ -233,11 +236,11 @@ forms:
|
|
| 233 |
- an ellipsis conversion sequence [[over.ics.ellipsis]].
|
| 234 |
|
| 235 |
However, if the target is
|
| 236 |
|
| 237 |
- the first parameter of a constructor or
|
| 238 |
-
- the
|
| 239 |
|
| 240 |
and the constructor or user-defined conversion function is a candidate
|
| 241 |
by
|
| 242 |
|
| 243 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
|
@@ -294,22 +297,29 @@ case is the identity sequence; it contains no “conversion” from
|
|
| 294 |
|
| 295 |
When the parameter has a class type and the argument expression has the
|
| 296 |
same type, the implicit conversion sequence is an identity conversion.
|
| 297 |
When the parameter has a class type and the argument expression has a
|
| 298 |
derived class type, the implicit conversion sequence is a
|
| 299 |
-
derived-to-base conversion from the derived class to the base class.
|
|
|
|
| 300 |
|
| 301 |
[*Note 4*: There is no such standard conversion; this derived-to-base
|
| 302 |
conversion exists only in the description of implicit conversion
|
| 303 |
sequences. — *end note*]
|
| 304 |
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
In all contexts, when converting to the implicit object parameter or
|
| 308 |
when converting to the left operand of an assignment operation only
|
| 309 |
standard conversion sequences are allowed.
|
| 310 |
|
|
|
|
|
|
|
|
|
|
| 311 |
If no conversions are required to match an argument to a parameter type,
|
| 312 |
the implicit conversion sequence is the standard conversion sequence
|
| 313 |
consisting of the identity conversion [[over.ics.scs]].
|
| 314 |
|
| 315 |
If no sequence of conversions can be found to convert an argument to a
|
|
@@ -322,11 +332,11 @@ conversion sequence designated the *ambiguous conversion sequence*. For
|
|
| 322 |
the purpose of ranking implicit conversion sequences as described in
|
| 323 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 324 |
user-defined conversion sequence that is indistinguishable from any
|
| 325 |
other user-defined conversion sequence.
|
| 326 |
|
| 327 |
-
[*Note
|
| 328 |
|
| 329 |
This rule prevents a function from becoming non-viable because of an
|
| 330 |
ambiguous conversion sequence for one of its parameters.
|
| 331 |
|
| 332 |
[*Example 3*:
|
|
@@ -360,19 +370,19 @@ defined in the following subclauses.
|
|
| 360 |
|
| 361 |
summarizes the conversions defined in [[conv]] and partitions them into
|
| 362 |
four disjoint categories: Lvalue Transformation, Qualification
|
| 363 |
Adjustment, Promotion, and Conversion.
|
| 364 |
|
| 365 |
-
[*Note
|
| 366 |
category, cv-qualification, and data representation: the Lvalue
|
| 367 |
Transformations do not change the cv-qualification or data
|
| 368 |
representation of the type; the Qualification Adjustments do not change
|
| 369 |
the value category or data representation of the type; and the
|
| 370 |
Promotions and Conversions do not change the value category or
|
| 371 |
cv-qualification of the type. — *end note*]
|
| 372 |
|
| 373 |
-
[*Note
|
| 374 |
either is the Identity conversion by itself (that is, no conversion) or
|
| 375 |
consists of one to three conversions from the other four categories. If
|
| 376 |
there are two or more conversions in the sequence, the conversions are
|
| 377 |
applied in the canonical order: **Lvalue Transformation**, **Promotion**
|
| 378 |
or **Conversion**, **Qualification Adjustment**. — *end note*]
|
|
@@ -400,14 +410,14 @@ Promotion rank; otherwise, the sequence has Exact Match rank.
|
|
| 400 |
A *user-defined conversion sequence* consists of an initial standard
|
| 401 |
conversion sequence followed by a user-defined conversion [[class.conv]]
|
| 402 |
followed by a second standard conversion sequence. If the user-defined
|
| 403 |
conversion is specified by a constructor [[class.conv.ctor]], the
|
| 404 |
initial standard conversion sequence converts the source type to the
|
| 405 |
-
type
|
| 406 |
conversion is specified by a conversion function [[class.conv.fct]], the
|
| 407 |
initial standard conversion sequence converts the source type to the
|
| 408 |
-
|
| 409 |
|
| 410 |
The second standard conversion sequence converts the result of the
|
| 411 |
user-defined conversion to the target type for the sequence; any
|
| 412 |
reference binding is included in the second standard conversion
|
| 413 |
sequence. Since an implicit conversion sequence is an initialization,
|
|
@@ -435,11 +445,11 @@ function called (see [[expr.call]]).
|
|
| 435 |
|
| 436 |
When a parameter of reference type binds directly [[dcl.init.ref]] to an
|
| 437 |
argument expression, the implicit conversion sequence is the identity
|
| 438 |
conversion, unless the argument expression has a type that is a derived
|
| 439 |
class of the parameter type, in which case the implicit conversion
|
| 440 |
-
sequence is a derived-to-base
|
| 441 |
|
| 442 |
[*Example 4*:
|
| 443 |
|
| 444 |
``` cpp
|
| 445 |
struct A {};
|
|
@@ -451,12 +461,12 @@ int i = f(b); // calls f(B&), an exact match, rather than f(A&), a convers
|
|
| 451 |
|
| 452 |
— *end example*]
|
| 453 |
|
| 454 |
If the parameter binds directly to the result of applying a conversion
|
| 455 |
function to the argument expression, the implicit conversion sequence is
|
| 456 |
-
a user-defined conversion sequence [[over.ics.user]]
|
| 457 |
-
standard conversion sequence either an identity conversion or, if the
|
| 458 |
conversion function returns an entity of a type that is a derived class
|
| 459 |
of the parameter type, a derived-to-base conversion.
|
| 460 |
|
| 461 |
When a parameter of reference type is not bound directly to an argument
|
| 462 |
expression, the conversion sequence is the one required to convert the
|
|
@@ -470,11 +480,11 @@ Except for an implicit object parameter, for which see
|
|
| 470 |
[[over.match.funcs]], an implicit conversion sequence cannot be formed
|
| 471 |
if it requires binding an lvalue reference other than a reference to a
|
| 472 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 473 |
an lvalue other than a function lvalue.
|
| 474 |
|
| 475 |
-
[*Note
|
| 476 |
a viable function if it has a non-`const` lvalue reference parameter
|
| 477 |
(other than the implicit object parameter) and the corresponding
|
| 478 |
argument would require a temporary to be created to initialize the
|
| 479 |
lvalue reference (see [[dcl.init.ref]]). — *end note*]
|
| 480 |
|
|
@@ -502,11 +512,11 @@ is only possible if the parameter has an aggregate type that can be
|
|
| 502 |
initialized from the initializer list according to the rules for
|
| 503 |
aggregate initialization [[dcl.init.aggr]], in which case the implicit
|
| 504 |
conversion sequence is a user-defined conversion sequence whose second
|
| 505 |
standard conversion sequence is an identity conversion.
|
| 506 |
|
| 507 |
-
[*Note
|
| 508 |
|
| 509 |
Aggregate initialization does not require that the members are declared
|
| 510 |
in designation order. If, after overload resolution, the order does not
|
| 511 |
match for the selected overload, the initialization of the parameter
|
| 512 |
will be ill-formed [[dcl.init.list]].
|
|
@@ -535,14 +545,15 @@ void h() {
|
|
| 535 |
Otherwise, if the parameter type is an aggregate class `X` and the
|
| 536 |
initializer list has a single element of type cv `U`, where `U` is `X`
|
| 537 |
or a class derived from `X`, the implicit conversion sequence is the one
|
| 538 |
required to convert the element to the parameter type.
|
| 539 |
|
| 540 |
-
Otherwise, if the parameter type is a character array
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
|
|
|
| 544 |
|
| 545 |
Otherwise, if the parameter type is `std::initializer_list<X>` and all
|
| 546 |
the elements of the initializer list can be implicitly converted to `X`,
|
| 547 |
the implicit conversion sequence is the worst conversion necessary to
|
| 548 |
convert an element of the list to `X`, or if the initializer list has no
|
|
@@ -552,13 +563,13 @@ constructor.
|
|
| 552 |
|
| 553 |
[*Example 7*:
|
| 554 |
|
| 555 |
``` cpp
|
| 556 |
void f(std::initializer_list<int>);
|
| 557 |
-
f( {} ); // OK
|
| 558 |
-
f( {1,2,3} ); // OK
|
| 559 |
-
f( {'a','b'} ); // OK
|
| 560 |
f( {1.0} ); // error: narrowing
|
| 561 |
|
| 562 |
struct A {
|
| 563 |
A(std::initializer_list<double>); // #1
|
| 564 |
A(std::initializer_list<complex<double>>); // #2
|
|
@@ -569,11 +580,11 @@ A a{ 1.0,2.0 }; // OK, uses #1
|
|
| 569 |
void g(A);
|
| 570 |
g({ "foo", "bar" }); // OK, uses #3
|
| 571 |
|
| 572 |
typedef int IA[3];
|
| 573 |
void h(const IA&);
|
| 574 |
-
h({ 1, 2, 3 }); // OK
|
| 575 |
```
|
| 576 |
|
| 577 |
— *end example*]
|
| 578 |
|
| 579 |
Otherwise, if the parameter type is “array of `N` `X`” or “array of
|
|
@@ -591,11 +602,11 @@ the argument initializer list:
|
|
| 591 |
- If `C` is not an initializer-list constructor and the initializer list
|
| 592 |
has a single element of type cv `U`, where `U` is `X` or a class
|
| 593 |
derived from `X`, the implicit conversion sequence has Exact Match
|
| 594 |
rank if `U` is `X`, or Conversion rank if `U` is derived from `X`.
|
| 595 |
- Otherwise, the implicit conversion sequence is a user-defined
|
| 596 |
-
conversion sequence
|
| 597 |
identity conversion.
|
| 598 |
|
| 599 |
If multiple constructors are viable but none is better than the others,
|
| 600 |
the implicit conversion sequence is the ambiguous conversion sequence.
|
| 601 |
User-defined conversions are allowed for conversion of the initializer
|
|
@@ -607,61 +618,61 @@ list elements to the constructor parameter types except as noted in
|
|
| 607 |
``` cpp
|
| 608 |
struct A {
|
| 609 |
A(std::initializer_list<int>);
|
| 610 |
};
|
| 611 |
void f(A);
|
| 612 |
-
f( {'a', 'b'} ); // OK
|
| 613 |
|
| 614 |
struct B {
|
| 615 |
B(int, double);
|
| 616 |
};
|
| 617 |
void g(B);
|
| 618 |
-
g( {'a', 'b'} ); // OK
|
| 619 |
g( {1.0, 1.0} ); // error: narrowing
|
| 620 |
|
| 621 |
void f(B);
|
| 622 |
f( {'a', 'b'} ); // error: ambiguous f(A) or f(B)
|
| 623 |
|
| 624 |
struct C {
|
| 625 |
C(std::string);
|
| 626 |
};
|
| 627 |
void h(C);
|
| 628 |
-
h({"foo"}); // OK
|
| 629 |
|
| 630 |
struct D {
|
| 631 |
D(A, C);
|
| 632 |
};
|
| 633 |
void i(D);
|
| 634 |
-
i({ {1,2}, {"bar"} }); // OK
|
| 635 |
```
|
| 636 |
|
| 637 |
— *end example*]
|
| 638 |
|
| 639 |
Otherwise, if the parameter has an aggregate type which can be
|
| 640 |
initialized from the initializer list according to the rules for
|
| 641 |
aggregate initialization [[dcl.init.aggr]], the implicit conversion
|
| 642 |
-
sequence is a user-defined conversion sequence
|
| 643 |
-
conversion sequence an identity conversion.
|
| 644 |
|
| 645 |
[*Example 9*:
|
| 646 |
|
| 647 |
``` cpp
|
| 648 |
struct A {
|
| 649 |
int m1;
|
| 650 |
double m2;
|
| 651 |
};
|
| 652 |
|
| 653 |
void f(A);
|
| 654 |
-
f( {'a', 'b'} ); // OK
|
| 655 |
f( {1.0} ); // error: narrowing
|
| 656 |
```
|
| 657 |
|
| 658 |
— *end example*]
|
| 659 |
|
| 660 |
Otherwise, if the parameter is a reference, see [[over.ics.ref]].
|
| 661 |
|
| 662 |
-
[*Note
|
| 663 |
underlying temporary for the reference. — *end note*]
|
| 664 |
|
| 665 |
[*Example 10*:
|
| 666 |
|
| 667 |
``` cpp
|
|
@@ -669,11 +680,11 @@ struct A {
|
|
| 669 |
int m1;
|
| 670 |
double m2;
|
| 671 |
};
|
| 672 |
|
| 673 |
void f(const A&);
|
| 674 |
-
f( {'a', 'b'} ); // OK
|
| 675 |
f( {1.0} ); // error: narrowing
|
| 676 |
|
| 677 |
void g(const double &);
|
| 678 |
g({1}); // same conversion as int to double
|
| 679 |
```
|
|
@@ -686,21 +697,21 @@ Otherwise, if the parameter type is not a class:
|
|
| 686 |
initializer list, the implicit conversion sequence is the one required
|
| 687 |
to convert the element to the parameter type;
|
| 688 |
\[*Example 11*:
|
| 689 |
``` cpp
|
| 690 |
void f(int);
|
| 691 |
-
f( {'a'} ); // OK
|
| 692 |
f( {1.0} ); // error: narrowing
|
| 693 |
```
|
| 694 |
|
| 695 |
— *end example*]
|
| 696 |
- if the initializer list has no elements, the implicit conversion
|
| 697 |
sequence is the identity conversion.
|
| 698 |
\[*Example 12*:
|
| 699 |
``` cpp
|
| 700 |
void f(int);
|
| 701 |
-
f( { } ); // OK
|
| 702 |
```
|
| 703 |
|
| 704 |
— *end example*]
|
| 705 |
|
| 706 |
In all cases other than those enumerated above, no conversion is
|
|
@@ -889,19 +900,41 @@ indistinguishable unless one of the following rules applies:
|
|
| 889 |
- A conversion that does not convert a pointer or a pointer to member to
|
| 890 |
`bool` is better than one that does.
|
| 891 |
- A conversion that promotes an enumeration whose underlying type is
|
| 892 |
fixed to its underlying type is better than one that promotes to the
|
| 893 |
promoted underlying type, if the two are different.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 894 |
- If class `B` is derived directly or indirectly from class `A`,
|
| 895 |
conversion of `B*` to `A*` is better than conversion of `B*` to
|
| 896 |
`void*`, and conversion of `A*` to `void*` is better than conversion
|
| 897 |
of `B*` to `void*`.
|
| 898 |
- If class `B` is derived directly or indirectly from class `A` and
|
| 899 |
class `C` is derived directly or indirectly from `B`,
|
| 900 |
- conversion of `C*` to `B*` is better than conversion of `C*` to
|
| 901 |
`A*`,
|
| 902 |
-
\[*Example
|
| 903 |
``` cpp
|
| 904 |
struct A {};
|
| 905 |
struct B : public A {};
|
| 906 |
struct C : public B {};
|
| 907 |
C* pc;
|
|
|
|
| 1 |
### Best viable function <a id="over.match.best">[[over.match.best]]</a>
|
| 2 |
|
| 3 |
+
#### General <a id="over.match.best.general">[[over.match.best.general]]</a>
|
| 4 |
|
| 5 |
+
Define ICSⁱ(`F`) as the implicit conversion sequence that converts the
|
| 6 |
+
iᵗʰ argument in the list to the type of the iᵗʰ parameter of viable
|
| 7 |
+
function `F`. [[over.best.ics]] defines the implicit conversion
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
sequences and [[over.ics.rank]] defines what it means for one implicit
|
| 9 |
conversion sequence to be a better conversion sequence or worse
|
| 10 |
conversion sequence than another.
|
| 11 |
|
| 12 |
+
Given these definitions, a viable function `F₁` is defined to be a
|
| 13 |
+
*better* function than another viable function `F₂` if for all arguments
|
| 14 |
+
i, ICSⁱ(`F₁`) is not a worse conversion sequence than ICSⁱ(`F₂`), and
|
| 15 |
+
then
|
| 16 |
|
| 17 |
+
- for some argument j, ICSʲ(`F₁`) is a better conversion sequence than
|
| 18 |
+
ICSʲ(`F₂`), or, if not that,
|
| 19 |
- the context is an initialization by user-defined conversion (see
|
| 20 |
[[dcl.init]], [[over.match.conv]], and [[over.match.ref]]) and the
|
| 21 |
+
standard conversion sequence from the return type of `F₁` to the
|
| 22 |
destination type (i.e., the type of the entity being initialized) is a
|
| 23 |
better conversion sequence than the standard conversion sequence from
|
| 24 |
+
the return type of `F₂` to the destination type
|
| 25 |
\[*Example 1*:
|
| 26 |
``` cpp
|
| 27 |
struct A {
|
| 28 |
A();
|
| 29 |
operator int();
|
|
|
|
| 66 |
parameter-type-lists, and `F1` is more constrained than `F2` according
|
| 67 |
to the partial ordering of constraints described in
|
| 68 |
[[temp.constr.order]], or if not that,
|
| 69 |
- `F1` is a constructor for a class `D`, `F2` is a constructor for a
|
| 70 |
base class `B` of `D`, and for all arguments the corresponding
|
| 71 |
+
parameters of `F1` and `F2` have the same type
|
| 72 |
\[*Example 3*:
|
| 73 |
``` cpp
|
| 74 |
struct A {
|
| 75 |
A(int = 0);
|
| 76 |
};
|
|
|
|
| 110 |
bool b = 1 < S(); // calls #2
|
| 111 |
```
|
| 112 |
|
| 113 |
— *end example*]
|
| 114 |
or, if not that
|
| 115 |
+
- `F1` and `F2` are generated from class template argument deduction
|
| 116 |
+
[[over.match.class.deduct]] for a class `D`, and `F2` is generated
|
| 117 |
+
from inheriting constructors from a base class of `D` while `F1` is
|
| 118 |
+
not, and for each explicit function argument, the corresponding
|
| 119 |
+
parameters of `F1` and `F2` are either both ellipses or have the same
|
| 120 |
+
type, or, if not that,
|
| 121 |
- `F1` is generated from a *deduction-guide* [[over.match.class.deduct]]
|
| 122 |
and `F2` is not, or, if not that,
|
| 123 |
- `F1` is the copy deduction candidate [[over.match.class.deduct]] and
|
| 124 |
`F2` is not, or, if not that,
|
| 125 |
- `F1` is generated from a non-template constructor and `F2` is
|
|
|
|
| 152 |
|
| 153 |
— *end example*]
|
| 154 |
|
| 155 |
If there is exactly one viable function that is a better function than
|
| 156 |
all other viable functions, then it is the one selected by overload
|
| 157 |
+
resolution; otherwise the call is ill-formed.[^7]
|
| 158 |
|
| 159 |
[*Example 7*:
|
| 160 |
|
| 161 |
``` cpp
|
| 162 |
void Fcn(const int*, short);
|
|
|
|
| 171 |
|
| 172 |
Fcn(&i, 1L); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
| 173 |
// and 1L → short and 1L → int are indistinguishable
|
| 174 |
|
| 175 |
Fcn(&i, 'c'); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
| 176 |
+
// and 'c' → int is better than 'c' → short
|
| 177 |
}
|
| 178 |
```
|
| 179 |
|
| 180 |
— *end example*]
|
| 181 |
|
| 182 |
If the best viable function resolves to a function for which multiple
|
| 183 |
+
declarations were found, and if any two of these declarations inhabit
|
| 184 |
+
different scopes and specify a default argument that made the function
|
| 185 |
+
viable, the program is ill-formed.
|
|
|
|
| 186 |
|
| 187 |
[*Example 8*:
|
| 188 |
|
| 189 |
``` cpp
|
| 190 |
namespace A {
|
|
|
|
| 205 |
|
| 206 |
— *end example*]
|
| 207 |
|
| 208 |
#### Implicit conversion sequences <a id="over.best.ics">[[over.best.ics]]</a>
|
| 209 |
|
| 210 |
+
##### General <a id="over.best.ics.general">[[over.best.ics.general]]</a>
|
| 211 |
+
|
| 212 |
An *implicit conversion sequence* is a sequence of conversions used to
|
| 213 |
convert an argument in a function call to the type of the corresponding
|
| 214 |
parameter of the function being called. The sequence of conversions is
|
| 215 |
an implicit conversion as defined in [[conv]], which means it is
|
| 216 |
governed by the rules for initialization of an object or reference by a
|
| 217 |
+
single expression [[dcl.init]], [[dcl.init.ref]].
|
| 218 |
|
| 219 |
Implicit conversion sequences are concerned only with the type,
|
| 220 |
cv-qualification, and value category of the argument and how these are
|
| 221 |
converted to match the corresponding properties of the parameter.
|
| 222 |
|
|
|
|
| 236 |
- an ellipsis conversion sequence [[over.ics.ellipsis]].
|
| 237 |
|
| 238 |
However, if the target is
|
| 239 |
|
| 240 |
- the first parameter of a constructor or
|
| 241 |
+
- the object parameter of a user-defined conversion function
|
| 242 |
|
| 243 |
and the constructor or user-defined conversion function is a candidate
|
| 244 |
by
|
| 245 |
|
| 246 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
|
|
|
| 297 |
|
| 298 |
When the parameter has a class type and the argument expression has the
|
| 299 |
same type, the implicit conversion sequence is an identity conversion.
|
| 300 |
When the parameter has a class type and the argument expression has a
|
| 301 |
derived class type, the implicit conversion sequence is a
|
| 302 |
+
derived-to-base conversion from the derived class to the base class. A
|
| 303 |
+
derived-to-base conversion has Conversion rank [[over.ics.scs]].
|
| 304 |
|
| 305 |
[*Note 4*: There is no such standard conversion; this derived-to-base
|
| 306 |
conversion exists only in the description of implicit conversion
|
| 307 |
sequences. — *end note*]
|
| 308 |
|
| 309 |
+
When the parameter is the implicit object parameter of a static member
|
| 310 |
+
function, the implicit conversion sequence is a standard conversion
|
| 311 |
+
sequence that is neither better nor worse than any other standard
|
| 312 |
+
conversion sequence.
|
| 313 |
|
| 314 |
In all contexts, when converting to the implicit object parameter or
|
| 315 |
when converting to the left operand of an assignment operation only
|
| 316 |
standard conversion sequences are allowed.
|
| 317 |
|
| 318 |
+
[*Note 5*: When converting to the explicit object parameter, if any,
|
| 319 |
+
user-defined conversion sequences are allowed. — *end note*]
|
| 320 |
+
|
| 321 |
If no conversions are required to match an argument to a parameter type,
|
| 322 |
the implicit conversion sequence is the standard conversion sequence
|
| 323 |
consisting of the identity conversion [[over.ics.scs]].
|
| 324 |
|
| 325 |
If no sequence of conversions can be found to convert an argument to a
|
|
|
|
| 332 |
the purpose of ranking implicit conversion sequences as described in
|
| 333 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 334 |
user-defined conversion sequence that is indistinguishable from any
|
| 335 |
other user-defined conversion sequence.
|
| 336 |
|
| 337 |
+
[*Note 6*:
|
| 338 |
|
| 339 |
This rule prevents a function from becoming non-viable because of an
|
| 340 |
ambiguous conversion sequence for one of its parameters.
|
| 341 |
|
| 342 |
[*Example 3*:
|
|
|
|
| 370 |
|
| 371 |
summarizes the conversions defined in [[conv]] and partitions them into
|
| 372 |
four disjoint categories: Lvalue Transformation, Qualification
|
| 373 |
Adjustment, Promotion, and Conversion.
|
| 374 |
|
| 375 |
+
[*Note 7*: These categories are orthogonal with respect to value
|
| 376 |
category, cv-qualification, and data representation: the Lvalue
|
| 377 |
Transformations do not change the cv-qualification or data
|
| 378 |
representation of the type; the Qualification Adjustments do not change
|
| 379 |
the value category or data representation of the type; and the
|
| 380 |
Promotions and Conversions do not change the value category or
|
| 381 |
cv-qualification of the type. — *end note*]
|
| 382 |
|
| 383 |
+
[*Note 8*: As described in [[conv]], a standard conversion sequence
|
| 384 |
either is the Identity conversion by itself (that is, no conversion) or
|
| 385 |
consists of one to three conversions from the other four categories. If
|
| 386 |
there are two or more conversions in the sequence, the conversions are
|
| 387 |
applied in the canonical order: **Lvalue Transformation**, **Promotion**
|
| 388 |
or **Conversion**, **Qualification Adjustment**. — *end note*]
|
|
|
|
| 410 |
A *user-defined conversion sequence* consists of an initial standard
|
| 411 |
conversion sequence followed by a user-defined conversion [[class.conv]]
|
| 412 |
followed by a second standard conversion sequence. If the user-defined
|
| 413 |
conversion is specified by a constructor [[class.conv.ctor]], the
|
| 414 |
initial standard conversion sequence converts the source type to the
|
| 415 |
+
type of the first parameter of that constructor. If the user-defined
|
| 416 |
conversion is specified by a conversion function [[class.conv.fct]], the
|
| 417 |
initial standard conversion sequence converts the source type to the
|
| 418 |
+
type of the object parameter of that conversion function.
|
| 419 |
|
| 420 |
The second standard conversion sequence converts the result of the
|
| 421 |
user-defined conversion to the target type for the sequence; any
|
| 422 |
reference binding is included in the second standard conversion
|
| 423 |
sequence. Since an implicit conversion sequence is an initialization,
|
|
|
|
| 445 |
|
| 446 |
When a parameter of reference type binds directly [[dcl.init.ref]] to an
|
| 447 |
argument expression, the implicit conversion sequence is the identity
|
| 448 |
conversion, unless the argument expression has a type that is a derived
|
| 449 |
class of the parameter type, in which case the implicit conversion
|
| 450 |
+
sequence is a derived-to-base conversion [[over.best.ics]].
|
| 451 |
|
| 452 |
[*Example 4*:
|
| 453 |
|
| 454 |
``` cpp
|
| 455 |
struct A {};
|
|
|
|
| 461 |
|
| 462 |
— *end example*]
|
| 463 |
|
| 464 |
If the parameter binds directly to the result of applying a conversion
|
| 465 |
function to the argument expression, the implicit conversion sequence is
|
| 466 |
+
a user-defined conversion sequence [[over.ics.user]] whose second
|
| 467 |
+
standard conversion sequence is either an identity conversion or, if the
|
| 468 |
conversion function returns an entity of a type that is a derived class
|
| 469 |
of the parameter type, a derived-to-base conversion.
|
| 470 |
|
| 471 |
When a parameter of reference type is not bound directly to an argument
|
| 472 |
expression, the conversion sequence is the one required to convert the
|
|
|
|
| 480 |
[[over.match.funcs]], an implicit conversion sequence cannot be formed
|
| 481 |
if it requires binding an lvalue reference other than a reference to a
|
| 482 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 483 |
an lvalue other than a function lvalue.
|
| 484 |
|
| 485 |
+
[*Note 9*: This means, for example, that a candidate function cannot be
|
| 486 |
a viable function if it has a non-`const` lvalue reference parameter
|
| 487 |
(other than the implicit object parameter) and the corresponding
|
| 488 |
argument would require a temporary to be created to initialize the
|
| 489 |
lvalue reference (see [[dcl.init.ref]]). — *end note*]
|
| 490 |
|
|
|
|
| 512 |
initialized from the initializer list according to the rules for
|
| 513 |
aggregate initialization [[dcl.init.aggr]], in which case the implicit
|
| 514 |
conversion sequence is a user-defined conversion sequence whose second
|
| 515 |
standard conversion sequence is an identity conversion.
|
| 516 |
|
| 517 |
+
[*Note 10*:
|
| 518 |
|
| 519 |
Aggregate initialization does not require that the members are declared
|
| 520 |
in designation order. If, after overload resolution, the order does not
|
| 521 |
match for the selected overload, the initialization of the parameter
|
| 522 |
will be ill-formed [[dcl.init.list]].
|
|
|
|
| 545 |
Otherwise, if the parameter type is an aggregate class `X` and the
|
| 546 |
initializer list has a single element of type cv `U`, where `U` is `X`
|
| 547 |
or a class derived from `X`, the implicit conversion sequence is the one
|
| 548 |
required to convert the element to the parameter type.
|
| 549 |
|
| 550 |
+
Otherwise, if the parameter type is a character array[^8]
|
| 551 |
+
|
| 552 |
+
and the initializer list has a single element that is an
|
| 553 |
+
appropriately-typed *string-literal* [[dcl.init.string]], the implicit
|
| 554 |
+
conversion sequence is the identity conversion.
|
| 555 |
|
| 556 |
Otherwise, if the parameter type is `std::initializer_list<X>` and all
|
| 557 |
the elements of the initializer list can be implicitly converted to `X`,
|
| 558 |
the implicit conversion sequence is the worst conversion necessary to
|
| 559 |
convert an element of the list to `X`, or if the initializer list has no
|
|
|
|
| 563 |
|
| 564 |
[*Example 7*:
|
| 565 |
|
| 566 |
``` cpp
|
| 567 |
void f(std::initializer_list<int>);
|
| 568 |
+
f( {} ); // OK, f(initializer_list<int>) identity conversion
|
| 569 |
+
f( {1,2,3} ); // OK, f(initializer_list<int>) identity conversion
|
| 570 |
+
f( {'a','b'} ); // OK, f(initializer_list<int>) integral promotion
|
| 571 |
f( {1.0} ); // error: narrowing
|
| 572 |
|
| 573 |
struct A {
|
| 574 |
A(std::initializer_list<double>); // #1
|
| 575 |
A(std::initializer_list<complex<double>>); // #2
|
|
|
|
| 580 |
void g(A);
|
| 581 |
g({ "foo", "bar" }); // OK, uses #3
|
| 582 |
|
| 583 |
typedef int IA[3];
|
| 584 |
void h(const IA&);
|
| 585 |
+
h({ 1, 2, 3 }); // OK, identity conversion
|
| 586 |
```
|
| 587 |
|
| 588 |
— *end example*]
|
| 589 |
|
| 590 |
Otherwise, if the parameter type is “array of `N` `X`” or “array of
|
|
|
|
| 602 |
- If `C` is not an initializer-list constructor and the initializer list
|
| 603 |
has a single element of type cv `U`, where `U` is `X` or a class
|
| 604 |
derived from `X`, the implicit conversion sequence has Exact Match
|
| 605 |
rank if `U` is `X`, or Conversion rank if `U` is derived from `X`.
|
| 606 |
- Otherwise, the implicit conversion sequence is a user-defined
|
| 607 |
+
conversion sequence whose second standard conversion sequence is an
|
| 608 |
identity conversion.
|
| 609 |
|
| 610 |
If multiple constructors are viable but none is better than the others,
|
| 611 |
the implicit conversion sequence is the ambiguous conversion sequence.
|
| 612 |
User-defined conversions are allowed for conversion of the initializer
|
|
|
|
| 618 |
``` cpp
|
| 619 |
struct A {
|
| 620 |
A(std::initializer_list<int>);
|
| 621 |
};
|
| 622 |
void f(A);
|
| 623 |
+
f( {'a', 'b'} ); // OK, f(A(std::initializer_list<int>)) user-defined conversion
|
| 624 |
|
| 625 |
struct B {
|
| 626 |
B(int, double);
|
| 627 |
};
|
| 628 |
void g(B);
|
| 629 |
+
g( {'a', 'b'} ); // OK, g(B(int, double)) user-defined conversion
|
| 630 |
g( {1.0, 1.0} ); // error: narrowing
|
| 631 |
|
| 632 |
void f(B);
|
| 633 |
f( {'a', 'b'} ); // error: ambiguous f(A) or f(B)
|
| 634 |
|
| 635 |
struct C {
|
| 636 |
C(std::string);
|
| 637 |
};
|
| 638 |
void h(C);
|
| 639 |
+
h({"foo"}); // OK, h(C(std::string("foo")))
|
| 640 |
|
| 641 |
struct D {
|
| 642 |
D(A, C);
|
| 643 |
};
|
| 644 |
void i(D);
|
| 645 |
+
i({ {1,2}, {"bar"} }); // OK, i(D(A(std::initializer_list<int>{1,2\), C(std::string("bar"))))}
|
| 646 |
```
|
| 647 |
|
| 648 |
— *end example*]
|
| 649 |
|
| 650 |
Otherwise, if the parameter has an aggregate type which can be
|
| 651 |
initialized from the initializer list according to the rules for
|
| 652 |
aggregate initialization [[dcl.init.aggr]], the implicit conversion
|
| 653 |
+
sequence is a user-defined conversion sequence whose second standard
|
| 654 |
+
conversion sequence is an identity conversion.
|
| 655 |
|
| 656 |
[*Example 9*:
|
| 657 |
|
| 658 |
``` cpp
|
| 659 |
struct A {
|
| 660 |
int m1;
|
| 661 |
double m2;
|
| 662 |
};
|
| 663 |
|
| 664 |
void f(A);
|
| 665 |
+
f( {'a', 'b'} ); // OK, f(A(int,double)) user-defined conversion
|
| 666 |
f( {1.0} ); // error: narrowing
|
| 667 |
```
|
| 668 |
|
| 669 |
— *end example*]
|
| 670 |
|
| 671 |
Otherwise, if the parameter is a reference, see [[over.ics.ref]].
|
| 672 |
|
| 673 |
+
[*Note 11*: The rules in this subclause will apply for initializing the
|
| 674 |
underlying temporary for the reference. — *end note*]
|
| 675 |
|
| 676 |
[*Example 10*:
|
| 677 |
|
| 678 |
``` cpp
|
|
|
|
| 680 |
int m1;
|
| 681 |
double m2;
|
| 682 |
};
|
| 683 |
|
| 684 |
void f(const A&);
|
| 685 |
+
f( {'a', 'b'} ); // OK, f(A(int,double)) user-defined conversion
|
| 686 |
f( {1.0} ); // error: narrowing
|
| 687 |
|
| 688 |
void g(const double &);
|
| 689 |
g({1}); // same conversion as int to double
|
| 690 |
```
|
|
|
|
| 697 |
initializer list, the implicit conversion sequence is the one required
|
| 698 |
to convert the element to the parameter type;
|
| 699 |
\[*Example 11*:
|
| 700 |
``` cpp
|
| 701 |
void f(int);
|
| 702 |
+
f( {'a'} ); // OK, same conversion as char to int
|
| 703 |
f( {1.0} ); // error: narrowing
|
| 704 |
```
|
| 705 |
|
| 706 |
— *end example*]
|
| 707 |
- if the initializer list has no elements, the implicit conversion
|
| 708 |
sequence is the identity conversion.
|
| 709 |
\[*Example 12*:
|
| 710 |
``` cpp
|
| 711 |
void f(int);
|
| 712 |
+
f( { } ); // OK, identity conversion
|
| 713 |
```
|
| 714 |
|
| 715 |
— *end example*]
|
| 716 |
|
| 717 |
In all cases other than those enumerated above, no conversion is
|
|
|
|
| 900 |
- A conversion that does not convert a pointer or a pointer to member to
|
| 901 |
`bool` is better than one that does.
|
| 902 |
- A conversion that promotes an enumeration whose underlying type is
|
| 903 |
fixed to its underlying type is better than one that promotes to the
|
| 904 |
promoted underlying type, if the two are different.
|
| 905 |
+
- A conversion in either direction between floating-point type `FP1` and
|
| 906 |
+
floating-point type `FP2` is better than a conversion in the same
|
| 907 |
+
direction between `FP1` and arithmetic type `T3` if
|
| 908 |
+
- the floating-point conversion rank [[conv.rank]] of `FP1` is equal
|
| 909 |
+
to the rank of `FP2`, and
|
| 910 |
+
- `T3` is not a floating-point type, or `T3` is a floating-point type
|
| 911 |
+
whose rank is not equal to the rank of `FP1`, or the floating-point
|
| 912 |
+
conversion subrank [[conv.rank]] of `FP2` is greater than the
|
| 913 |
+
subrank of `T3`.
|
| 914 |
+
\[*Example 8*:
|
| 915 |
+
``` cpp
|
| 916 |
+
int f(std::float32_t);
|
| 917 |
+
int f(std::float64_t);
|
| 918 |
+
int f(long long);
|
| 919 |
+
float x;
|
| 920 |
+
std::float16_t y;
|
| 921 |
+
int i = f(x); // calls f(std::float32_t) on implementations where
|
| 922 |
+
// float and std::float32_t have equal conversion ranks
|
| 923 |
+
int j = f(y); // error: ambiguous, no equal conversion rank
|
| 924 |
+
```
|
| 925 |
+
|
| 926 |
+
— *end example*]
|
| 927 |
- If class `B` is derived directly or indirectly from class `A`,
|
| 928 |
conversion of `B*` to `A*` is better than conversion of `B*` to
|
| 929 |
`void*`, and conversion of `A*` to `void*` is better than conversion
|
| 930 |
of `B*` to `void*`.
|
| 931 |
- If class `B` is derived directly or indirectly from class `A` and
|
| 932 |
class `C` is derived directly or indirectly from `B`,
|
| 933 |
- conversion of `C*` to `B*` is better than conversion of `C*` to
|
| 934 |
`A*`,
|
| 935 |
+
\[*Example 9*:
|
| 936 |
``` cpp
|
| 937 |
struct A {};
|
| 938 |
struct B : public A {};
|
| 939 |
struct C : public B {};
|
| 940 |
C* pc;
|