- tmp/tmpl_o8d4pm/{from.md → to.md} +273 -115
tmp/tmpl_o8d4pm/{from.md → to.md}
RENAMED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
### Best viable function <a id="over.match.best">[[over.match.best]]</a>
|
| 2 |
|
| 3 |
Define ICS*i*(`F`) as follows:
|
| 4 |
|
| 5 |
-
-
|
| 6 |
ICS*1*(`F`) is neither better nor worse than ICS*1*(`G`) for any
|
| 7 |
function `G`, and, symmetrically, ICS*1*(`G`) is neither better nor
|
| 8 |
-
worse than ICS*1*(`F`)[^9]
|
| 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
|
|
@@ -23,30 +23,32 @@ and then
|
|
| 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 `F1` to the
|
| 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 `F2` to the destination type
|
|
|
|
| 29 |
``` cpp
|
| 30 |
struct A {
|
| 31 |
A();
|
| 32 |
operator int();
|
| 33 |
operator double();
|
| 34 |
} a;
|
| 35 |
-
int i = a;
|
| 36 |
-
|
| 37 |
-
// a conversion to int
|
| 38 |
float x = a; // ambiguous: both possibilities require conversions,
|
| 39 |
// and neither is better than the other
|
| 40 |
```
|
| 41 |
|
|
|
|
| 42 |
or, if not that,
|
| 43 |
- the context is an initialization by conversion function for direct
|
| 44 |
reference binding ([[over.match.ref]]) of a reference to function
|
| 45 |
type, the return type of `F1` is the same kind of reference (i.e.
|
| 46 |
lvalue or rvalue) as the reference being initialized, and the return
|
| 47 |
type of `F2` is not
|
|
|
|
| 48 |
``` cpp
|
| 49 |
template <class T> struct A {
|
| 50 |
operator T&(); // #1
|
| 51 |
operator T&&(); // #2
|
| 52 |
};
|
|
@@ -54,50 +56,87 @@ and then
|
|
| 54 |
A<Fn> a;
|
| 55 |
Fn& lf = a; // calls #1
|
| 56 |
Fn&& rf = a; // calls #2
|
| 57 |
```
|
| 58 |
|
|
|
|
| 59 |
or, if not that,
|
| 60 |
- `F1` is not a function template specialization and `F2` is a function
|
| 61 |
template specialization, or, if not that,
|
| 62 |
- `F1` and `F2` are function template specializations, and the function
|
| 63 |
template for `F1` is more specialized than the template for `F2`
|
| 64 |
according to the partial ordering rules described in
|
| 65 |
-
[[temp.func.order]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
If there is exactly one viable function that is a better function than
|
| 68 |
all other viable functions, then it is the one selected by overload
|
| 69 |
-
resolution; otherwise the call is ill-formed[^10]
|
|
|
|
|
|
|
| 70 |
|
| 71 |
``` cpp
|
| 72 |
void Fcn(const int*, short);
|
| 73 |
void Fcn(int*, int);
|
| 74 |
|
| 75 |
int i;
|
| 76 |
short s = 0;
|
| 77 |
|
| 78 |
void f() {
|
| 79 |
-
Fcn(&i, s);
|
| 80 |
-
// &i → int* is better than &i → const int*
|
| 81 |
// but s → short is also better than s → int
|
| 82 |
|
| 83 |
-
Fcn(&i, 1L);
|
| 84 |
-
// &i → int* is better than &i → const int*
|
| 85 |
// and 1L → short and 1L → int are indistinguishable
|
| 86 |
|
| 87 |
-
Fcn(&i,'c');
|
| 88 |
-
// &i → int* is better than &i → const int*
|
| 89 |
// and c → int is better than c → short
|
| 90 |
}
|
| 91 |
```
|
| 92 |
|
|
|
|
|
|
|
| 93 |
If the best viable function resolves to a function for which multiple
|
| 94 |
declarations were found, and if at least two of these declarations — or
|
| 95 |
the declarations they refer to in the case of *using-declaration*s —
|
| 96 |
specify a default argument that made the function viable, the program is
|
| 97 |
ill-formed.
|
| 98 |
|
|
|
|
|
|
|
| 99 |
``` cpp
|
| 100 |
namespace A {
|
| 101 |
extern "C" void f(int = 5);
|
| 102 |
}
|
| 103 |
namespace B {
|
|
@@ -111,10 +150,12 @@ void use() {
|
|
| 111 |
f(3); // OK, default argument was not used for viability
|
| 112 |
f(); // Error: found default argument twice
|
| 113 |
}
|
| 114 |
```
|
| 115 |
|
|
|
|
|
|
|
| 116 |
#### Implicit conversion sequences <a id="over.best.ics">[[over.best.ics]]</a>
|
| 117 |
|
| 118 |
An *implicit conversion sequence* is a sequence of conversions used to
|
| 119 |
convert an argument in a function call to the type of the corresponding
|
| 120 |
parameter of the function being called. The sequence of conversions is
|
|
@@ -123,16 +164,16 @@ governed by the rules for initialization of an object or reference by a
|
|
| 123 |
single expression ([[dcl.init]], [[dcl.init.ref]]).
|
| 124 |
|
| 125 |
Implicit conversion sequences are concerned only with the type,
|
| 126 |
cv-qualification, and value category of the argument and how these are
|
| 127 |
converted to match the corresponding properties of the parameter. Other
|
| 128 |
-
properties, such as the lifetime, storage class, alignment,
|
| 129 |
-
accessibility of the argument
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
argument
|
| 133 |
-
analysis.
|
| 134 |
|
| 135 |
A well-formed implicit conversion sequence is one of the following
|
| 136 |
forms:
|
| 137 |
|
| 138 |
- a *standard conversion sequence* ([[over.ics.scs]]),
|
|
@@ -150,17 +191,21 @@ by
|
|
| 150 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
| 151 |
step of a class copy-initialization,
|
| 152 |
- [[over.match.copy]], [[over.match.conv]], or [[over.match.ref]] (in
|
| 153 |
all cases), or
|
| 154 |
- the second phase of [[over.match.list]] when the initializer list has
|
| 155 |
-
exactly one element
|
| 156 |
-
|
| 157 |
-
|
| 158 |
|
| 159 |
-
user-defined conversion sequences are not considered.
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
``` cpp
|
| 164 |
struct Y { Y(int); };
|
| 165 |
struct A { operator int(); };
|
| 166 |
Y y1 = A(); // error: A::operator int() is not a candidate
|
|
@@ -169,83 +214,120 @@ struct Y { Y(int); };
|
|
| 169 |
struct B { operator X(); };
|
| 170 |
B b;
|
| 171 |
X x({b}); // error: B::operator X() is not a candidate
|
| 172 |
```
|
| 173 |
|
|
|
|
|
|
|
| 174 |
For the case where the parameter type is a reference, see
|
| 175 |
[[over.ics.ref]].
|
| 176 |
|
| 177 |
When the parameter type is not a reference, the implicit conversion
|
| 178 |
sequence models a copy-initialization of the parameter from the argument
|
| 179 |
expression. The implicit conversion sequence is the one required to
|
| 180 |
convert the argument expression to a prvalue of the type of the
|
| 181 |
-
parameter.
|
|
|
|
|
|
|
| 182 |
conversion defined for the purposes of Clause [[over]]; the actual
|
| 183 |
initialization is defined in terms of constructors and is not a
|
| 184 |
-
conversion.
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
When the parameter has a class type and the argument expression has the
|
| 190 |
same type, the implicit conversion sequence is an identity conversion.
|
| 191 |
When the parameter has a class type and the argument expression has a
|
| 192 |
derived class type, the implicit conversion sequence is a
|
| 193 |
derived-to-base Conversion from the derived class to the base class.
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
In all contexts, when converting to the implicit object parameter or
|
| 199 |
when converting to the left operand of an assignment operation only
|
| 200 |
-
standard conversion sequences
|
| 201 |
-
result are allowed.
|
| 202 |
|
| 203 |
If no conversions are required to match an argument to a parameter type,
|
| 204 |
the implicit conversion sequence is the standard conversion sequence
|
| 205 |
consisting of the identity conversion ([[over.ics.scs]]).
|
| 206 |
|
| 207 |
If no sequence of conversions can be found to convert an argument to a
|
| 208 |
-
parameter type
|
| 209 |
-
conversion sequence cannot be formed.
|
| 210 |
|
| 211 |
If several different sequences of conversions exist that each convert
|
| 212 |
the argument to the parameter type, the implicit conversion sequence
|
| 213 |
associated with the parameter is defined to be the unique conversion
|
| 214 |
sequence designated the *ambiguous conversion sequence*. For the purpose
|
| 215 |
of ranking implicit conversion sequences as described in
|
| 216 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 217 |
-
user-defined sequence that is indistinguishable from any
|
| 218 |
-
user-defined conversion sequence
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
The three forms of implicit conversion sequences mentioned above are
|
| 224 |
defined in the following subclauses.
|
| 225 |
|
| 226 |
##### Standard conversion sequences <a id="over.ics.scs">[[over.ics.scs]]</a>
|
| 227 |
|
| 228 |
Table [[tab:over.conversions]] summarizes the conversions defined in
|
| 229 |
Clause [[conv]] and partitions them into four disjoint categories:
|
| 230 |
Lvalue Transformation, Qualification Adjustment, Promotion, and
|
| 231 |
-
Conversion.
|
|
|
|
|
|
|
| 232 |
category, cv-qualification, and data representation: the Lvalue
|
| 233 |
Transformations do not change the cv-qualification or data
|
| 234 |
representation of the type; the Qualification Adjustments do not change
|
| 235 |
the value category or data representation of the type; and the
|
| 236 |
Promotions and Conversions do not change the value category or
|
| 237 |
-
cv-qualification of the type.
|
| 238 |
|
| 239 |
-
As described in Clause [[conv]], a standard conversion
|
| 240 |
-
either the Identity conversion by itself (that is, no
|
| 241 |
-
consists of one to three conversions from the other four
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
sequence, the conversions are applied in the canonical order: **Lvalue
|
| 245 |
Transformation**, **Promotion** or **Conversion**, **Qualification
|
| 246 |
-
Adjustment**.
|
| 247 |
|
| 248 |
Each conversion in Table [[tab:over.conversions]] also has an
|
| 249 |
associated rank (Exact Match, Promotion, or Conversion). These are used
|
| 250 |
to rank standard conversion sequences ([[over.ics.rank]]). The rank of
|
| 251 |
a conversion sequence is determined by considering the rank of each
|
|
@@ -306,70 +388,88 @@ When a parameter of reference type binds directly ([[dcl.init.ref]]) to
|
|
| 306 |
an argument expression, the implicit conversion sequence is the identity
|
| 307 |
conversion, unless the argument expression has a type that is a derived
|
| 308 |
class of the parameter type, in which case the implicit conversion
|
| 309 |
sequence is a derived-to-base Conversion ([[over.best.ics]]).
|
| 310 |
|
|
|
|
|
|
|
| 311 |
``` cpp
|
| 312 |
struct A {};
|
| 313 |
struct B : public A {} b;
|
| 314 |
int f(A&);
|
| 315 |
int f(B&);
|
| 316 |
-
int i = f(b);
|
| 317 |
-
// f(A&), a conversion
|
| 318 |
```
|
| 319 |
|
|
|
|
|
|
|
| 320 |
If the parameter binds directly to the result of applying a conversion
|
| 321 |
function to the argument expression, the implicit conversion sequence is
|
| 322 |
a user-defined conversion sequence ([[over.ics.user]]), with the second
|
| 323 |
standard conversion sequence either an identity conversion or, if the
|
| 324 |
conversion function returns an entity of a type that is a derived class
|
| 325 |
of the parameter type, a derived-to-base Conversion.
|
| 326 |
|
| 327 |
When a parameter of reference type is not bound directly to an argument
|
| 328 |
expression, the conversion sequence is the one required to convert the
|
| 329 |
-
argument expression to the
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
conversion.
|
| 335 |
|
| 336 |
Except for an implicit object parameter, for which see
|
| 337 |
[[over.match.funcs]], a standard conversion sequence cannot be formed if
|
| 338 |
it requires binding an lvalue reference other than a reference to a
|
| 339 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 340 |
-
an lvalue other than a function lvalue.
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
|
|
|
|
|
|
| 345 |
|
| 346 |
Other restrictions on binding a reference to a particular argument that
|
| 347 |
are not based on the types of the reference and the argument do not
|
| 348 |
-
affect the formation of a standard conversion sequence, however.
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
|
|
|
|
|
|
| 356 |
|
| 357 |
##### List-initialization sequence <a id="over.ics.list">[[over.ics.list]]</a>
|
| 358 |
|
| 359 |
When an argument is an initializer list ([[dcl.init.list]]), it is not
|
| 360 |
an expression and special rules apply for converting it to a parameter
|
| 361 |
type.
|
| 362 |
|
| 363 |
-
If the parameter type is `
|
| 364 |
-
|
| 365 |
-
conversion sequence is the
|
| 366 |
-
|
| 367 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
conversion even in the context of a call to an initializer-list
|
| 369 |
constructor.
|
| 370 |
|
|
|
|
|
|
|
| 371 |
``` cpp
|
| 372 |
void f(std::initializer_list<int>);
|
| 373 |
f( {} ); // OK: f(initializer_list<int>) identity conversion
|
| 374 |
f( {1,2,3} ); // OK: f(initializer_list<int>) identity conversion
|
| 375 |
f( {'a','b'} ); // OK: f(initializer_list<int>) integral promotion
|
|
@@ -388,27 +488,38 @@ g({ "foo", "bar" }); // OK, uses #3
|
|
| 388 |
typedef int IA[3];
|
| 389 |
void h(const IA&);
|
| 390 |
h({ 1, 2, 3 }); // OK: identity conversion
|
| 391 |
```
|
| 392 |
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
|
|
|
| 399 |
|
| 400 |
Otherwise, if the parameter is a non-aggregate class `X` and overload
|
| 401 |
-
resolution per [[over.match.list]] chooses a single best constructor
|
| 402 |
-
`X` to perform the initialization of an object of type `X` from
|
| 403 |
-
argument initializer list
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
|
| 411 |
``` cpp
|
| 412 |
struct A {
|
| 413 |
A(std::initializer_list<int>);
|
| 414 |
};
|
|
@@ -436,16 +547,20 @@ struct D {
|
|
| 436 |
};
|
| 437 |
void i(D);
|
| 438 |
i({ {1,2}, {"bar"} }); // OK: i(D(A(std::initializer_list<int>{1,2\), C(std::string("bar"))))}
|
| 439 |
```
|
| 440 |
|
|
|
|
|
|
|
| 441 |
Otherwise, if the parameter has an aggregate type which can be
|
| 442 |
initialized from the initializer list according to the rules for
|
| 443 |
aggregate initialization ([[dcl.init.aggr]]), the implicit conversion
|
| 444 |
sequence is a user-defined conversion sequence with the second standard
|
| 445 |
conversion sequence an identity conversion.
|
| 446 |
|
|
|
|
|
|
|
| 447 |
``` cpp
|
| 448 |
struct A {
|
| 449 |
int m1;
|
| 450 |
double m2;
|
| 451 |
};
|
|
@@ -453,13 +568,18 @@ struct A {
|
|
| 453 |
void f(A);
|
| 454 |
f( {'a', 'b'} ); // OK: f(A(int,double)) user-defined conversion
|
| 455 |
f( {1.0} ); // error: narrowing
|
| 456 |
```
|
| 457 |
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
|
| 462 |
``` cpp
|
| 463 |
struct A {
|
| 464 |
int m1;
|
| 465 |
double m2;
|
|
@@ -471,33 +591,41 @@ f( {1.0} ); // error: narrowing
|
|
| 471 |
|
| 472 |
void g(const double &);
|
| 473 |
g({1}); // same conversion as int to double
|
| 474 |
```
|
| 475 |
|
|
|
|
|
|
|
| 476 |
Otherwise, if the parameter type is not a class:
|
| 477 |
|
| 478 |
-
- if the initializer list has one element
|
| 479 |
-
|
| 480 |
-
type;
|
|
|
|
| 481 |
``` cpp
|
| 482 |
void f(int);
|
| 483 |
f( {'a'} ); // OK: same conversion as char to int
|
| 484 |
f( {1.0} ); // error: narrowing
|
| 485 |
```
|
|
|
|
|
|
|
| 486 |
- if the initializer list has no elements, the implicit conversion
|
| 487 |
sequence is the identity conversion.
|
|
|
|
| 488 |
``` cpp
|
| 489 |
void f(int);
|
| 490 |
f( { } ); // OK: identity conversion
|
| 491 |
```
|
| 492 |
|
|
|
|
|
|
|
| 493 |
In all cases other than those enumerated above, no conversion is
|
| 494 |
possible.
|
| 495 |
|
| 496 |
#### Ranking implicit conversion sequences <a id="over.ics.rank">[[over.ics.rank]]</a>
|
| 497 |
|
| 498 |
-
|
| 499 |
sequences based on the relationships *better conversion sequence* and
|
| 500 |
*better conversion*. If an implicit conversion sequence S1 is defined by
|
| 501 |
these rules to be a better conversion sequence than S2, then it is also
|
| 502 |
the case that S2 is a *worse conversion sequence* than S1. If conversion
|
| 503 |
sequence S1 is neither better than nor worse than conversion sequence
|
|
@@ -514,10 +642,31 @@ defined in [[over.best.ics]])
|
|
| 514 |
[[over.ics.ellipsis]]).
|
| 515 |
|
| 516 |
Two implicit conversion sequences of the same form are indistinguishable
|
| 517 |
conversion sequences unless one of the following rules applies:
|
| 518 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 519 |
- Standard conversion sequence `S1` is a better conversion sequence than
|
| 520 |
standard conversion sequence `S2` if
|
| 521 |
- `S1` is a proper subsequence of `S2` (comparing the conversion
|
| 522 |
sequences in the canonical form defined by [[over.ics.scs]],
|
| 523 |
excluding any Lvalue Transformation; the identity conversion
|
|
@@ -527,11 +676,12 @@ conversion sequences unless one of the following rules applies:
|
|
| 527 |
have the same rank and are distinguishable by the rules in the
|
| 528 |
paragraph below, or, if not that,
|
| 529 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and neither
|
| 530 |
refers to an implicit object parameter of a non-static member
|
| 531 |
function declared without a *ref-qualifier*, and `S1` binds an
|
| 532 |
-
rvalue reference to an rvalue and `S2` binds an lvalue reference
|
|
|
|
| 533 |
``` cpp
|
| 534 |
int i;
|
| 535 |
int f1();
|
| 536 |
int&& f2();
|
| 537 |
int g(const int&);
|
|
@@ -553,41 +703,47 @@ conversion sequences unless one of the following rules applies:
|
|
| 553 |
a << 'c'; // calls A::operator<<(int)
|
| 554 |
A().p(); // calls A::p()&&
|
| 555 |
a.p(); // calls A::p()&
|
| 556 |
```
|
| 557 |
|
|
|
|
| 558 |
or, if not that,
|
| 559 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and `S1`
|
| 560 |
binds an lvalue reference to a function lvalue and `S2` binds an
|
| 561 |
-
rvalue reference to a function lvalue
|
|
|
|
| 562 |
``` cpp
|
| 563 |
int f(void(&)()); // #1
|
| 564 |
int f(void(&&)()); // #2
|
| 565 |
void g();
|
| 566 |
int i1 = f(g); // calls #1
|
| 567 |
```
|
| 568 |
|
|
|
|
| 569 |
or, if not that,
|
| 570 |
- `S1`
|
| 571 |
and `S2` differ only in their qualification conversion and yield
|
| 572 |
similar types `T1` and `T2` ([[conv.qual]]), respectively, and the
|
| 573 |
cv-qualification signature of type `T1` is a proper subset of the
|
| 574 |
-
cv-qualification signature of type `T2`
|
|
|
|
| 575 |
``` cpp
|
| 576 |
int f(const volatile int *);
|
| 577 |
int f(const int *);
|
| 578 |
int i;
|
| 579 |
int j = f(&i); // calls f(const int*)
|
| 580 |
```
|
| 581 |
|
|
|
|
| 582 |
or, if not that,
|
| 583 |
- `S1`
|
| 584 |
and `S2` are reference bindings ([[dcl.init.ref]]), and the types
|
| 585 |
to which the references refer are the same type except for top-level
|
| 586 |
cv-qualifiers, and the type to which the reference initialized by
|
| 587 |
`S2` refers is more cv-qualified than the type to which the
|
| 588 |
reference initialized by `S1` refers.
|
|
|
|
| 589 |
``` cpp
|
| 590 |
int f(const int &);
|
| 591 |
int f(int &);
|
| 592 |
int g(const int &);
|
| 593 |
int g(int);
|
|
@@ -603,31 +759,30 @@ conversion sequences unless one of the following rules applies:
|
|
| 603 |
void g(const X& a, X b) {
|
| 604 |
a.f(); // calls X::f() const
|
| 605 |
b.f(); // calls X::f()
|
| 606 |
}
|
| 607 |
```
|
|
|
|
|
|
|
| 608 |
- User-defined conversion sequence `U1` is a better conversion sequence
|
| 609 |
than another user-defined conversion sequence `U2` if they contain the
|
| 610 |
same user-defined conversion function or constructor or they
|
| 611 |
initialize the same class in an aggregate initialization and in either
|
| 612 |
case the second standard conversion sequence of `U1` is better than
|
| 613 |
the second standard conversion sequence of `U2`.
|
|
|
|
| 614 |
``` cpp
|
| 615 |
struct A {
|
| 616 |
operator short();
|
| 617 |
} a;
|
| 618 |
int f(int);
|
| 619 |
int f(float);
|
| 620 |
int i = f(a); // calls f(int), because short → int is
|
| 621 |
// better than short → float.
|
| 622 |
```
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
- `L1` converts to `std::initializer_list<X>` for some `X` and `L2`
|
| 626 |
-
does not, or, if not that,
|
| 627 |
-
- `L1` converts to type “array of `N1` `T`”, `L2` converts to type
|
| 628 |
-
“array of `N2` `T`”, and `N1` is smaller than `N2`.
|
| 629 |
|
| 630 |
Standard conversion sequences are ordered by their ranks: an Exact Match
|
| 631 |
is a better conversion than a Promotion, which is a better conversion
|
| 632 |
than a Conversion. Two conversion sequences with the same rank are
|
| 633 |
indistinguishable unless one of the following rules applies:
|
|
@@ -643,19 +798,22 @@ indistinguishable unless one of the following rules applies:
|
|
| 643 |
of `B*` to `void*`.
|
| 644 |
- If class `B` is derived directly or indirectly from class `A` and
|
| 645 |
class `C` is derived directly or indirectly from `B`,
|
| 646 |
- conversion of `C*` to `B*` is better than conversion of `C*` to
|
| 647 |
`A*`,
|
|
|
|
| 648 |
``` cpp
|
| 649 |
struct A {};
|
| 650 |
struct B : public A {};
|
| 651 |
struct C : public B {};
|
| 652 |
C* pc;
|
| 653 |
int f(A*);
|
| 654 |
int f(B*);
|
| 655 |
int i = f(pc); // calls f(B*)
|
| 656 |
```
|
|
|
|
|
|
|
| 657 |
- binding of an expression of type `C` to a reference to type `B` is
|
| 658 |
better than binding an expression of type `C` to a reference to type
|
| 659 |
`A`,
|
| 660 |
- conversion of `A::*` to `B::*` is better than conversion of `A::*`
|
| 661 |
to `C::*`,
|
|
@@ -667,11 +825,11 @@ indistinguishable unless one of the following rules applies:
|
|
| 667 |
`A`,
|
| 668 |
- conversion of `B::*` to `C::*` is better than conversion of `A::*`
|
| 669 |
to `C::*`, and
|
| 670 |
- conversion of `B` to `A` is better than conversion of `C` to `A`.
|
| 671 |
|
| 672 |
-
Compared conversion sequences will have different source
|
| 673 |
-
the context of comparing the second standard conversion
|
| 674 |
-
initialization by user-defined conversion (see
|
| 675 |
-
in all other contexts, the source types will be
|
| 676 |
-
target types will be different.
|
| 677 |
|
|
|
|
| 1 |
### Best viable function <a id="over.match.best">[[over.match.best]]</a>
|
| 2 |
|
| 3 |
Define ICS*i*(`F`) as follows:
|
| 4 |
|
| 5 |
+
- If `F` is a static member function, ICS*1*(`F`) is defined such that
|
| 6 |
ICS*1*(`F`) is neither better nor worse than ICS*1*(`G`) for any
|
| 7 |
function `G`, and, symmetrically, ICS*1*(`G`) is neither better nor
|
| 8 |
+
worse than ICS*1*(`F`);[^9] 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
|
|
|
|
| 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 `F1` to the
|
| 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 `F2` to the destination type
|
| 29 |
+
\[*Example 1*:
|
| 30 |
``` cpp
|
| 31 |
struct A {
|
| 32 |
A();
|
| 33 |
operator int();
|
| 34 |
operator double();
|
| 35 |
} a;
|
| 36 |
+
int i = a; // a.operator int() followed by no conversion is better than
|
| 37 |
+
// a.operator double() followed by a conversion to int
|
|
|
|
| 38 |
float x = a; // ambiguous: both possibilities require conversions,
|
| 39 |
// and neither is better than the other
|
| 40 |
```
|
| 41 |
|
| 42 |
+
— *end example*]
|
| 43 |
or, if not that,
|
| 44 |
- the context is an initialization by conversion function for direct
|
| 45 |
reference binding ([[over.match.ref]]) of a reference to function
|
| 46 |
type, the return type of `F1` is the same kind of reference (i.e.
|
| 47 |
lvalue or rvalue) as the reference being initialized, and the return
|
| 48 |
type of `F2` is not
|
| 49 |
+
\[*Example 2*:
|
| 50 |
``` cpp
|
| 51 |
template <class T> struct A {
|
| 52 |
operator T&(); // #1
|
| 53 |
operator T&&(); // #2
|
| 54 |
};
|
|
|
|
| 56 |
A<Fn> a;
|
| 57 |
Fn& lf = a; // calls #1
|
| 58 |
Fn&& rf = a; // calls #2
|
| 59 |
```
|
| 60 |
|
| 61 |
+
— *end example*]
|
| 62 |
or, if not that,
|
| 63 |
- `F1` is not a function template specialization and `F2` is a function
|
| 64 |
template specialization, or, if not that,
|
| 65 |
- `F1` and `F2` are function template specializations, and the function
|
| 66 |
template for `F1` is more specialized than the template for `F2`
|
| 67 |
according to the partial ordering rules described in
|
| 68 |
+
[[temp.func.order]], or, if not that,
|
| 69 |
+
- `F1` is generated from a *deduction-guide* (
|
| 70 |
+
[[over.match.class.deduct]]) and `F2` is not, or, if not that,
|
| 71 |
+
- `F1` is the copy deduction candidate ([[over.match.class.deduct]])
|
| 72 |
+
and `F2` is not, or, if not that,
|
| 73 |
+
- `F1` is generated from a non-template constructor and `F2` is
|
| 74 |
+
generated from a constructor template.
|
| 75 |
+
\[*Example 3*:
|
| 76 |
+
``` cpp
|
| 77 |
+
template <class T> struct A {
|
| 78 |
+
using value_type = T;
|
| 79 |
+
A(value_type); // #1
|
| 80 |
+
A(const A&); // #2
|
| 81 |
+
A(T, T, int); // #3
|
| 82 |
+
template<class U>
|
| 83 |
+
A(int, T, U); // #4
|
| 84 |
+
// #5 is the copy deduction candidate, A(A)
|
| 85 |
+
};
|
| 86 |
+
|
| 87 |
+
A x(1, 2, 3); // uses #3, generated from a non-template constructor
|
| 88 |
+
|
| 89 |
+
template <class T>
|
| 90 |
+
A(T) -> A<T>; // #6, less specialized than #5
|
| 91 |
+
|
| 92 |
+
A a(42); // uses #6 to deduce A<int> and #1 to initialize
|
| 93 |
+
A b = a; // uses #5 to deduce A<int> and #2 to initialize
|
| 94 |
+
|
| 95 |
+
template <class T>
|
| 96 |
+
A(A<T>) -> A<A<T>>; // #7, as specialized as #5
|
| 97 |
+
|
| 98 |
+
A b2 = a; // uses #7 to deduce A<A<int>> and #1 to initialize
|
| 99 |
+
```
|
| 100 |
+
|
| 101 |
+
— *end example*]
|
| 102 |
|
| 103 |
If there is exactly one viable function that is a better function than
|
| 104 |
all other viable functions, then it is the one selected by overload
|
| 105 |
+
resolution; otherwise the call is ill-formed.[^10]
|
| 106 |
+
|
| 107 |
+
[*Example 4*:
|
| 108 |
|
| 109 |
``` cpp
|
| 110 |
void Fcn(const int*, short);
|
| 111 |
void Fcn(int*, int);
|
| 112 |
|
| 113 |
int i;
|
| 114 |
short s = 0;
|
| 115 |
|
| 116 |
void f() {
|
| 117 |
+
Fcn(&i, s); // is ambiguous because &i → int* is better than &i → const int*
|
|
|
|
| 118 |
// but s → short is also better than s → int
|
| 119 |
|
| 120 |
+
Fcn(&i, 1L); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
|
|
|
| 121 |
// and 1L → short and 1L → int are indistinguishable
|
| 122 |
|
| 123 |
+
Fcn(&i, 'c'); // calls Fcn(int*, int), because &i → int* is better than &i → const int*
|
|
|
|
| 124 |
// and c → int is better than c → short
|
| 125 |
}
|
| 126 |
```
|
| 127 |
|
| 128 |
+
— *end example*]
|
| 129 |
+
|
| 130 |
If the best viable function resolves to a function for which multiple
|
| 131 |
declarations were found, and if at least two of these declarations — or
|
| 132 |
the declarations they refer to in the case of *using-declaration*s —
|
| 133 |
specify a default argument that made the function viable, the program is
|
| 134 |
ill-formed.
|
| 135 |
|
| 136 |
+
[*Example 5*:
|
| 137 |
+
|
| 138 |
``` cpp
|
| 139 |
namespace A {
|
| 140 |
extern "C" void f(int = 5);
|
| 141 |
}
|
| 142 |
namespace B {
|
|
|
|
| 150 |
f(3); // OK, default argument was not used for viability
|
| 151 |
f(); // Error: found default argument twice
|
| 152 |
}
|
| 153 |
```
|
| 154 |
|
| 155 |
+
— *end example*]
|
| 156 |
+
|
| 157 |
#### Implicit conversion sequences <a id="over.best.ics">[[over.best.ics]]</a>
|
| 158 |
|
| 159 |
An *implicit conversion sequence* is a sequence of conversions used to
|
| 160 |
convert an argument in a function call to the type of the corresponding
|
| 161 |
parameter of the function being called. The sequence of conversions is
|
|
|
|
| 164 |
single expression ([[dcl.init]], [[dcl.init.ref]]).
|
| 165 |
|
| 166 |
Implicit conversion sequences are concerned only with the type,
|
| 167 |
cv-qualification, and value category of the argument and how these are
|
| 168 |
converted to match the corresponding properties of the parameter. Other
|
| 169 |
+
properties, such as the lifetime, storage class, alignment,
|
| 170 |
+
accessibility of the argument, whether the argument is a bit-field, and
|
| 171 |
+
whether a function is deleted ([[dcl.fct.def.delete]]), are ignored.
|
| 172 |
+
So, although an implicit conversion sequence can be defined for a given
|
| 173 |
+
argument-parameter pair, the conversion from the argument to the
|
| 174 |
+
parameter might still be ill-formed in the final analysis.
|
| 175 |
|
| 176 |
A well-formed implicit conversion sequence is one of the following
|
| 177 |
forms:
|
| 178 |
|
| 179 |
- a *standard conversion sequence* ([[over.ics.scs]]),
|
|
|
|
| 191 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
| 192 |
step of a class copy-initialization,
|
| 193 |
- [[over.match.copy]], [[over.match.conv]], or [[over.match.ref]] (in
|
| 194 |
all cases), or
|
| 195 |
- the second phase of [[over.match.list]] when the initializer list has
|
| 196 |
+
exactly one element that is itself an initializer list, and the target
|
| 197 |
+
is the first parameter of a constructor of class `X`, and the
|
| 198 |
+
conversion is to `X` or reference to cv `X`,
|
| 199 |
|
| 200 |
+
user-defined conversion sequences are not considered.
|
| 201 |
+
|
| 202 |
+
[*Note 1*: These rules prevent more than one user-defined conversion
|
| 203 |
+
from being applied during overload resolution, thereby avoiding infinite
|
| 204 |
+
recursion. — *end note*]
|
| 205 |
+
|
| 206 |
+
[*Example 1*:
|
| 207 |
|
| 208 |
``` cpp
|
| 209 |
struct Y { Y(int); };
|
| 210 |
struct A { operator int(); };
|
| 211 |
Y y1 = A(); // error: A::operator int() is not a candidate
|
|
|
|
| 214 |
struct B { operator X(); };
|
| 215 |
B b;
|
| 216 |
X x({b}); // error: B::operator X() is not a candidate
|
| 217 |
```
|
| 218 |
|
| 219 |
+
— *end example*]
|
| 220 |
+
|
| 221 |
For the case where the parameter type is a reference, see
|
| 222 |
[[over.ics.ref]].
|
| 223 |
|
| 224 |
When the parameter type is not a reference, the implicit conversion
|
| 225 |
sequence models a copy-initialization of the parameter from the argument
|
| 226 |
expression. The implicit conversion sequence is the one required to
|
| 227 |
convert the argument expression to a prvalue of the type of the
|
| 228 |
+
parameter.
|
| 229 |
+
|
| 230 |
+
[*Note 2*: When the parameter has a class type, this is a conceptual
|
| 231 |
conversion defined for the purposes of Clause [[over]]; the actual
|
| 232 |
initialization is defined in terms of constructors and is not a
|
| 233 |
+
conversion. — *end note*]
|
| 234 |
+
|
| 235 |
+
Any difference in top-level cv-qualification is subsumed by the
|
| 236 |
+
initialization itself and does not constitute a conversion.
|
| 237 |
+
|
| 238 |
+
[*Example 2*: A parameter of type `A` can be initialized from an
|
| 239 |
+
argument of type `const A`. The implicit conversion sequence for that
|
| 240 |
+
case is the identity sequence; it contains no “conversion” from
|
| 241 |
+
`const A` to `A`. — *end example*]
|
| 242 |
+
|
| 243 |
When the parameter has a class type and the argument expression has the
|
| 244 |
same type, the implicit conversion sequence is an identity conversion.
|
| 245 |
When the parameter has a class type and the argument expression has a
|
| 246 |
derived class type, the implicit conversion sequence is a
|
| 247 |
derived-to-base Conversion from the derived class to the base class.
|
| 248 |
+
|
| 249 |
+
[*Note 3*: There is no such standard conversion; this derived-to-base
|
| 250 |
+
Conversion exists only in the description of implicit conversion
|
| 251 |
+
sequences. — *end note*]
|
| 252 |
+
|
| 253 |
+
A derived-to-base Conversion has Conversion rank ([[over.ics.scs]]).
|
| 254 |
|
| 255 |
In all contexts, when converting to the implicit object parameter or
|
| 256 |
when converting to the left operand of an assignment operation only
|
| 257 |
+
standard conversion sequences are allowed.
|
|
|
|
| 258 |
|
| 259 |
If no conversions are required to match an argument to a parameter type,
|
| 260 |
the implicit conversion sequence is the standard conversion sequence
|
| 261 |
consisting of the identity conversion ([[over.ics.scs]]).
|
| 262 |
|
| 263 |
If no sequence of conversions can be found to convert an argument to a
|
| 264 |
+
parameter type, an implicit conversion sequence cannot be formed.
|
|
|
|
| 265 |
|
| 266 |
If several different sequences of conversions exist that each convert
|
| 267 |
the argument to the parameter type, the implicit conversion sequence
|
| 268 |
associated with the parameter is defined to be the unique conversion
|
| 269 |
sequence designated the *ambiguous conversion sequence*. For the purpose
|
| 270 |
of ranking implicit conversion sequences as described in
|
| 271 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 272 |
+
user-defined conversion sequence that is indistinguishable from any
|
| 273 |
+
other user-defined conversion sequence.
|
| 274 |
+
|
| 275 |
+
[*Note 4*:
|
| 276 |
+
|
| 277 |
+
This rule prevents a function from becoming non-viable because of an
|
| 278 |
+
ambiguous conversion sequence for one of its parameters.
|
| 279 |
+
|
| 280 |
+
[*Example 3*:
|
| 281 |
+
|
| 282 |
+
``` cpp
|
| 283 |
+
class B;
|
| 284 |
+
class A { A (B&);};
|
| 285 |
+
class B { operator A (); };
|
| 286 |
+
class C { C (B&); };
|
| 287 |
+
void f(A) { }
|
| 288 |
+
void f(C) { }
|
| 289 |
+
B b;
|
| 290 |
+
f(b); // ill-formed: ambiguous because there is a conversion b → C (via constructor)
|
| 291 |
+
// and an (ambiguous) conversion b → A (via constructor or conversion function)
|
| 292 |
+
void f(B) { }
|
| 293 |
+
f(b); // OK, unambiguous
|
| 294 |
+
```
|
| 295 |
+
|
| 296 |
+
— *end example*]
|
| 297 |
+
|
| 298 |
+
— *end note*]
|
| 299 |
+
|
| 300 |
+
If a function that uses the ambiguous conversion sequence is selected as
|
| 301 |
+
the best viable function, the call will be ill-formed because the
|
| 302 |
+
conversion of one of the arguments in the call is ambiguous.
|
| 303 |
|
| 304 |
The three forms of implicit conversion sequences mentioned above are
|
| 305 |
defined in the following subclauses.
|
| 306 |
|
| 307 |
##### Standard conversion sequences <a id="over.ics.scs">[[over.ics.scs]]</a>
|
| 308 |
|
| 309 |
Table [[tab:over.conversions]] summarizes the conversions defined in
|
| 310 |
Clause [[conv]] and partitions them into four disjoint categories:
|
| 311 |
Lvalue Transformation, Qualification Adjustment, Promotion, and
|
| 312 |
+
Conversion.
|
| 313 |
+
|
| 314 |
+
[*Note 5*: These categories are orthogonal with respect to value
|
| 315 |
category, cv-qualification, and data representation: the Lvalue
|
| 316 |
Transformations do not change the cv-qualification or data
|
| 317 |
representation of the type; the Qualification Adjustments do not change
|
| 318 |
the value category or data representation of the type; and the
|
| 319 |
Promotions and Conversions do not change the value category or
|
| 320 |
+
cv-qualification of the type. — *end note*]
|
| 321 |
|
| 322 |
+
[*Note 6*: As described in Clause [[conv]], a standard conversion
|
| 323 |
+
sequence is either the Identity conversion by itself (that is, no
|
| 324 |
+
conversion) or consists of one to three conversions from the other four
|
| 325 |
+
categories. If there are two or more conversions in the sequence, the
|
| 326 |
+
conversions are applied in the canonical order: **Lvalue
|
|
|
|
| 327 |
Transformation**, **Promotion** or **Conversion**, **Qualification
|
| 328 |
+
Adjustment**. — *end note*]
|
| 329 |
|
| 330 |
Each conversion in Table [[tab:over.conversions]] also has an
|
| 331 |
associated rank (Exact Match, Promotion, or Conversion). These are used
|
| 332 |
to rank standard conversion sequences ([[over.ics.rank]]). The rank of
|
| 333 |
a conversion sequence is determined by considering the rank of each
|
|
|
|
| 388 |
an argument expression, the implicit conversion sequence is the identity
|
| 389 |
conversion, unless the argument expression has a type that is a derived
|
| 390 |
class of the parameter type, in which case the implicit conversion
|
| 391 |
sequence is a derived-to-base Conversion ([[over.best.ics]]).
|
| 392 |
|
| 393 |
+
[*Example 4*:
|
| 394 |
+
|
| 395 |
``` cpp
|
| 396 |
struct A {};
|
| 397 |
struct B : public A {} b;
|
| 398 |
int f(A&);
|
| 399 |
int f(B&);
|
| 400 |
+
int i = f(b); // calls f(B&), an exact match, rather than f(A&), a conversion
|
|
|
|
| 401 |
```
|
| 402 |
|
| 403 |
+
— *end example*]
|
| 404 |
+
|
| 405 |
If the parameter binds directly to the result of applying a conversion
|
| 406 |
function to the argument expression, the implicit conversion sequence is
|
| 407 |
a user-defined conversion sequence ([[over.ics.user]]), with the second
|
| 408 |
standard conversion sequence either an identity conversion or, if the
|
| 409 |
conversion function returns an entity of a type that is a derived class
|
| 410 |
of the parameter type, a derived-to-base Conversion.
|
| 411 |
|
| 412 |
When a parameter of reference type is not bound directly to an argument
|
| 413 |
expression, the conversion sequence is the one required to convert the
|
| 414 |
+
argument expression to the referenced type according to
|
| 415 |
+
[[over.best.ics]]. Conceptually, this conversion sequence corresponds to
|
| 416 |
+
copy-initializing a temporary of the referenced type with the argument
|
| 417 |
+
expression. Any difference in top-level cv-qualification is subsumed by
|
| 418 |
+
the initialization itself and does not constitute a conversion.
|
|
|
|
| 419 |
|
| 420 |
Except for an implicit object parameter, for which see
|
| 421 |
[[over.match.funcs]], a standard conversion sequence cannot be formed if
|
| 422 |
it requires binding an lvalue reference other than a reference to a
|
| 423 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 424 |
+
an lvalue other than a function lvalue.
|
| 425 |
+
|
| 426 |
+
[*Note 7*: This means, for example, that a candidate function cannot be
|
| 427 |
+
a viable function if it has a non-`const` lvalue reference parameter
|
| 428 |
+
(other than the implicit object parameter) and the corresponding
|
| 429 |
+
argument would require a temporary to be created to initialize the
|
| 430 |
+
lvalue reference (see [[dcl.init.ref]]). — *end note*]
|
| 431 |
|
| 432 |
Other restrictions on binding a reference to a particular argument that
|
| 433 |
are not based on the types of the reference and the argument do not
|
| 434 |
+
affect the formation of a standard conversion sequence, however.
|
| 435 |
+
|
| 436 |
+
[*Example 5*: A function with an “lvalue reference to `int`” parameter
|
| 437 |
+
can be a viable candidate even if the corresponding argument is an `int`
|
| 438 |
+
bit-field. The formation of implicit conversion sequences treats the
|
| 439 |
+
`int` bit-field as an `int` lvalue and finds an exact match with the
|
| 440 |
+
parameter. If the function is selected by overload resolution, the call
|
| 441 |
+
will nonetheless be ill-formed because of the prohibition on binding a
|
| 442 |
+
non-`const` lvalue reference to a bit-field (
|
| 443 |
+
[[dcl.init.ref]]). — *end example*]
|
| 444 |
|
| 445 |
##### List-initialization sequence <a id="over.ics.list">[[over.ics.list]]</a>
|
| 446 |
|
| 447 |
When an argument is an initializer list ([[dcl.init.list]]), it is not
|
| 448 |
an expression and special rules apply for converting it to a parameter
|
| 449 |
type.
|
| 450 |
|
| 451 |
+
If the parameter type is an aggregate class `X` and the initializer list
|
| 452 |
+
has a single element of type cv `U`, where `U` is `X` or a class derived
|
| 453 |
+
from `X`, the implicit conversion sequence is the one required to
|
| 454 |
+
convert the element to the parameter type.
|
| 455 |
+
|
| 456 |
+
Otherwise, if the parameter type is a character array [^11] and the
|
| 457 |
+
initializer list has a single element that is an appropriately-typed
|
| 458 |
+
string literal ([[dcl.init.string]]), the implicit conversion sequence
|
| 459 |
+
is the identity conversion.
|
| 460 |
+
|
| 461 |
+
Otherwise, if the parameter type is `std::initializer_list<X>` and all
|
| 462 |
+
the elements of the initializer list can be implicitly converted to `X`,
|
| 463 |
+
the implicit conversion sequence is the worst conversion necessary to
|
| 464 |
+
convert an element of the list to `X`, or if the initializer list has no
|
| 465 |
+
elements, the identity conversion. This conversion can be a user-defined
|
| 466 |
conversion even in the context of a call to an initializer-list
|
| 467 |
constructor.
|
| 468 |
|
| 469 |
+
[*Example 6*:
|
| 470 |
+
|
| 471 |
``` cpp
|
| 472 |
void f(std::initializer_list<int>);
|
| 473 |
f( {} ); // OK: f(initializer_list<int>) identity conversion
|
| 474 |
f( {1,2,3} ); // OK: f(initializer_list<int>) identity conversion
|
| 475 |
f( {'a','b'} ); // OK: f(initializer_list<int>) integral promotion
|
|
|
|
| 488 |
typedef int IA[3];
|
| 489 |
void h(const IA&);
|
| 490 |
h({ 1, 2, 3 }); // OK: identity conversion
|
| 491 |
```
|
| 492 |
|
| 493 |
+
— *end example*]
|
| 494 |
+
|
| 495 |
+
Otherwise, if the parameter type is “array of `N` `X`”, if there exists
|
| 496 |
+
an implicit conversion sequence for each element of the array from the
|
| 497 |
+
corresponding element of the initializer list (or from `{}` if there is
|
| 498 |
+
no such element), the implicit conversion sequence is the worst such
|
| 499 |
+
implicit conversion sequence.
|
| 500 |
|
| 501 |
Otherwise, if the parameter is a non-aggregate class `X` and overload
|
| 502 |
+
resolution per [[over.match.list]] chooses a single best constructor
|
| 503 |
+
`C` of `X` to perform the initialization of an object of type `X` from
|
| 504 |
+
the argument initializer list:
|
| 505 |
+
|
| 506 |
+
- If `C` is not an initializer-list constructor and the initializer list
|
| 507 |
+
has a single element of type cv `U`, where `U` is `X` or a class
|
| 508 |
+
derived from `X`, the implicit conversion sequence has Exact Match
|
| 509 |
+
rank if `U` is `X`, or Conversion rank if `U` is derived from `X`.
|
| 510 |
+
- Otherwise, the implicit conversion sequence is a user-defined
|
| 511 |
+
conversion sequence with the second standard conversion sequence an
|
| 512 |
+
identity conversion.
|
| 513 |
+
|
| 514 |
+
If multiple constructors are viable but none is better than the others,
|
| 515 |
+
the implicit conversion sequence is the ambiguous conversion sequence.
|
| 516 |
+
User-defined conversions are allowed for conversion of the initializer
|
| 517 |
+
list elements to the constructor parameter types except as noted in
|
| 518 |
+
[[over.best.ics]].
|
| 519 |
+
|
| 520 |
+
[*Example 7*:
|
| 521 |
|
| 522 |
``` cpp
|
| 523 |
struct A {
|
| 524 |
A(std::initializer_list<int>);
|
| 525 |
};
|
|
|
|
| 547 |
};
|
| 548 |
void i(D);
|
| 549 |
i({ {1,2}, {"bar"} }); // OK: i(D(A(std::initializer_list<int>{1,2\), C(std::string("bar"))))}
|
| 550 |
```
|
| 551 |
|
| 552 |
+
— *end example*]
|
| 553 |
+
|
| 554 |
Otherwise, if the parameter has an aggregate type which can be
|
| 555 |
initialized from the initializer list according to the rules for
|
| 556 |
aggregate initialization ([[dcl.init.aggr]]), the implicit conversion
|
| 557 |
sequence is a user-defined conversion sequence with the second standard
|
| 558 |
conversion sequence an identity conversion.
|
| 559 |
|
| 560 |
+
[*Example 8*:
|
| 561 |
+
|
| 562 |
``` cpp
|
| 563 |
struct A {
|
| 564 |
int m1;
|
| 565 |
double m2;
|
| 566 |
};
|
|
|
|
| 568 |
void f(A);
|
| 569 |
f( {'a', 'b'} ); // OK: f(A(int,double)) user-defined conversion
|
| 570 |
f( {1.0} ); // error: narrowing
|
| 571 |
```
|
| 572 |
|
| 573 |
+
— *end example*]
|
| 574 |
+
|
| 575 |
+
Otherwise, if the parameter is a reference, see [[over.ics.ref]].
|
| 576 |
+
|
| 577 |
+
[*Note 8*: The rules in this section will apply for initializing the
|
| 578 |
+
underlying temporary for the reference. — *end note*]
|
| 579 |
+
|
| 580 |
+
[*Example 9*:
|
| 581 |
|
| 582 |
``` cpp
|
| 583 |
struct A {
|
| 584 |
int m1;
|
| 585 |
double m2;
|
|
|
|
| 591 |
|
| 592 |
void g(const double &);
|
| 593 |
g({1}); // same conversion as int to double
|
| 594 |
```
|
| 595 |
|
| 596 |
+
— *end example*]
|
| 597 |
+
|
| 598 |
Otherwise, if the parameter type is not a class:
|
| 599 |
|
| 600 |
+
- if the initializer list has one element that is not itself an
|
| 601 |
+
initializer list, the implicit conversion sequence is the one required
|
| 602 |
+
to convert the element to the parameter type;
|
| 603 |
+
\[*Example 10*:
|
| 604 |
``` cpp
|
| 605 |
void f(int);
|
| 606 |
f( {'a'} ); // OK: same conversion as char to int
|
| 607 |
f( {1.0} ); // error: narrowing
|
| 608 |
```
|
| 609 |
+
|
| 610 |
+
— *end example*]
|
| 611 |
- if the initializer list has no elements, the implicit conversion
|
| 612 |
sequence is the identity conversion.
|
| 613 |
+
\[*Example 11*:
|
| 614 |
``` cpp
|
| 615 |
void f(int);
|
| 616 |
f( { } ); // OK: identity conversion
|
| 617 |
```
|
| 618 |
|
| 619 |
+
— *end example*]
|
| 620 |
+
|
| 621 |
In all cases other than those enumerated above, no conversion is
|
| 622 |
possible.
|
| 623 |
|
| 624 |
#### Ranking implicit conversion sequences <a id="over.ics.rank">[[over.ics.rank]]</a>
|
| 625 |
|
| 626 |
+
This subclause defines a partial ordering of implicit conversion
|
| 627 |
sequences based on the relationships *better conversion sequence* and
|
| 628 |
*better conversion*. If an implicit conversion sequence S1 is defined by
|
| 629 |
these rules to be a better conversion sequence than S2, then it is also
|
| 630 |
the case that S2 is a *worse conversion sequence* than S1. If conversion
|
| 631 |
sequence S1 is neither better than nor worse than conversion sequence
|
|
|
|
| 642 |
[[over.ics.ellipsis]]).
|
| 643 |
|
| 644 |
Two implicit conversion sequences of the same form are indistinguishable
|
| 645 |
conversion sequences unless one of the following rules applies:
|
| 646 |
|
| 647 |
+
- List-initialization sequence `L1` is a better conversion sequence than
|
| 648 |
+
list-initialization sequence `L2` if
|
| 649 |
+
- `L1` converts to `std::initializer_list<X>` for some `X` and `L2`
|
| 650 |
+
does not, or, if not that,
|
| 651 |
+
- `L1` converts to type “array of `N1` `T`”, `L2` converts to type
|
| 652 |
+
“array of `N2` `T`”, and `N1` is smaller than `N2`,
|
| 653 |
+
|
| 654 |
+
even if one of the other rules in this paragraph would otherwise
|
| 655 |
+
apply.
|
| 656 |
+
\[*Example 1*:
|
| 657 |
+
``` cpp
|
| 658 |
+
void f1(int); // #1
|
| 659 |
+
void f1(std::initializer_list<long>); // #2
|
| 660 |
+
void g1() { f1({42}); } // chooses #2
|
| 661 |
+
|
| 662 |
+
void f2(std::pair<const char*, const char*>); // #3
|
| 663 |
+
void f2(std::initializer_list<std::string>); // #4
|
| 664 |
+
void g2() { f2({"foo","bar"}); } // chooses #4
|
| 665 |
+
```
|
| 666 |
+
|
| 667 |
+
— *end example*]
|
| 668 |
- Standard conversion sequence `S1` is a better conversion sequence than
|
| 669 |
standard conversion sequence `S2` if
|
| 670 |
- `S1` is a proper subsequence of `S2` (comparing the conversion
|
| 671 |
sequences in the canonical form defined by [[over.ics.scs]],
|
| 672 |
excluding any Lvalue Transformation; the identity conversion
|
|
|
|
| 676 |
have the same rank and are distinguishable by the rules in the
|
| 677 |
paragraph below, or, if not that,
|
| 678 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and neither
|
| 679 |
refers to an implicit object parameter of a non-static member
|
| 680 |
function declared without a *ref-qualifier*, and `S1` binds an
|
| 681 |
+
rvalue reference to an rvalue and `S2` binds an lvalue reference
|
| 682 |
+
\[*Example 2*:
|
| 683 |
``` cpp
|
| 684 |
int i;
|
| 685 |
int f1();
|
| 686 |
int&& f2();
|
| 687 |
int g(const int&);
|
|
|
|
| 703 |
a << 'c'; // calls A::operator<<(int)
|
| 704 |
A().p(); // calls A::p()&&
|
| 705 |
a.p(); // calls A::p()&
|
| 706 |
```
|
| 707 |
|
| 708 |
+
— *end example*]
|
| 709 |
or, if not that,
|
| 710 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and `S1`
|
| 711 |
binds an lvalue reference to a function lvalue and `S2` binds an
|
| 712 |
+
rvalue reference to a function lvalue
|
| 713 |
+
\[*Example 3*:
|
| 714 |
``` cpp
|
| 715 |
int f(void(&)()); // #1
|
| 716 |
int f(void(&&)()); // #2
|
| 717 |
void g();
|
| 718 |
int i1 = f(g); // calls #1
|
| 719 |
```
|
| 720 |
|
| 721 |
+
— *end example*]
|
| 722 |
or, if not that,
|
| 723 |
- `S1`
|
| 724 |
and `S2` differ only in their qualification conversion and yield
|
| 725 |
similar types `T1` and `T2` ([[conv.qual]]), respectively, and the
|
| 726 |
cv-qualification signature of type `T1` is a proper subset of the
|
| 727 |
+
cv-qualification signature of type `T2`
|
| 728 |
+
\[*Example 4*:
|
| 729 |
``` cpp
|
| 730 |
int f(const volatile int *);
|
| 731 |
int f(const int *);
|
| 732 |
int i;
|
| 733 |
int j = f(&i); // calls f(const int*)
|
| 734 |
```
|
| 735 |
|
| 736 |
+
— *end example*]
|
| 737 |
or, if not that,
|
| 738 |
- `S1`
|
| 739 |
and `S2` are reference bindings ([[dcl.init.ref]]), and the types
|
| 740 |
to which the references refer are the same type except for top-level
|
| 741 |
cv-qualifiers, and the type to which the reference initialized by
|
| 742 |
`S2` refers is more cv-qualified than the type to which the
|
| 743 |
reference initialized by `S1` refers.
|
| 744 |
+
\[*Example 5*:
|
| 745 |
``` cpp
|
| 746 |
int f(const int &);
|
| 747 |
int f(int &);
|
| 748 |
int g(const int &);
|
| 749 |
int g(int);
|
|
|
|
| 759 |
void g(const X& a, X b) {
|
| 760 |
a.f(); // calls X::f() const
|
| 761 |
b.f(); // calls X::f()
|
| 762 |
}
|
| 763 |
```
|
| 764 |
+
|
| 765 |
+
— *end example*]
|
| 766 |
- User-defined conversion sequence `U1` is a better conversion sequence
|
| 767 |
than another user-defined conversion sequence `U2` if they contain the
|
| 768 |
same user-defined conversion function or constructor or they
|
| 769 |
initialize the same class in an aggregate initialization and in either
|
| 770 |
case the second standard conversion sequence of `U1` is better than
|
| 771 |
the second standard conversion sequence of `U2`.
|
| 772 |
+
\[*Example 6*:
|
| 773 |
``` cpp
|
| 774 |
struct A {
|
| 775 |
operator short();
|
| 776 |
} a;
|
| 777 |
int f(int);
|
| 778 |
int f(float);
|
| 779 |
int i = f(a); // calls f(int), because short → int is
|
| 780 |
// better than short → float.
|
| 781 |
```
|
| 782 |
+
|
| 783 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 784 |
|
| 785 |
Standard conversion sequences are ordered by their ranks: an Exact Match
|
| 786 |
is a better conversion than a Promotion, which is a better conversion
|
| 787 |
than a Conversion. Two conversion sequences with the same rank are
|
| 788 |
indistinguishable unless one of the following rules applies:
|
|
|
|
| 798 |
of `B*` to `void*`.
|
| 799 |
- If class `B` is derived directly or indirectly from class `A` and
|
| 800 |
class `C` is derived directly or indirectly from `B`,
|
| 801 |
- conversion of `C*` to `B*` is better than conversion of `C*` to
|
| 802 |
`A*`,
|
| 803 |
+
\[*Example 7*:
|
| 804 |
``` cpp
|
| 805 |
struct A {};
|
| 806 |
struct B : public A {};
|
| 807 |
struct C : public B {};
|
| 808 |
C* pc;
|
| 809 |
int f(A*);
|
| 810 |
int f(B*);
|
| 811 |
int i = f(pc); // calls f(B*)
|
| 812 |
```
|
| 813 |
+
|
| 814 |
+
— *end example*]
|
| 815 |
- binding of an expression of type `C` to a reference to type `B` is
|
| 816 |
better than binding an expression of type `C` to a reference to type
|
| 817 |
`A`,
|
| 818 |
- conversion of `A::*` to `B::*` is better than conversion of `A::*`
|
| 819 |
to `C::*`,
|
|
|
|
| 825 |
`A`,
|
| 826 |
- conversion of `B::*` to `C::*` is better than conversion of `A::*`
|
| 827 |
to `C::*`, and
|
| 828 |
- conversion of `B` to `A` is better than conversion of `C` to `A`.
|
| 829 |
|
| 830 |
+
\[*Note 1*: Compared conversion sequences will have different source
|
| 831 |
+
types only in the context of comparing the second standard conversion
|
| 832 |
+
sequence of an initialization by user-defined conversion (see
|
| 833 |
+
[[over.match.best]]); in all other contexts, the source types will be
|
| 834 |
+
the same and the target types will be different. — *end note*]
|
| 835 |
|