- tmp/tmp1nv356qm/{from.md → to.md} +236 -137
tmp/tmp1nv356qm/{from.md → to.md}
RENAMED
|
@@ -3,11 +3,11 @@
|
|
| 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 |
- 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
|
|
@@ -40,14 +40,14 @@ and then
|
|
| 40 |
```
|
| 41 |
|
| 42 |
— *end example*]
|
| 43 |
or, if not that,
|
| 44 |
- the context is an initialization by conversion function for direct
|
| 45 |
-
reference binding
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
\[*Example 2*:
|
| 50 |
``` cpp
|
| 51 |
template <class T> struct A {
|
| 52 |
operator T&(); // #1
|
| 53 |
operator T&&(); // #2
|
|
@@ -64,17 +64,67 @@ and then
|
|
| 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`
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 76 |
``` cpp
|
| 77 |
template <class T> struct A {
|
| 78 |
using value_type = T;
|
| 79 |
A(value_type); // #1
|
| 80 |
A(const A&); // #2
|
|
@@ -100,13 +150,13 @@ and then
|
|
| 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.[^
|
| 106 |
|
| 107 |
-
[*Example
|
| 108 |
|
| 109 |
``` cpp
|
| 110 |
void Fcn(const int*, short);
|
| 111 |
void Fcn(int*, int);
|
| 112 |
|
|
@@ -131,11 +181,11 @@ 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
|
| 137 |
|
| 138 |
``` cpp
|
| 139 |
namespace A {
|
| 140 |
extern "C" void f(int = 5);
|
| 141 |
}
|
|
@@ -146,41 +196,43 @@ namespace B {
|
|
| 146 |
using A::f;
|
| 147 |
using B::f;
|
| 148 |
|
| 149 |
void use() {
|
| 150 |
f(3); // OK, default argument was not used for viability
|
| 151 |
-
f(); //
|
| 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
|
| 162 |
-
an implicit conversion as defined in
|
| 163 |
governed by the rules for initialization of an object or reference by a
|
| 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.
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
| 175 |
|
| 176 |
A well-formed implicit conversion sequence is one of the following
|
| 177 |
forms:
|
| 178 |
|
| 179 |
-
- a
|
| 180 |
-
- a
|
| 181 |
-
- an
|
| 182 |
|
| 183 |
However, if the target is
|
| 184 |
|
| 185 |
- the first parameter of a constructor or
|
| 186 |
- the implicit object parameter of a user-defined conversion function
|
|
@@ -197,25 +249,25 @@ by
|
|
| 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
|
| 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
|
| 212 |
|
| 213 |
-
|
| 214 |
struct B { operator X(); };
|
| 215 |
B b;
|
| 216 |
-
|
| 217 |
```
|
| 218 |
|
| 219 |
— *end example*]
|
| 220 |
|
| 221 |
For the case where the parameter type is a reference, see
|
|
@@ -225,12 +277,12 @@ 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
|
| 231 |
-
conversion defined for the purposes of
|
| 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.
|
|
@@ -242,39 +294,39 @@ case is the identity sequence; it contains no “conversion” from
|
|
| 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
|
| 248 |
|
| 249 |
-
[*Note
|
| 250 |
-
|
| 251 |
sequences. — *end note*]
|
| 252 |
|
| 253 |
-
A derived-to-base
|
| 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
|
| 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
|
| 267 |
-
the argument to the parameter type, the implicit conversion
|
| 268 |
-
associated with the parameter is defined to be the unique
|
| 269 |
-
sequence designated the *ambiguous conversion sequence*. For
|
| 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
|
| 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*:
|
|
@@ -285,11 +337,11 @@ 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); //
|
| 291 |
// and an (ambiguous) conversion b → A (via constructor or conversion function)
|
| 292 |
void f(B) { }
|
| 293 |
f(b); // OK, unambiguous
|
| 294 |
```
|
| 295 |
|
|
@@ -304,69 +356,66 @@ conversion of one of the arguments in the call is ambiguous.
|
|
| 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 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
Conversion.
|
| 313 |
|
| 314 |
-
[*Note
|
| 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
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
Adjustment**. — *end note*]
|
| 329 |
|
| 330 |
-
Each conversion in
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
rank.
|
| 339 |
|
| 340 |
-
**Table: Conversions** <a id="
|
| 341 |
|
| 342 |
| Conversion | Category | Rank | Subclause |
|
| 343 |
| ----------------------- | -------- | ---- | ----------------- |
|
| 344 |
| No conversions required | Identity | | |
|
| 345 |
| Integral promotions | | | [[conv.prom]] |
|
| 346 |
| Integral conversions | | | [[conv.integral]] |
|
| 347 |
|
| 348 |
|
| 349 |
##### User-defined conversion sequences <a id="over.ics.user">[[over.ics.user]]</a>
|
| 350 |
|
| 351 |
-
A user-defined conversion sequence consists of an initial standard
|
| 352 |
-
conversion sequence followed by a user-defined conversion
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
function.
|
| 361 |
|
| 362 |
The second standard conversion sequence converts the result of the
|
| 363 |
-
user-defined conversion to the target type for the sequence
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
|
|
|
| 368 |
|
| 369 |
If the user-defined conversion is specified by a specialization of a
|
| 370 |
conversion function template, the second standard conversion sequence
|
| 371 |
shall have exact match rank.
|
| 372 |
|
|
@@ -382,15 +431,15 @@ An ellipsis conversion sequence occurs when an argument in a function
|
|
| 382 |
call is matched with the ellipsis parameter specification of the
|
| 383 |
function called (see [[expr.call]]).
|
| 384 |
|
| 385 |
##### Reference binding <a id="over.ics.ref">[[over.ics.ref]]</a>
|
| 386 |
|
| 387 |
-
When a parameter of reference type binds directly
|
| 388 |
-
|
| 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
|
| 392 |
|
| 393 |
[*Example 4*:
|
| 394 |
|
| 395 |
``` cpp
|
| 396 |
struct A {};
|
|
@@ -402,73 +451,108 @@ int i = f(b); // calls f(B&), an exact match, rather than f(A&), a convers
|
|
| 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
|
| 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
|
| 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]],
|
| 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
|
| 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
|
| 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]]
|
| 444 |
|
| 445 |
##### List-initialization sequence <a id="over.ics.list">[[over.ics.list]]</a>
|
| 446 |
|
| 447 |
-
When an argument is an initializer list
|
| 448 |
-
|
| 449 |
type.
|
| 450 |
|
| 451 |
-
If the
|
| 452 |
-
|
| 453 |
-
from
|
| 454 |
-
|
|
|
|
|
|
|
| 455 |
|
| 456 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
initializer list has a single element that is an appropriately-typed
|
| 458 |
-
string
|
| 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
|
| 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
|
|
@@ -490,15 +574,16 @@ 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`”
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 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:
|
|
@@ -515,11 +600,11 @@ 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
|
| 521 |
|
| 522 |
``` cpp
|
| 523 |
struct A {
|
| 524 |
A(std::initializer_list<int>);
|
| 525 |
};
|
|
@@ -551,15 +636,15 @@ i({ {1,2}, {"bar"} }); // OK: i(D(A(std::initializer_list<int>{1,2\), C(std::st
|
|
| 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
|
| 557 |
sequence is a user-defined conversion sequence with the second standard
|
| 558 |
conversion sequence an identity conversion.
|
| 559 |
|
| 560 |
-
[*Example
|
| 561 |
|
| 562 |
``` cpp
|
| 563 |
struct A {
|
| 564 |
int m1;
|
| 565 |
double m2;
|
|
@@ -572,14 +657,14 @@ f( {1.0} ); // error: narrowing
|
|
| 572 |
|
| 573 |
— *end example*]
|
| 574 |
|
| 575 |
Otherwise, if the parameter is a reference, see [[over.ics.ref]].
|
| 576 |
|
| 577 |
-
[*Note
|
| 578 |
underlying temporary for the reference. — *end note*]
|
| 579 |
|
| 580 |
-
[*Example
|
| 581 |
|
| 582 |
``` cpp
|
| 583 |
struct A {
|
| 584 |
int m1;
|
| 585 |
double m2;
|
|
@@ -598,21 +683,21 @@ g({1}); // same conversion as int to double
|
|
| 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
|
| 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
|
| 614 |
``` cpp
|
| 615 |
void f(int);
|
| 616 |
f( { } ); // OK: identity conversion
|
| 617 |
```
|
| 618 |
|
|
@@ -632,26 +717,28 @@ sequence S1 is neither better than nor worse than conversion sequence
|
|
| 632 |
S2, S1 and S2 are said to be *indistinguishable conversion sequences*.
|
| 633 |
|
| 634 |
When comparing the basic forms of implicit conversion sequences (as
|
| 635 |
defined in [[over.best.ics]])
|
| 636 |
|
| 637 |
-
- a standard conversion sequence
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
- a user-defined conversion sequence
|
| 641 |
-
conversion sequence than an ellipsis 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`
|
| 652 |
-
|
|
|
|
|
|
|
| 653 |
|
| 654 |
even if one of the other rules in this paragraph would otherwise
|
| 655 |
apply.
|
| 656 |
\[*Example 1*:
|
| 657 |
``` cpp
|
|
@@ -662,10 +749,24 @@ conversion sequences unless one of the following rules applies:
|
|
| 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]],
|
|
@@ -673,15 +774,15 @@ conversion sequences unless one of the following rules applies:
|
|
| 673 |
sequence is considered to be a subsequence of any non-identity
|
| 674 |
conversion sequence) or, if not that,
|
| 675 |
- the rank of `S1` is better than the rank of `S2`, or `S1` and `S2`
|
| 676 |
have the same rank and are distinguishable by the rules in the
|
| 677 |
paragraph below, or, if not that,
|
| 678 |
-
- `S1` and `S2`
|
| 679 |
-
refers to an implicit object parameter of a non-static
|
| 680 |
-
function declared without a *ref-qualifier*, and `S1` binds
|
| 681 |
-
rvalue reference to an rvalue and `S2` binds an lvalue reference
|
| 682 |
-
\[*Example
|
| 683 |
``` cpp
|
| 684 |
int i;
|
| 685 |
int f1();
|
| 686 |
int&& f2();
|
| 687 |
int g(const int&);
|
|
@@ -705,45 +806,43 @@ conversion sequences unless one of the following rules applies:
|
|
| 705 |
a.p(); // calls A::p()&
|
| 706 |
```
|
| 707 |
|
| 708 |
— *end example*]
|
| 709 |
or, if not that,
|
| 710 |
-
- `S1` and `S2`
|
| 711 |
binds an lvalue reference to a function lvalue and `S2` binds an
|
| 712 |
rvalue reference to a function lvalue
|
| 713 |
-
\[*Example
|
| 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
|
| 725 |
-
|
| 726 |
-
|
| 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`
|
| 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
|
| 745 |
``` cpp
|
| 746 |
int f(const int &);
|
| 747 |
int f(int &);
|
| 748 |
int g(const int &);
|
| 749 |
int g(int);
|
|
@@ -767,11 +866,11 @@ conversion sequences unless one of the following rules applies:
|
|
| 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
|
| 773 |
``` cpp
|
| 774 |
struct A {
|
| 775 |
operator short();
|
| 776 |
} a;
|
| 777 |
int f(int);
|
|
@@ -785,12 +884,12 @@ conversion sequences unless one of the following rules applies:
|
|
| 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:
|
| 789 |
|
| 790 |
-
- A conversion that does not convert a pointer
|
| 791 |
-
`
|
| 792 |
- A conversion that promotes an enumeration whose underlying type is
|
| 793 |
fixed to its underlying type is better than one that promotes to the
|
| 794 |
promoted underlying type, if the two are different.
|
| 795 |
- If class `B` is derived directly or indirectly from class `A`,
|
| 796 |
conversion of `B*` to `A*` is better than conversion of `B*` to
|
|
@@ -798,11 +897,11 @@ 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
|
| 804 |
``` cpp
|
| 805 |
struct A {};
|
| 806 |
struct B : public A {};
|
| 807 |
struct C : public B {};
|
| 808 |
C* pc;
|
|
|
|
| 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`);[^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
|
|
|
|
| 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 type,
|
| 46 |
+
the return type of `F1` is the same kind of reference (lvalue or
|
| 47 |
+
rvalue) as the reference being initialized, and the return type of
|
| 48 |
+
`F2` is not
|
| 49 |
\[*Example 2*:
|
| 50 |
``` cpp
|
| 51 |
template <class T> struct A {
|
| 52 |
operator T&(); // #1
|
| 53 |
operator T&&(); // #2
|
|
|
|
| 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` and `F2` are non-template functions with the same
|
| 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 |
+
};
|
| 81 |
+
|
| 82 |
+
struct B: A {
|
| 83 |
+
using A::A;
|
| 84 |
+
B();
|
| 85 |
+
};
|
| 86 |
+
|
| 87 |
+
int main() {
|
| 88 |
+
B b; // OK, B::B()
|
| 89 |
+
}
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
— *end example*]
|
| 93 |
+
or, if not that,
|
| 94 |
+
- `F2` is a rewritten candidate [[over.match.oper]] and `F1` is not
|
| 95 |
+
\[*Example 4*:
|
| 96 |
+
``` cpp
|
| 97 |
+
struct S {
|
| 98 |
+
friend auto operator<=>(const S&, const S&) = default; // #1
|
| 99 |
+
friend bool operator<(const S&, const S&); // #2
|
| 100 |
+
};
|
| 101 |
+
bool b = S() < S(); // calls #2
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
— *end example*]
|
| 105 |
+
or, if not that,
|
| 106 |
+
- `F1` and `F2` are rewritten candidates, and `F2` is a synthesized
|
| 107 |
+
candidate with reversed order of parameters and `F1` is not
|
| 108 |
+
\[*Example 5*:
|
| 109 |
+
``` cpp
|
| 110 |
+
struct S {
|
| 111 |
+
friend std::weak_ordering operator<=>(const S&, int); // #1
|
| 112 |
+
friend std::weak_ordering operator<=>(int, const S&); // #2
|
| 113 |
+
};
|
| 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
|
| 124 |
generated from a constructor template.
|
| 125 |
+
\[*Example 6*:
|
| 126 |
``` cpp
|
| 127 |
template <class T> struct A {
|
| 128 |
using value_type = T;
|
| 129 |
A(value_type); // #1
|
| 130 |
A(const A&); // #2
|
|
|
|
| 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.[^9]
|
| 156 |
|
| 157 |
+
[*Example 7*:
|
| 158 |
|
| 159 |
``` cpp
|
| 160 |
void Fcn(const int*, short);
|
| 161 |
void Fcn(int*, int);
|
| 162 |
|
|
|
|
| 181 |
declarations were found, and if at least two of these declarations — or
|
| 182 |
the declarations they refer to in the case of *using-declaration*s —
|
| 183 |
specify a default argument that made the function viable, the program is
|
| 184 |
ill-formed.
|
| 185 |
|
| 186 |
+
[*Example 8*:
|
| 187 |
|
| 188 |
``` cpp
|
| 189 |
namespace A {
|
| 190 |
extern "C" void f(int = 5);
|
| 191 |
}
|
|
|
|
| 196 |
using A::f;
|
| 197 |
using B::f;
|
| 198 |
|
| 199 |
void use() {
|
| 200 |
f(3); // OK, default argument was not used for viability
|
| 201 |
+
f(); // error: found default argument twice
|
| 202 |
}
|
| 203 |
```
|
| 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 ([[dcl.init]], [[dcl.init.ref]]).
|
| 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 |
+
|
| 220 |
+
[*Note 1*: Other properties, such as the lifetime, storage class,
|
| 221 |
+
alignment, accessibility of the argument, whether the argument is a
|
| 222 |
+
bit-field, and whether a function is deleted [[dcl.fct.def.delete]], are
|
| 223 |
+
ignored. So, although an implicit conversion sequence can be defined for
|
| 224 |
+
a given argument-parameter pair, the conversion from the argument to the
|
| 225 |
+
parameter might still be ill-formed in the final
|
| 226 |
+
analysis. — *end note*]
|
| 227 |
|
| 228 |
A well-formed implicit conversion sequence is one of the following
|
| 229 |
forms:
|
| 230 |
|
| 231 |
+
- a standard conversion sequence [[over.ics.scs]],
|
| 232 |
+
- a user-defined conversion sequence [[over.ics.user]], or
|
| 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 implicit object parameter of a user-defined conversion function
|
|
|
|
| 249 |
is the first parameter of a constructor of class `X`, and the
|
| 250 |
conversion is to `X` or reference to cv `X`,
|
| 251 |
|
| 252 |
user-defined conversion sequences are not considered.
|
| 253 |
|
| 254 |
+
[*Note 2*: These rules prevent more than one user-defined conversion
|
| 255 |
from being applied during overload resolution, thereby avoiding infinite
|
| 256 |
recursion. — *end note*]
|
| 257 |
|
| 258 |
[*Example 1*:
|
| 259 |
|
| 260 |
``` cpp
|
| 261 |
struct Y { Y(int); };
|
| 262 |
struct A { operator int(); };
|
| 263 |
Y y1 = A(); // error: A::operator int() is not a candidate
|
| 264 |
|
| 265 |
+
struct X { X(); };
|
| 266 |
struct B { operator X(); };
|
| 267 |
B b;
|
| 268 |
+
X x{{b}}; // error: B::operator X() is not a candidate
|
| 269 |
```
|
| 270 |
|
| 271 |
— *end example*]
|
| 272 |
|
| 273 |
For the case where the parameter type is a reference, see
|
|
|
|
| 277 |
sequence models a copy-initialization of the parameter from the argument
|
| 278 |
expression. The implicit conversion sequence is the one required to
|
| 279 |
convert the argument expression to a prvalue of the type of the
|
| 280 |
parameter.
|
| 281 |
|
| 282 |
+
[*Note 3*: When the parameter has a class type, this is a conceptual
|
| 283 |
+
conversion defined for the purposes of [[over]]; the actual
|
| 284 |
initialization is defined in terms of constructors and is not a
|
| 285 |
conversion. — *end note*]
|
| 286 |
|
| 287 |
Any difference in top-level cv-qualification is subsumed by the
|
| 288 |
initialization itself and does not constitute a conversion.
|
|
|
|
| 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 |
+
A derived-to-base conversion has Conversion rank [[over.ics.scs]].
|
| 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
|
| 316 |
parameter type, an implicit conversion sequence cannot be formed.
|
| 317 |
|
| 318 |
+
If there are multiple well-formed implicit conversion sequences
|
| 319 |
+
converting the argument to the parameter type, the implicit conversion
|
| 320 |
+
sequence associated with the parameter is defined to be the unique
|
| 321 |
+
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 5*:
|
| 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*:
|
|
|
|
| 337 |
class B { operator A (); };
|
| 338 |
class C { C (B&); };
|
| 339 |
void f(A) { }
|
| 340 |
void f(C) { }
|
| 341 |
B b;
|
| 342 |
+
f(b); // error: ambiguous because there is a conversion b → C (via constructor)
|
| 343 |
// and an (ambiguous) conversion b → A (via constructor or conversion function)
|
| 344 |
void f(B) { }
|
| 345 |
f(b); // OK, unambiguous
|
| 346 |
```
|
| 347 |
|
|
|
|
| 356 |
The three forms of implicit conversion sequences mentioned above are
|
| 357 |
defined in the following subclauses.
|
| 358 |
|
| 359 |
##### Standard conversion sequences <a id="over.ics.scs">[[over.ics.scs]]</a>
|
| 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 6*: These categories are orthogonal with respect to value
|
| 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 7*: As described in [[conv]], a standard conversion sequence
|
| 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*]
|
|
|
|
| 379 |
|
| 380 |
+
Each conversion in [[over.ics.scs]] also has an associated rank (Exact
|
| 381 |
+
Match, Promotion, or Conversion). These are used to rank standard
|
| 382 |
+
conversion sequences [[over.ics.rank]]. The rank of a conversion
|
| 383 |
+
sequence is determined by considering the rank of each conversion in the
|
| 384 |
+
sequence and the rank of any reference binding [[over.ics.ref]]. If any
|
| 385 |
+
of those has Conversion rank, the sequence has Conversion rank;
|
| 386 |
+
otherwise, if any of those has Promotion rank, the sequence has
|
| 387 |
+
Promotion rank; otherwise, the sequence has Exact Match rank.
|
|
|
|
| 388 |
|
| 389 |
+
**Table: Conversions** <a id="over.ics.scs">[over.ics.scs]</a>
|
| 390 |
|
| 391 |
| Conversion | Category | Rank | Subclause |
|
| 392 |
| ----------------------- | -------- | ---- | ----------------- |
|
| 393 |
| No conversions required | Identity | | |
|
| 394 |
| Integral promotions | | | [[conv.prom]] |
|
| 395 |
| Integral conversions | | | [[conv.integral]] |
|
| 396 |
|
| 397 |
|
| 398 |
##### User-defined conversion sequences <a id="over.ics.user">[[over.ics.user]]</a>
|
| 399 |
|
| 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 required by the argument of the constructor. If the user-defined
|
| 406 |
+
conversion is specified by a conversion function [[class.conv.fct]], the
|
| 407 |
+
initial standard conversion sequence converts the source type to the
|
| 408 |
+
implicit object parameter of the conversion function.
|
|
|
|
| 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,
|
| 414 |
+
the special rules for initialization by user-defined conversion apply
|
| 415 |
+
when selecting the best user-defined conversion for a user-defined
|
| 416 |
+
conversion sequence (see [[over.match.best]] and [[over.best.ics]]).
|
| 417 |
|
| 418 |
If the user-defined conversion is specified by a specialization of a
|
| 419 |
conversion function template, the second standard conversion sequence
|
| 420 |
shall have exact match rank.
|
| 421 |
|
|
|
|
| 431 |
call is matched with the ellipsis parameter specification of the
|
| 432 |
function called (see [[expr.call]]).
|
| 433 |
|
| 434 |
##### Reference binding <a id="over.ics.ref">[[over.ics.ref]]</a>
|
| 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 Conversion [[over.best.ics]].
|
| 441 |
|
| 442 |
[*Example 4*:
|
| 443 |
|
| 444 |
``` cpp
|
| 445 |
struct A {};
|
|
|
|
| 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]], with the second
|
| 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
|
| 463 |
argument expression to the referenced type according to
|
| 464 |
[[over.best.ics]]. Conceptually, this conversion sequence corresponds to
|
| 465 |
copy-initializing a temporary of the referenced type with the argument
|
| 466 |
expression. Any difference in top-level cv-qualification is subsumed by
|
| 467 |
the initialization itself and does not constitute a conversion.
|
| 468 |
|
| 469 |
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 8*: This means, for example, that a candidate function cannot be
|
| 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 |
|
| 481 |
Other restrictions on binding a reference to a particular argument that
|
| 482 |
are not based on the types of the reference and the argument do not
|
| 483 |
+
affect the formation of an implicit conversion sequence, however.
|
| 484 |
|
| 485 |
[*Example 5*: A function with an “lvalue reference to `int`” parameter
|
| 486 |
can be a viable candidate even if the corresponding argument is an `int`
|
| 487 |
bit-field. The formation of implicit conversion sequences treats the
|
| 488 |
`int` bit-field as an `int` lvalue and finds an exact match with the
|
| 489 |
parameter. If the function is selected by overload resolution, the call
|
| 490 |
will nonetheless be ill-formed because of the prohibition on binding a
|
| 491 |
+
non-`const` lvalue reference to a bit-field
|
| 492 |
+
[[dcl.init.ref]]. — *end example*]
|
| 493 |
|
| 494 |
##### List-initialization sequence <a id="over.ics.list">[[over.ics.list]]</a>
|
| 495 |
|
| 496 |
+
When an argument is an initializer list [[dcl.init.list]], it is not an
|
| 497 |
+
expression and special rules apply for converting it to a parameter
|
| 498 |
type.
|
| 499 |
|
| 500 |
+
If the initializer list is a *designated-initializer-list*, a conversion
|
| 501 |
+
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 9*:
|
| 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]].
|
| 513 |
+
|
| 514 |
+
[*Example 6*:
|
| 515 |
+
|
| 516 |
+
``` cpp
|
| 517 |
+
struct A { int x, y; };
|
| 518 |
+
struct B { int y, x; };
|
| 519 |
+
void f(A a, int); // #1
|
| 520 |
+
void f(B b, ...); // #2
|
| 521 |
+
void g(A a); // #3
|
| 522 |
+
void g(B b); // #4
|
| 523 |
+
void h() {
|
| 524 |
+
f({.x = 1, .y = 2}, 0); // OK; calls #1
|
| 525 |
+
f({.y = 2, .x = 1}, 0); // error: selects #1, initialization of a fails
|
| 526 |
+
// due to non-matching member order[dcl.init.list]
|
| 527 |
+
g({.x = 1, .y = 2}); // error: ambiguous between #3 and #4
|
| 528 |
+
}
|
| 529 |
+
```
|
| 530 |
+
|
| 531 |
+
— *end example*]
|
| 532 |
+
|
| 533 |
+
— *end note*]
|
| 534 |
+
|
| 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 [^10] and the
|
| 541 |
initializer list has a single element that is an appropriately-typed
|
| 542 |
+
*string-literal* [[dcl.init.string]], the implicit conversion sequence
|
| 543 |
is the identity conversion.
|
| 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
|
| 549 |
elements, the identity conversion. This conversion can be a user-defined
|
| 550 |
conversion even in the context of a call to an initializer-list
|
| 551 |
constructor.
|
| 552 |
|
| 553 |
+
[*Example 7*:
|
| 554 |
|
| 555 |
``` cpp
|
| 556 |
void f(std::initializer_list<int>);
|
| 557 |
f( {} ); // OK: f(initializer_list<int>) identity conversion
|
| 558 |
f( {1,2,3} ); // OK: f(initializer_list<int>) identity conversion
|
|
|
|
| 574 |
h({ 1, 2, 3 }); // OK: identity conversion
|
| 575 |
```
|
| 576 |
|
| 577 |
— *end example*]
|
| 578 |
|
| 579 |
+
Otherwise, if the parameter type is “array of `N` `X`” or “array of
|
| 580 |
+
unknown bound of `X`”, if there exists an implicit conversion sequence
|
| 581 |
+
from each element of the initializer list (and from `{}` in the former
|
| 582 |
+
case if `N` exceeds the number of elements in the initializer list) to
|
| 583 |
+
`X`, the implicit conversion sequence is the worst such implicit
|
| 584 |
+
conversion sequence.
|
| 585 |
|
| 586 |
Otherwise, if the parameter is a non-aggregate class `X` and overload
|
| 587 |
resolution per [[over.match.list]] chooses a single best constructor
|
| 588 |
`C` of `X` to perform the initialization of an object of type `X` from
|
| 589 |
the argument initializer list:
|
|
|
|
| 600 |
the implicit conversion sequence is the ambiguous conversion sequence.
|
| 601 |
User-defined conversions are allowed for conversion of the initializer
|
| 602 |
list elements to the constructor parameter types except as noted in
|
| 603 |
[[over.best.ics]].
|
| 604 |
|
| 605 |
+
[*Example 8*:
|
| 606 |
|
| 607 |
``` cpp
|
| 608 |
struct A {
|
| 609 |
A(std::initializer_list<int>);
|
| 610 |
};
|
|
|
|
| 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 with the second standard
|
| 643 |
conversion sequence an identity conversion.
|
| 644 |
|
| 645 |
+
[*Example 9*:
|
| 646 |
|
| 647 |
``` cpp
|
| 648 |
struct A {
|
| 649 |
int m1;
|
| 650 |
double m2;
|
|
|
|
| 657 |
|
| 658 |
— *end example*]
|
| 659 |
|
| 660 |
Otherwise, if the parameter is a reference, see [[over.ics.ref]].
|
| 661 |
|
| 662 |
+
[*Note 10*: The rules in this subclause will apply for initializing the
|
| 663 |
underlying temporary for the reference. — *end note*]
|
| 664 |
|
| 665 |
+
[*Example 10*:
|
| 666 |
|
| 667 |
``` cpp
|
| 668 |
struct A {
|
| 669 |
int m1;
|
| 670 |
double m2;
|
|
|
|
| 683 |
Otherwise, if the parameter type is not a class:
|
| 684 |
|
| 685 |
- if the initializer list has one element that is not itself an
|
| 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: same conversion as char to int
|
| 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: identity conversion
|
| 702 |
```
|
| 703 |
|
|
|
|
| 717 |
S2, S1 and S2 are said to be *indistinguishable conversion sequences*.
|
| 718 |
|
| 719 |
When comparing the basic forms of implicit conversion sequences (as
|
| 720 |
defined in [[over.best.ics]])
|
| 721 |
|
| 722 |
+
- a standard conversion sequence [[over.ics.scs]] is a better conversion
|
| 723 |
+
sequence than a user-defined conversion sequence or an ellipsis
|
| 724 |
+
conversion sequence, and
|
| 725 |
+
- a user-defined conversion sequence [[over.ics.user]] is a better
|
| 726 |
+
conversion sequence than an ellipsis conversion sequence
|
| 727 |
+
[[over.ics.ellipsis]].
|
| 728 |
|
| 729 |
Two implicit conversion sequences of the same form are indistinguishable
|
| 730 |
conversion sequences unless one of the following rules applies:
|
| 731 |
|
| 732 |
- List-initialization sequence `L1` is a better conversion sequence than
|
| 733 |
list-initialization sequence `L2` if
|
| 734 |
- `L1` converts to `std::initializer_list<X>` for some `X` and `L2`
|
| 735 |
does not, or, if not that,
|
| 736 |
+
- `L1` and `L2` convert to arrays of the same element type, and either
|
| 737 |
+
the number of elements n₁ initialized by `L1` is less than the
|
| 738 |
+
number of elements n₂ initialized by `L2`, or n₁ = n₂ and `L2`
|
| 739 |
+
converts to an array of unknown bound and `L1` does not,
|
| 740 |
|
| 741 |
even if one of the other rules in this paragraph would otherwise
|
| 742 |
apply.
|
| 743 |
\[*Example 1*:
|
| 744 |
``` cpp
|
|
|
|
| 749 |
void f2(std::pair<const char*, const char*>); // #3
|
| 750 |
void f2(std::initializer_list<std::string>); // #4
|
| 751 |
void g2() { f2({"foo","bar"}); } // chooses #4
|
| 752 |
```
|
| 753 |
|
| 754 |
+
— *end example*]
|
| 755 |
+
\[*Example 2*:
|
| 756 |
+
``` cpp
|
| 757 |
+
void f(int (&&)[] ); // #1
|
| 758 |
+
void f(double (&&)[] ); // #2
|
| 759 |
+
void f(int (&&)[2]); // #3
|
| 760 |
+
|
| 761 |
+
f( {1} ); // Calls #1: Better than #2 due to conversion, better than #3 due to bounds
|
| 762 |
+
f( {1.0} ); // Calls #2: Identity conversion is better than floating-integral conversion
|
| 763 |
+
f( {1.0, 2.0} ); // Calls #2: Identity conversion is better than floating-integral conversion
|
| 764 |
+
f( {1, 2} ); // Calls #3: Converting to array of known bound is better than to unknown bound,
|
| 765 |
+
// and an identity conversion is better than floating-integral conversion
|
| 766 |
+
```
|
| 767 |
+
|
| 768 |
— *end example*]
|
| 769 |
- Standard conversion sequence `S1` is a better conversion sequence than
|
| 770 |
standard conversion sequence `S2` if
|
| 771 |
- `S1` is a proper subsequence of `S2` (comparing the conversion
|
| 772 |
sequences in the canonical form defined by [[over.ics.scs]],
|
|
|
|
| 774 |
sequence is considered to be a subsequence of any non-identity
|
| 775 |
conversion sequence) or, if not that,
|
| 776 |
- the rank of `S1` is better than the rank of `S2`, or `S1` and `S2`
|
| 777 |
have the same rank and are distinguishable by the rules in the
|
| 778 |
paragraph below, or, if not that,
|
| 779 |
+
- `S1` and `S2` include reference bindings [[dcl.init.ref]] and
|
| 780 |
+
neither refers to an implicit object parameter of a non-static
|
| 781 |
+
member function declared without a *ref-qualifier*, and `S1` binds
|
| 782 |
+
an rvalue reference to an rvalue and `S2` binds an lvalue reference
|
| 783 |
+
\[*Example 3*:
|
| 784 |
``` cpp
|
| 785 |
int i;
|
| 786 |
int f1();
|
| 787 |
int&& f2();
|
| 788 |
int g(const int&);
|
|
|
|
| 806 |
a.p(); // calls A::p()&
|
| 807 |
```
|
| 808 |
|
| 809 |
— *end example*]
|
| 810 |
or, if not that,
|
| 811 |
+
- `S1` and `S2` include reference bindings [[dcl.init.ref]] and `S1`
|
| 812 |
binds an lvalue reference to a function lvalue and `S2` binds an
|
| 813 |
rvalue reference to a function lvalue
|
| 814 |
+
\[*Example 4*:
|
| 815 |
``` cpp
|
| 816 |
int f(void(&)()); // #1
|
| 817 |
int f(void(&&)()); // #2
|
| 818 |
void g();
|
| 819 |
int i1 = f(g); // calls #1
|
| 820 |
```
|
| 821 |
|
| 822 |
— *end example*]
|
| 823 |
or, if not that,
|
| 824 |
+
- `S1` and `S2` differ only in their qualification conversion
|
| 825 |
+
[[conv.qual]] and yield similar types `T1` and `T2`, respectively,
|
| 826 |
+
where `T1` can be converted to `T2` by a qualification conversion.
|
| 827 |
+
\[*Example 5*:
|
|
|
|
|
|
|
| 828 |
``` cpp
|
| 829 |
int f(const volatile int *);
|
| 830 |
int f(const int *);
|
| 831 |
int i;
|
| 832 |
int j = f(&i); // calls f(const int*)
|
| 833 |
```
|
| 834 |
|
| 835 |
— *end example*]
|
| 836 |
or, if not that,
|
| 837 |
- `S1`
|
| 838 |
+
and `S2` include reference bindings [[dcl.init.ref]], and the types
|
| 839 |
to which the references refer are the same type except for top-level
|
| 840 |
cv-qualifiers, and the type to which the reference initialized by
|
| 841 |
`S2` refers is more cv-qualified than the type to which the
|
| 842 |
reference initialized by `S1` refers.
|
| 843 |
+
\[*Example 6*:
|
| 844 |
``` cpp
|
| 845 |
int f(const int &);
|
| 846 |
int f(int &);
|
| 847 |
int g(const int &);
|
| 848 |
int g(int);
|
|
|
|
| 866 |
than another user-defined conversion sequence `U2` if they contain the
|
| 867 |
same user-defined conversion function or constructor or they
|
| 868 |
initialize the same class in an aggregate initialization and in either
|
| 869 |
case the second standard conversion sequence of `U1` is better than
|
| 870 |
the second standard conversion sequence of `U2`.
|
| 871 |
+
\[*Example 7*:
|
| 872 |
``` cpp
|
| 873 |
struct A {
|
| 874 |
operator short();
|
| 875 |
} a;
|
| 876 |
int f(int);
|
|
|
|
| 884 |
Standard conversion sequences are ordered by their ranks: an Exact Match
|
| 885 |
is a better conversion than a Promotion, which is a better conversion
|
| 886 |
than a Conversion. Two conversion sequences with the same rank are
|
| 887 |
indistinguishable unless one of the following rules applies:
|
| 888 |
|
| 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
|
|
|
|
| 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 8*:
|
| 903 |
``` cpp
|
| 904 |
struct A {};
|
| 905 |
struct B : public A {};
|
| 906 |
struct C : public B {};
|
| 907 |
C* pc;
|