tmp/tmpvkhzcvpy/{from.md → to.md}
RENAMED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
#### Implicit conversion sequences <a id="over.best.ics">[[over.best.ics]]</a>
|
| 2 |
|
|
|
|
|
|
|
| 3 |
An *implicit conversion sequence* is a sequence of conversions used to
|
| 4 |
convert an argument in a function call to the type of the corresponding
|
| 5 |
parameter of the function being called. The sequence of conversions is
|
| 6 |
an implicit conversion as defined in [[conv]], which means it is
|
| 7 |
governed by the rules for initialization of an object or reference by a
|
| 8 |
-
single expression
|
| 9 |
|
| 10 |
Implicit conversion sequences are concerned only with the type,
|
| 11 |
cv-qualification, and value category of the argument and how these are
|
| 12 |
converted to match the corresponding properties of the parameter.
|
| 13 |
|
|
@@ -27,11 +29,11 @@ forms:
|
|
| 27 |
- an ellipsis conversion sequence [[over.ics.ellipsis]].
|
| 28 |
|
| 29 |
However, if the target is
|
| 30 |
|
| 31 |
- the first parameter of a constructor or
|
| 32 |
-
- the
|
| 33 |
|
| 34 |
and the constructor or user-defined conversion function is a candidate
|
| 35 |
by
|
| 36 |
|
| 37 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
|
@@ -88,22 +90,29 @@ case is the identity sequence; it contains no “conversion” from
|
|
| 88 |
|
| 89 |
When the parameter has a class type and the argument expression has the
|
| 90 |
same type, the implicit conversion sequence is an identity conversion.
|
| 91 |
When the parameter has a class type and the argument expression has a
|
| 92 |
derived class type, the implicit conversion sequence is a
|
| 93 |
-
derived-to-base conversion from the derived class to the base class.
|
|
|
|
| 94 |
|
| 95 |
[*Note 4*: There is no such standard conversion; this derived-to-base
|
| 96 |
conversion exists only in the description of implicit conversion
|
| 97 |
sequences. — *end note*]
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
In all contexts, when converting to the implicit object parameter or
|
| 102 |
when converting to the left operand of an assignment operation only
|
| 103 |
standard conversion sequences are allowed.
|
| 104 |
|
|
|
|
|
|
|
|
|
|
| 105 |
If no conversions are required to match an argument to a parameter type,
|
| 106 |
the implicit conversion sequence is the standard conversion sequence
|
| 107 |
consisting of the identity conversion [[over.ics.scs]].
|
| 108 |
|
| 109 |
If no sequence of conversions can be found to convert an argument to a
|
|
@@ -116,11 +125,11 @@ conversion sequence designated the *ambiguous conversion sequence*. For
|
|
| 116 |
the purpose of ranking implicit conversion sequences as described in
|
| 117 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 118 |
user-defined conversion sequence that is indistinguishable from any
|
| 119 |
other user-defined conversion sequence.
|
| 120 |
|
| 121 |
-
[*Note
|
| 122 |
|
| 123 |
This rule prevents a function from becoming non-viable because of an
|
| 124 |
ambiguous conversion sequence for one of its parameters.
|
| 125 |
|
| 126 |
[*Example 3*:
|
|
@@ -154,19 +163,19 @@ defined in the following subclauses.
|
|
| 154 |
|
| 155 |
summarizes the conversions defined in [[conv]] and partitions them into
|
| 156 |
four disjoint categories: Lvalue Transformation, Qualification
|
| 157 |
Adjustment, Promotion, and Conversion.
|
| 158 |
|
| 159 |
-
[*Note
|
| 160 |
category, cv-qualification, and data representation: the Lvalue
|
| 161 |
Transformations do not change the cv-qualification or data
|
| 162 |
representation of the type; the Qualification Adjustments do not change
|
| 163 |
the value category or data representation of the type; and the
|
| 164 |
Promotions and Conversions do not change the value category or
|
| 165 |
cv-qualification of the type. — *end note*]
|
| 166 |
|
| 167 |
-
[*Note
|
| 168 |
either is the Identity conversion by itself (that is, no conversion) or
|
| 169 |
consists of one to three conversions from the other four categories. If
|
| 170 |
there are two or more conversions in the sequence, the conversions are
|
| 171 |
applied in the canonical order: **Lvalue Transformation**, **Promotion**
|
| 172 |
or **Conversion**, **Qualification Adjustment**. — *end note*]
|
|
@@ -194,14 +203,14 @@ Promotion rank; otherwise, the sequence has Exact Match rank.
|
|
| 194 |
A *user-defined conversion sequence* consists of an initial standard
|
| 195 |
conversion sequence followed by a user-defined conversion [[class.conv]]
|
| 196 |
followed by a second standard conversion sequence. If the user-defined
|
| 197 |
conversion is specified by a constructor [[class.conv.ctor]], the
|
| 198 |
initial standard conversion sequence converts the source type to the
|
| 199 |
-
type
|
| 200 |
conversion is specified by a conversion function [[class.conv.fct]], the
|
| 201 |
initial standard conversion sequence converts the source type to the
|
| 202 |
-
|
| 203 |
|
| 204 |
The second standard conversion sequence converts the result of the
|
| 205 |
user-defined conversion to the target type for the sequence; any
|
| 206 |
reference binding is included in the second standard conversion
|
| 207 |
sequence. Since an implicit conversion sequence is an initialization,
|
|
@@ -229,11 +238,11 @@ function called (see [[expr.call]]).
|
|
| 229 |
|
| 230 |
When a parameter of reference type binds directly [[dcl.init.ref]] to an
|
| 231 |
argument expression, the implicit conversion sequence is the identity
|
| 232 |
conversion, unless the argument expression has a type that is a derived
|
| 233 |
class of the parameter type, in which case the implicit conversion
|
| 234 |
-
sequence is a derived-to-base
|
| 235 |
|
| 236 |
[*Example 4*:
|
| 237 |
|
| 238 |
``` cpp
|
| 239 |
struct A {};
|
|
@@ -245,12 +254,12 @@ int i = f(b); // calls f(B&), an exact match, rather than f(A&), a convers
|
|
| 245 |
|
| 246 |
— *end example*]
|
| 247 |
|
| 248 |
If the parameter binds directly to the result of applying a conversion
|
| 249 |
function to the argument expression, the implicit conversion sequence is
|
| 250 |
-
a user-defined conversion sequence [[over.ics.user]]
|
| 251 |
-
standard conversion sequence either an identity conversion or, if the
|
| 252 |
conversion function returns an entity of a type that is a derived class
|
| 253 |
of the parameter type, a derived-to-base conversion.
|
| 254 |
|
| 255 |
When a parameter of reference type is not bound directly to an argument
|
| 256 |
expression, the conversion sequence is the one required to convert the
|
|
@@ -264,11 +273,11 @@ Except for an implicit object parameter, for which see
|
|
| 264 |
[[over.match.funcs]], an implicit conversion sequence cannot be formed
|
| 265 |
if it requires binding an lvalue reference other than a reference to a
|
| 266 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 267 |
an lvalue other than a function lvalue.
|
| 268 |
|
| 269 |
-
[*Note
|
| 270 |
a viable function if it has a non-`const` lvalue reference parameter
|
| 271 |
(other than the implicit object parameter) and the corresponding
|
| 272 |
argument would require a temporary to be created to initialize the
|
| 273 |
lvalue reference (see [[dcl.init.ref]]). — *end note*]
|
| 274 |
|
|
@@ -296,11 +305,11 @@ is only possible if the parameter has an aggregate type that can be
|
|
| 296 |
initialized from the initializer list according to the rules for
|
| 297 |
aggregate initialization [[dcl.init.aggr]], in which case the implicit
|
| 298 |
conversion sequence is a user-defined conversion sequence whose second
|
| 299 |
standard conversion sequence is an identity conversion.
|
| 300 |
|
| 301 |
-
[*Note
|
| 302 |
|
| 303 |
Aggregate initialization does not require that the members are declared
|
| 304 |
in designation order. If, after overload resolution, the order does not
|
| 305 |
match for the selected overload, the initialization of the parameter
|
| 306 |
will be ill-formed [[dcl.init.list]].
|
|
@@ -329,14 +338,15 @@ void h() {
|
|
| 329 |
Otherwise, if the parameter type is an aggregate class `X` and the
|
| 330 |
initializer list has a single element of type cv `U`, where `U` is `X`
|
| 331 |
or a class derived from `X`, the implicit conversion sequence is the one
|
| 332 |
required to convert the element to the parameter type.
|
| 333 |
|
| 334 |
-
Otherwise, if the parameter type is a character array
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
|
|
|
| 338 |
|
| 339 |
Otherwise, if the parameter type is `std::initializer_list<X>` and all
|
| 340 |
the elements of the initializer list can be implicitly converted to `X`,
|
| 341 |
the implicit conversion sequence is the worst conversion necessary to
|
| 342 |
convert an element of the list to `X`, or if the initializer list has no
|
|
@@ -346,13 +356,13 @@ constructor.
|
|
| 346 |
|
| 347 |
[*Example 7*:
|
| 348 |
|
| 349 |
``` cpp
|
| 350 |
void f(std::initializer_list<int>);
|
| 351 |
-
f( {} ); // OK
|
| 352 |
-
f( {1,2,3} ); // OK
|
| 353 |
-
f( {'a','b'} ); // OK
|
| 354 |
f( {1.0} ); // error: narrowing
|
| 355 |
|
| 356 |
struct A {
|
| 357 |
A(std::initializer_list<double>); // #1
|
| 358 |
A(std::initializer_list<complex<double>>); // #2
|
|
@@ -363,11 +373,11 @@ A a{ 1.0,2.0 }; // OK, uses #1
|
|
| 363 |
void g(A);
|
| 364 |
g({ "foo", "bar" }); // OK, uses #3
|
| 365 |
|
| 366 |
typedef int IA[3];
|
| 367 |
void h(const IA&);
|
| 368 |
-
h({ 1, 2, 3 }); // OK
|
| 369 |
```
|
| 370 |
|
| 371 |
— *end example*]
|
| 372 |
|
| 373 |
Otherwise, if the parameter type is “array of `N` `X`” or “array of
|
|
@@ -385,11 +395,11 @@ the argument initializer list:
|
|
| 385 |
- If `C` is not an initializer-list constructor and the initializer list
|
| 386 |
has a single element of type cv `U`, where `U` is `X` or a class
|
| 387 |
derived from `X`, the implicit conversion sequence has Exact Match
|
| 388 |
rank if `U` is `X`, or Conversion rank if `U` is derived from `X`.
|
| 389 |
- Otherwise, the implicit conversion sequence is a user-defined
|
| 390 |
-
conversion sequence
|
| 391 |
identity conversion.
|
| 392 |
|
| 393 |
If multiple constructors are viable but none is better than the others,
|
| 394 |
the implicit conversion sequence is the ambiguous conversion sequence.
|
| 395 |
User-defined conversions are allowed for conversion of the initializer
|
|
@@ -401,61 +411,61 @@ list elements to the constructor parameter types except as noted in
|
|
| 401 |
``` cpp
|
| 402 |
struct A {
|
| 403 |
A(std::initializer_list<int>);
|
| 404 |
};
|
| 405 |
void f(A);
|
| 406 |
-
f( {'a', 'b'} ); // OK
|
| 407 |
|
| 408 |
struct B {
|
| 409 |
B(int, double);
|
| 410 |
};
|
| 411 |
void g(B);
|
| 412 |
-
g( {'a', 'b'} ); // OK
|
| 413 |
g( {1.0, 1.0} ); // error: narrowing
|
| 414 |
|
| 415 |
void f(B);
|
| 416 |
f( {'a', 'b'} ); // error: ambiguous f(A) or f(B)
|
| 417 |
|
| 418 |
struct C {
|
| 419 |
C(std::string);
|
| 420 |
};
|
| 421 |
void h(C);
|
| 422 |
-
h({"foo"}); // OK
|
| 423 |
|
| 424 |
struct D {
|
| 425 |
D(A, C);
|
| 426 |
};
|
| 427 |
void i(D);
|
| 428 |
-
i({ {1,2}, {"bar"} }); // OK
|
| 429 |
```
|
| 430 |
|
| 431 |
— *end example*]
|
| 432 |
|
| 433 |
Otherwise, if the parameter has an aggregate type which can be
|
| 434 |
initialized from the initializer list according to the rules for
|
| 435 |
aggregate initialization [[dcl.init.aggr]], the implicit conversion
|
| 436 |
-
sequence is a user-defined conversion sequence
|
| 437 |
-
conversion sequence an identity conversion.
|
| 438 |
|
| 439 |
[*Example 9*:
|
| 440 |
|
| 441 |
``` cpp
|
| 442 |
struct A {
|
| 443 |
int m1;
|
| 444 |
double m2;
|
| 445 |
};
|
| 446 |
|
| 447 |
void f(A);
|
| 448 |
-
f( {'a', 'b'} ); // OK
|
| 449 |
f( {1.0} ); // error: narrowing
|
| 450 |
```
|
| 451 |
|
| 452 |
— *end example*]
|
| 453 |
|
| 454 |
Otherwise, if the parameter is a reference, see [[over.ics.ref]].
|
| 455 |
|
| 456 |
-
[*Note
|
| 457 |
underlying temporary for the reference. — *end note*]
|
| 458 |
|
| 459 |
[*Example 10*:
|
| 460 |
|
| 461 |
``` cpp
|
|
@@ -463,11 +473,11 @@ struct A {
|
|
| 463 |
int m1;
|
| 464 |
double m2;
|
| 465 |
};
|
| 466 |
|
| 467 |
void f(const A&);
|
| 468 |
-
f( {'a', 'b'} ); // OK
|
| 469 |
f( {1.0} ); // error: narrowing
|
| 470 |
|
| 471 |
void g(const double &);
|
| 472 |
g({1}); // same conversion as int to double
|
| 473 |
```
|
|
@@ -480,21 +490,21 @@ Otherwise, if the parameter type is not a class:
|
|
| 480 |
initializer list, the implicit conversion sequence is the one required
|
| 481 |
to convert the element to the parameter type;
|
| 482 |
\[*Example 11*:
|
| 483 |
``` cpp
|
| 484 |
void f(int);
|
| 485 |
-
f( {'a'} ); // OK
|
| 486 |
f( {1.0} ); // error: narrowing
|
| 487 |
```
|
| 488 |
|
| 489 |
— *end example*]
|
| 490 |
- if the initializer list has no elements, the implicit conversion
|
| 491 |
sequence is the identity conversion.
|
| 492 |
\[*Example 12*:
|
| 493 |
``` cpp
|
| 494 |
void f(int);
|
| 495 |
-
f( { } ); // OK
|
| 496 |
```
|
| 497 |
|
| 498 |
— *end example*]
|
| 499 |
|
| 500 |
In all cases other than those enumerated above, no conversion is
|
|
|
|
| 1 |
#### Implicit conversion sequences <a id="over.best.ics">[[over.best.ics]]</a>
|
| 2 |
|
| 3 |
+
##### General <a id="over.best.ics.general">[[over.best.ics.general]]</a>
|
| 4 |
+
|
| 5 |
An *implicit conversion sequence* is a sequence of conversions used to
|
| 6 |
convert an argument in a function call to the type of the corresponding
|
| 7 |
parameter of the function being called. The sequence of conversions is
|
| 8 |
an implicit conversion as defined in [[conv]], which means it is
|
| 9 |
governed by the rules for initialization of an object or reference by a
|
| 10 |
+
single expression [[dcl.init]], [[dcl.init.ref]].
|
| 11 |
|
| 12 |
Implicit conversion sequences are concerned only with the type,
|
| 13 |
cv-qualification, and value category of the argument and how these are
|
| 14 |
converted to match the corresponding properties of the parameter.
|
| 15 |
|
|
|
|
| 29 |
- an ellipsis conversion sequence [[over.ics.ellipsis]].
|
| 30 |
|
| 31 |
However, if the target is
|
| 32 |
|
| 33 |
- the first parameter of a constructor or
|
| 34 |
+
- the object parameter of a user-defined conversion function
|
| 35 |
|
| 36 |
and the constructor or user-defined conversion function is a candidate
|
| 37 |
by
|
| 38 |
|
| 39 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
|
|
|
| 90 |
|
| 91 |
When the parameter has a class type and the argument expression has the
|
| 92 |
same type, the implicit conversion sequence is an identity conversion.
|
| 93 |
When the parameter has a class type and the argument expression has a
|
| 94 |
derived class type, the implicit conversion sequence is a
|
| 95 |
+
derived-to-base conversion from the derived class to the base class. A
|
| 96 |
+
derived-to-base conversion has Conversion rank [[over.ics.scs]].
|
| 97 |
|
| 98 |
[*Note 4*: There is no such standard conversion; this derived-to-base
|
| 99 |
conversion exists only in the description of implicit conversion
|
| 100 |
sequences. — *end note*]
|
| 101 |
|
| 102 |
+
When the parameter is the implicit object parameter of a static member
|
| 103 |
+
function, the implicit conversion sequence is a standard conversion
|
| 104 |
+
sequence that is neither better nor worse than any other standard
|
| 105 |
+
conversion sequence.
|
| 106 |
|
| 107 |
In all contexts, when converting to the implicit object parameter or
|
| 108 |
when converting to the left operand of an assignment operation only
|
| 109 |
standard conversion sequences are allowed.
|
| 110 |
|
| 111 |
+
[*Note 5*: When converting to the explicit object parameter, if any,
|
| 112 |
+
user-defined conversion sequences are allowed. — *end note*]
|
| 113 |
+
|
| 114 |
If no conversions are required to match an argument to a parameter type,
|
| 115 |
the implicit conversion sequence is the standard conversion sequence
|
| 116 |
consisting of the identity conversion [[over.ics.scs]].
|
| 117 |
|
| 118 |
If no sequence of conversions can be found to convert an argument to a
|
|
|
|
| 125 |
the purpose of ranking implicit conversion sequences as described in
|
| 126 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 127 |
user-defined conversion sequence that is indistinguishable from any
|
| 128 |
other user-defined conversion sequence.
|
| 129 |
|
| 130 |
+
[*Note 6*:
|
| 131 |
|
| 132 |
This rule prevents a function from becoming non-viable because of an
|
| 133 |
ambiguous conversion sequence for one of its parameters.
|
| 134 |
|
| 135 |
[*Example 3*:
|
|
|
|
| 163 |
|
| 164 |
summarizes the conversions defined in [[conv]] and partitions them into
|
| 165 |
four disjoint categories: Lvalue Transformation, Qualification
|
| 166 |
Adjustment, Promotion, and Conversion.
|
| 167 |
|
| 168 |
+
[*Note 7*: These categories are orthogonal with respect to value
|
| 169 |
category, cv-qualification, and data representation: the Lvalue
|
| 170 |
Transformations do not change the cv-qualification or data
|
| 171 |
representation of the type; the Qualification Adjustments do not change
|
| 172 |
the value category or data representation of the type; and the
|
| 173 |
Promotions and Conversions do not change the value category or
|
| 174 |
cv-qualification of the type. — *end note*]
|
| 175 |
|
| 176 |
+
[*Note 8*: As described in [[conv]], a standard conversion sequence
|
| 177 |
either is the Identity conversion by itself (that is, no conversion) or
|
| 178 |
consists of one to three conversions from the other four categories. If
|
| 179 |
there are two or more conversions in the sequence, the conversions are
|
| 180 |
applied in the canonical order: **Lvalue Transformation**, **Promotion**
|
| 181 |
or **Conversion**, **Qualification Adjustment**. — *end note*]
|
|
|
|
| 203 |
A *user-defined conversion sequence* consists of an initial standard
|
| 204 |
conversion sequence followed by a user-defined conversion [[class.conv]]
|
| 205 |
followed by a second standard conversion sequence. If the user-defined
|
| 206 |
conversion is specified by a constructor [[class.conv.ctor]], the
|
| 207 |
initial standard conversion sequence converts the source type to the
|
| 208 |
+
type of the first parameter of that constructor. If the user-defined
|
| 209 |
conversion is specified by a conversion function [[class.conv.fct]], the
|
| 210 |
initial standard conversion sequence converts the source type to the
|
| 211 |
+
type of the object parameter of that conversion function.
|
| 212 |
|
| 213 |
The second standard conversion sequence converts the result of the
|
| 214 |
user-defined conversion to the target type for the sequence; any
|
| 215 |
reference binding is included in the second standard conversion
|
| 216 |
sequence. Since an implicit conversion sequence is an initialization,
|
|
|
|
| 238 |
|
| 239 |
When a parameter of reference type binds directly [[dcl.init.ref]] to an
|
| 240 |
argument expression, the implicit conversion sequence is the identity
|
| 241 |
conversion, unless the argument expression has a type that is a derived
|
| 242 |
class of the parameter type, in which case the implicit conversion
|
| 243 |
+
sequence is a derived-to-base conversion [[over.best.ics]].
|
| 244 |
|
| 245 |
[*Example 4*:
|
| 246 |
|
| 247 |
``` cpp
|
| 248 |
struct A {};
|
|
|
|
| 254 |
|
| 255 |
— *end example*]
|
| 256 |
|
| 257 |
If the parameter binds directly to the result of applying a conversion
|
| 258 |
function to the argument expression, the implicit conversion sequence is
|
| 259 |
+
a user-defined conversion sequence [[over.ics.user]] whose second
|
| 260 |
+
standard conversion sequence is either an identity conversion or, if the
|
| 261 |
conversion function returns an entity of a type that is a derived class
|
| 262 |
of the parameter type, a derived-to-base conversion.
|
| 263 |
|
| 264 |
When a parameter of reference type is not bound directly to an argument
|
| 265 |
expression, the conversion sequence is the one required to convert the
|
|
|
|
| 273 |
[[over.match.funcs]], an implicit conversion sequence cannot be formed
|
| 274 |
if it requires binding an lvalue reference other than a reference to a
|
| 275 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 276 |
an lvalue other than a function lvalue.
|
| 277 |
|
| 278 |
+
[*Note 9*: This means, for example, that a candidate function cannot be
|
| 279 |
a viable function if it has a non-`const` lvalue reference parameter
|
| 280 |
(other than the implicit object parameter) and the corresponding
|
| 281 |
argument would require a temporary to be created to initialize the
|
| 282 |
lvalue reference (see [[dcl.init.ref]]). — *end note*]
|
| 283 |
|
|
|
|
| 305 |
initialized from the initializer list according to the rules for
|
| 306 |
aggregate initialization [[dcl.init.aggr]], in which case the implicit
|
| 307 |
conversion sequence is a user-defined conversion sequence whose second
|
| 308 |
standard conversion sequence is an identity conversion.
|
| 309 |
|
| 310 |
+
[*Note 10*:
|
| 311 |
|
| 312 |
Aggregate initialization does not require that the members are declared
|
| 313 |
in designation order. If, after overload resolution, the order does not
|
| 314 |
match for the selected overload, the initialization of the parameter
|
| 315 |
will be ill-formed [[dcl.init.list]].
|
|
|
|
| 338 |
Otherwise, if the parameter type is an aggregate class `X` and the
|
| 339 |
initializer list has a single element of type cv `U`, where `U` is `X`
|
| 340 |
or a class derived from `X`, the implicit conversion sequence is the one
|
| 341 |
required to convert the element to the parameter type.
|
| 342 |
|
| 343 |
+
Otherwise, if the parameter type is a character array[^8]
|
| 344 |
+
|
| 345 |
+
and the initializer list has a single element that is an
|
| 346 |
+
appropriately-typed *string-literal* [[dcl.init.string]], the implicit
|
| 347 |
+
conversion sequence is the identity conversion.
|
| 348 |
|
| 349 |
Otherwise, if the parameter type is `std::initializer_list<X>` and all
|
| 350 |
the elements of the initializer list can be implicitly converted to `X`,
|
| 351 |
the implicit conversion sequence is the worst conversion necessary to
|
| 352 |
convert an element of the list to `X`, or if the initializer list has no
|
|
|
|
| 356 |
|
| 357 |
[*Example 7*:
|
| 358 |
|
| 359 |
``` cpp
|
| 360 |
void f(std::initializer_list<int>);
|
| 361 |
+
f( {} ); // OK, f(initializer_list<int>) identity conversion
|
| 362 |
+
f( {1,2,3} ); // OK, f(initializer_list<int>) identity conversion
|
| 363 |
+
f( {'a','b'} ); // OK, f(initializer_list<int>) integral promotion
|
| 364 |
f( {1.0} ); // error: narrowing
|
| 365 |
|
| 366 |
struct A {
|
| 367 |
A(std::initializer_list<double>); // #1
|
| 368 |
A(std::initializer_list<complex<double>>); // #2
|
|
|
|
| 373 |
void g(A);
|
| 374 |
g({ "foo", "bar" }); // OK, uses #3
|
| 375 |
|
| 376 |
typedef int IA[3];
|
| 377 |
void h(const IA&);
|
| 378 |
+
h({ 1, 2, 3 }); // OK, identity conversion
|
| 379 |
```
|
| 380 |
|
| 381 |
— *end example*]
|
| 382 |
|
| 383 |
Otherwise, if the parameter type is “array of `N` `X`” or “array of
|
|
|
|
| 395 |
- If `C` is not an initializer-list constructor and the initializer list
|
| 396 |
has a single element of type cv `U`, where `U` is `X` or a class
|
| 397 |
derived from `X`, the implicit conversion sequence has Exact Match
|
| 398 |
rank if `U` is `X`, or Conversion rank if `U` is derived from `X`.
|
| 399 |
- Otherwise, the implicit conversion sequence is a user-defined
|
| 400 |
+
conversion sequence whose second standard conversion sequence is an
|
| 401 |
identity conversion.
|
| 402 |
|
| 403 |
If multiple constructors are viable but none is better than the others,
|
| 404 |
the implicit conversion sequence is the ambiguous conversion sequence.
|
| 405 |
User-defined conversions are allowed for conversion of the initializer
|
|
|
|
| 411 |
``` cpp
|
| 412 |
struct A {
|
| 413 |
A(std::initializer_list<int>);
|
| 414 |
};
|
| 415 |
void f(A);
|
| 416 |
+
f( {'a', 'b'} ); // OK, f(A(std::initializer_list<int>)) user-defined conversion
|
| 417 |
|
| 418 |
struct B {
|
| 419 |
B(int, double);
|
| 420 |
};
|
| 421 |
void g(B);
|
| 422 |
+
g( {'a', 'b'} ); // OK, g(B(int, double)) user-defined conversion
|
| 423 |
g( {1.0, 1.0} ); // error: narrowing
|
| 424 |
|
| 425 |
void f(B);
|
| 426 |
f( {'a', 'b'} ); // error: ambiguous f(A) or f(B)
|
| 427 |
|
| 428 |
struct C {
|
| 429 |
C(std::string);
|
| 430 |
};
|
| 431 |
void h(C);
|
| 432 |
+
h({"foo"}); // OK, h(C(std::string("foo")))
|
| 433 |
|
| 434 |
struct D {
|
| 435 |
D(A, C);
|
| 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 |
— *end example*]
|
| 442 |
|
| 443 |
Otherwise, if the parameter has an aggregate type which can be
|
| 444 |
initialized from the initializer list according to the rules for
|
| 445 |
aggregate initialization [[dcl.init.aggr]], the implicit conversion
|
| 446 |
+
sequence is a user-defined conversion sequence whose second standard
|
| 447 |
+
conversion sequence is an identity conversion.
|
| 448 |
|
| 449 |
[*Example 9*:
|
| 450 |
|
| 451 |
``` cpp
|
| 452 |
struct A {
|
| 453 |
int m1;
|
| 454 |
double m2;
|
| 455 |
};
|
| 456 |
|
| 457 |
void f(A);
|
| 458 |
+
f( {'a', 'b'} ); // OK, f(A(int,double)) user-defined conversion
|
| 459 |
f( {1.0} ); // error: narrowing
|
| 460 |
```
|
| 461 |
|
| 462 |
— *end example*]
|
| 463 |
|
| 464 |
Otherwise, if the parameter is a reference, see [[over.ics.ref]].
|
| 465 |
|
| 466 |
+
[*Note 11*: The rules in this subclause will apply for initializing the
|
| 467 |
underlying temporary for the reference. — *end note*]
|
| 468 |
|
| 469 |
[*Example 10*:
|
| 470 |
|
| 471 |
``` cpp
|
|
|
|
| 473 |
int m1;
|
| 474 |
double m2;
|
| 475 |
};
|
| 476 |
|
| 477 |
void f(const A&);
|
| 478 |
+
f( {'a', 'b'} ); // OK, f(A(int,double)) user-defined conversion
|
| 479 |
f( {1.0} ); // error: narrowing
|
| 480 |
|
| 481 |
void g(const double &);
|
| 482 |
g({1}); // same conversion as int to double
|
| 483 |
```
|
|
|
|
| 490 |
initializer list, the implicit conversion sequence is the one required
|
| 491 |
to convert the element to the parameter type;
|
| 492 |
\[*Example 11*:
|
| 493 |
``` cpp
|
| 494 |
void f(int);
|
| 495 |
+
f( {'a'} ); // OK, same conversion as char to int
|
| 496 |
f( {1.0} ); // error: narrowing
|
| 497 |
```
|
| 498 |
|
| 499 |
— *end example*]
|
| 500 |
- if the initializer list has no elements, the implicit conversion
|
| 501 |
sequence is the identity conversion.
|
| 502 |
\[*Example 12*:
|
| 503 |
``` cpp
|
| 504 |
void f(int);
|
| 505 |
+
f( { } ); // OK, identity conversion
|
| 506 |
```
|
| 507 |
|
| 508 |
— *end example*]
|
| 509 |
|
| 510 |
In all cases other than those enumerated above, no conversion is
|