- tmp/tmp3jf91ws0/{from.md → to.md} +173 -86
tmp/tmp3jf91ws0/{from.md → to.md}
RENAMED
|
@@ -8,16 +8,16 @@ governed by the rules for initialization of an object or reference by a
|
|
| 8 |
single expression ([[dcl.init]], [[dcl.init.ref]]).
|
| 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. Other
|
| 13 |
-
properties, such as the lifetime, storage class, alignment,
|
| 14 |
-
accessibility of the argument
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
argument
|
| 18 |
-
analysis.
|
| 19 |
|
| 20 |
A well-formed implicit conversion sequence is one of the following
|
| 21 |
forms:
|
| 22 |
|
| 23 |
- a *standard conversion sequence* ([[over.ics.scs]]),
|
|
@@ -35,17 +35,21 @@ by
|
|
| 35 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
| 36 |
step of a class copy-initialization,
|
| 37 |
- [[over.match.copy]], [[over.match.conv]], or [[over.match.ref]] (in
|
| 38 |
all cases), or
|
| 39 |
- the second phase of [[over.match.list]] when the initializer list has
|
| 40 |
-
exactly one element
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
user-defined conversion sequences are not considered.
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
``` cpp
|
| 49 |
struct Y { Y(int); };
|
| 50 |
struct A { operator int(); };
|
| 51 |
Y y1 = A(); // error: A::operator int() is not a candidate
|
|
@@ -54,83 +58,120 @@ struct Y { Y(int); };
|
|
| 54 |
struct B { operator X(); };
|
| 55 |
B b;
|
| 56 |
X x({b}); // error: B::operator X() is not a candidate
|
| 57 |
```
|
| 58 |
|
|
|
|
|
|
|
| 59 |
For the case where the parameter type is a reference, see
|
| 60 |
[[over.ics.ref]].
|
| 61 |
|
| 62 |
When the parameter type is not a reference, the implicit conversion
|
| 63 |
sequence models a copy-initialization of the parameter from the argument
|
| 64 |
expression. The implicit conversion sequence is the one required to
|
| 65 |
convert the argument expression to a prvalue of the type of the
|
| 66 |
-
parameter.
|
|
|
|
|
|
|
| 67 |
conversion defined for the purposes of Clause [[over]]; the actual
|
| 68 |
initialization is defined in terms of constructors and is not a
|
| 69 |
-
conversion.
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
When the parameter has a class type and the argument expression has the
|
| 75 |
same type, the implicit conversion sequence is an identity conversion.
|
| 76 |
When the parameter has a class type and the argument expression has a
|
| 77 |
derived class type, the implicit conversion sequence is a
|
| 78 |
derived-to-base Conversion from the derived class to the base class.
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
In all contexts, when converting to the implicit object parameter or
|
| 84 |
when converting to the left operand of an assignment operation only
|
| 85 |
-
standard conversion sequences
|
| 86 |
-
result are allowed.
|
| 87 |
|
| 88 |
If no conversions are required to match an argument to a parameter type,
|
| 89 |
the implicit conversion sequence is the standard conversion sequence
|
| 90 |
consisting of the identity conversion ([[over.ics.scs]]).
|
| 91 |
|
| 92 |
If no sequence of conversions can be found to convert an argument to a
|
| 93 |
-
parameter type
|
| 94 |
-
conversion sequence cannot be formed.
|
| 95 |
|
| 96 |
If several different sequences of conversions exist that each convert
|
| 97 |
the argument to the parameter type, the implicit conversion sequence
|
| 98 |
associated with the parameter is defined to be the unique conversion
|
| 99 |
sequence designated the *ambiguous conversion sequence*. For the purpose
|
| 100 |
of ranking implicit conversion sequences as described in
|
| 101 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 102 |
-
user-defined sequence that is indistinguishable from any
|
| 103 |
-
user-defined conversion sequence
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
The three forms of implicit conversion sequences mentioned above are
|
| 109 |
defined in the following subclauses.
|
| 110 |
|
| 111 |
##### Standard conversion sequences <a id="over.ics.scs">[[over.ics.scs]]</a>
|
| 112 |
|
| 113 |
Table [[tab:over.conversions]] summarizes the conversions defined in
|
| 114 |
Clause [[conv]] and partitions them into four disjoint categories:
|
| 115 |
Lvalue Transformation, Qualification Adjustment, Promotion, and
|
| 116 |
-
Conversion.
|
|
|
|
|
|
|
| 117 |
category, cv-qualification, and data representation: the Lvalue
|
| 118 |
Transformations do not change the cv-qualification or data
|
| 119 |
representation of the type; the Qualification Adjustments do not change
|
| 120 |
the value category or data representation of the type; and the
|
| 121 |
Promotions and Conversions do not change the value category or
|
| 122 |
-
cv-qualification of the type.
|
| 123 |
|
| 124 |
-
As described in Clause [[conv]], a standard conversion
|
| 125 |
-
either the Identity conversion by itself (that is, no
|
| 126 |
-
consists of one to three conversions from the other four
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
sequence, the conversions are applied in the canonical order: **Lvalue
|
| 130 |
Transformation**, **Promotion** or **Conversion**, **Qualification
|
| 131 |
-
Adjustment**.
|
| 132 |
|
| 133 |
Each conversion in Table [[tab:over.conversions]] also has an
|
| 134 |
associated rank (Exact Match, Promotion, or Conversion). These are used
|
| 135 |
to rank standard conversion sequences ([[over.ics.rank]]). The rank of
|
| 136 |
a conversion sequence is determined by considering the rank of each
|
|
@@ -191,70 +232,88 @@ When a parameter of reference type binds directly ([[dcl.init.ref]]) to
|
|
| 191 |
an argument expression, the implicit conversion sequence is the identity
|
| 192 |
conversion, unless the argument expression has a type that is a derived
|
| 193 |
class of the parameter type, in which case the implicit conversion
|
| 194 |
sequence is a derived-to-base Conversion ([[over.best.ics]]).
|
| 195 |
|
|
|
|
|
|
|
| 196 |
``` cpp
|
| 197 |
struct A {};
|
| 198 |
struct B : public A {} b;
|
| 199 |
int f(A&);
|
| 200 |
int f(B&);
|
| 201 |
-
int i = f(b);
|
| 202 |
-
// f(A&), a conversion
|
| 203 |
```
|
| 204 |
|
|
|
|
|
|
|
| 205 |
If the parameter binds directly to the result of applying a conversion
|
| 206 |
function to the argument expression, the implicit conversion sequence is
|
| 207 |
a user-defined conversion sequence ([[over.ics.user]]), with the second
|
| 208 |
standard conversion sequence either an identity conversion or, if the
|
| 209 |
conversion function returns an entity of a type that is a derived class
|
| 210 |
of the parameter type, a derived-to-base Conversion.
|
| 211 |
|
| 212 |
When a parameter of reference type is not bound directly to an argument
|
| 213 |
expression, the conversion sequence is the one required to convert the
|
| 214 |
-
argument expression to the
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
conversion.
|
| 220 |
|
| 221 |
Except for an implicit object parameter, for which see
|
| 222 |
[[over.match.funcs]], a standard conversion sequence cannot be formed if
|
| 223 |
it requires binding an lvalue reference other than a reference to a
|
| 224 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 225 |
-
an lvalue other than a function lvalue.
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
|
|
|
|
|
|
| 230 |
|
| 231 |
Other restrictions on binding a reference to a particular argument that
|
| 232 |
are not based on the types of the reference and the argument do not
|
| 233 |
-
affect the formation of a standard conversion sequence, however.
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
| 241 |
|
| 242 |
##### List-initialization sequence <a id="over.ics.list">[[over.ics.list]]</a>
|
| 243 |
|
| 244 |
When an argument is an initializer list ([[dcl.init.list]]), it is not
|
| 245 |
an expression and special rules apply for converting it to a parameter
|
| 246 |
type.
|
| 247 |
|
| 248 |
-
If the parameter type is `
|
| 249 |
-
|
| 250 |
-
conversion sequence is the
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
conversion even in the context of a call to an initializer-list
|
| 254 |
constructor.
|
| 255 |
|
|
|
|
|
|
|
| 256 |
``` cpp
|
| 257 |
void f(std::initializer_list<int>);
|
| 258 |
f( {} ); // OK: f(initializer_list<int>) identity conversion
|
| 259 |
f( {1,2,3} ); // OK: f(initializer_list<int>) identity conversion
|
| 260 |
f( {'a','b'} ); // OK: f(initializer_list<int>) integral promotion
|
|
@@ -273,27 +332,38 @@ g({ "foo", "bar" }); // OK, uses #3
|
|
| 273 |
typedef int IA[3];
|
| 274 |
void h(const IA&);
|
| 275 |
h({ 1, 2, 3 }); // OK: identity conversion
|
| 276 |
```
|
| 277 |
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
|
|
|
| 284 |
|
| 285 |
Otherwise, if the parameter is a non-aggregate class `X` and overload
|
| 286 |
-
resolution per [[over.match.list]] chooses a single best constructor
|
| 287 |
-
`X` to perform the initialization of an object of type `X` from
|
| 288 |
-
argument initializer list
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
|
| 296 |
``` cpp
|
| 297 |
struct A {
|
| 298 |
A(std::initializer_list<int>);
|
| 299 |
};
|
|
@@ -321,16 +391,20 @@ struct D {
|
|
| 321 |
};
|
| 322 |
void i(D);
|
| 323 |
i({ {1,2}, {"bar"} }); // OK: i(D(A(std::initializer_list<int>{1,2\), C(std::string("bar"))))}
|
| 324 |
```
|
| 325 |
|
|
|
|
|
|
|
| 326 |
Otherwise, if the parameter has an aggregate type which can be
|
| 327 |
initialized from the initializer list according to the rules for
|
| 328 |
aggregate initialization ([[dcl.init.aggr]]), the implicit conversion
|
| 329 |
sequence is a user-defined conversion sequence with the second standard
|
| 330 |
conversion sequence an identity conversion.
|
| 331 |
|
|
|
|
|
|
|
| 332 |
``` cpp
|
| 333 |
struct A {
|
| 334 |
int m1;
|
| 335 |
double m2;
|
| 336 |
};
|
|
@@ -338,13 +412,18 @@ struct A {
|
|
| 338 |
void f(A);
|
| 339 |
f( {'a', 'b'} ); // OK: f(A(int,double)) user-defined conversion
|
| 340 |
f( {1.0} ); // error: narrowing
|
| 341 |
```
|
| 342 |
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
``` cpp
|
| 348 |
struct A {
|
| 349 |
int m1;
|
| 350 |
double m2;
|
|
@@ -356,25 +435,33 @@ f( {1.0} ); // error: narrowing
|
|
| 356 |
|
| 357 |
void g(const double &);
|
| 358 |
g({1}); // same conversion as int to double
|
| 359 |
```
|
| 360 |
|
|
|
|
|
|
|
| 361 |
Otherwise, if the parameter type is not a class:
|
| 362 |
|
| 363 |
-
- if the initializer list has one element
|
| 364 |
-
|
| 365 |
-
type;
|
|
|
|
| 366 |
``` cpp
|
| 367 |
void f(int);
|
| 368 |
f( {'a'} ); // OK: same conversion as char to int
|
| 369 |
f( {1.0} ); // error: narrowing
|
| 370 |
```
|
|
|
|
|
|
|
| 371 |
- if the initializer list has no elements, the implicit conversion
|
| 372 |
sequence is the identity conversion.
|
|
|
|
| 373 |
``` cpp
|
| 374 |
void f(int);
|
| 375 |
f( { } ); // OK: identity conversion
|
| 376 |
```
|
| 377 |
|
|
|
|
|
|
|
| 378 |
In all cases other than those enumerated above, no conversion is
|
| 379 |
possible.
|
| 380 |
|
|
|
|
| 8 |
single expression ([[dcl.init]], [[dcl.init.ref]]).
|
| 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. Other
|
| 13 |
+
properties, such as the lifetime, storage class, alignment,
|
| 14 |
+
accessibility of the argument, whether the argument is a bit-field, and
|
| 15 |
+
whether a function is deleted ([[dcl.fct.def.delete]]), are ignored.
|
| 16 |
+
So, although an implicit conversion sequence can be defined for a given
|
| 17 |
+
argument-parameter pair, the conversion from the argument to the
|
| 18 |
+
parameter might still be ill-formed in the final analysis.
|
| 19 |
|
| 20 |
A well-formed implicit conversion sequence is one of the following
|
| 21 |
forms:
|
| 22 |
|
| 23 |
- a *standard conversion sequence* ([[over.ics.scs]]),
|
|
|
|
| 35 |
- [[over.match.ctor]], when the argument is the temporary in the second
|
| 36 |
step of a class copy-initialization,
|
| 37 |
- [[over.match.copy]], [[over.match.conv]], or [[over.match.ref]] (in
|
| 38 |
all cases), or
|
| 39 |
- the second phase of [[over.match.list]] when the initializer list has
|
| 40 |
+
exactly one element that is itself an initializer list, and the target
|
| 41 |
+
is the first parameter of a constructor of class `X`, and the
|
| 42 |
+
conversion is to `X` or reference to cv `X`,
|
| 43 |
|
| 44 |
+
user-defined conversion sequences are not considered.
|
| 45 |
+
|
| 46 |
+
[*Note 1*: These rules prevent more than one user-defined conversion
|
| 47 |
+
from being applied during overload resolution, thereby avoiding infinite
|
| 48 |
+
recursion. — *end note*]
|
| 49 |
+
|
| 50 |
+
[*Example 1*:
|
| 51 |
|
| 52 |
``` cpp
|
| 53 |
struct Y { Y(int); };
|
| 54 |
struct A { operator int(); };
|
| 55 |
Y y1 = A(); // error: A::operator int() is not a candidate
|
|
|
|
| 58 |
struct B { operator X(); };
|
| 59 |
B b;
|
| 60 |
X x({b}); // error: B::operator X() is not a candidate
|
| 61 |
```
|
| 62 |
|
| 63 |
+
— *end example*]
|
| 64 |
+
|
| 65 |
For the case where the parameter type is a reference, see
|
| 66 |
[[over.ics.ref]].
|
| 67 |
|
| 68 |
When the parameter type is not a reference, the implicit conversion
|
| 69 |
sequence models a copy-initialization of the parameter from the argument
|
| 70 |
expression. The implicit conversion sequence is the one required to
|
| 71 |
convert the argument expression to a prvalue of the type of the
|
| 72 |
+
parameter.
|
| 73 |
+
|
| 74 |
+
[*Note 2*: When the parameter has a class type, this is a conceptual
|
| 75 |
conversion defined for the purposes of Clause [[over]]; the actual
|
| 76 |
initialization is defined in terms of constructors and is not a
|
| 77 |
+
conversion. — *end note*]
|
| 78 |
+
|
| 79 |
+
Any difference in top-level cv-qualification is subsumed by the
|
| 80 |
+
initialization itself and does not constitute a conversion.
|
| 81 |
+
|
| 82 |
+
[*Example 2*: A parameter of type `A` can be initialized from an
|
| 83 |
+
argument of type `const A`. The implicit conversion sequence for that
|
| 84 |
+
case is the identity sequence; it contains no “conversion” from
|
| 85 |
+
`const A` to `A`. — *end example*]
|
| 86 |
+
|
| 87 |
When the parameter has a class type and the argument expression has the
|
| 88 |
same type, the implicit conversion sequence is an identity conversion.
|
| 89 |
When the parameter has a class type and the argument expression has a
|
| 90 |
derived class type, the implicit conversion sequence is a
|
| 91 |
derived-to-base Conversion from the derived class to the base class.
|
| 92 |
+
|
| 93 |
+
[*Note 3*: There is no such standard conversion; this derived-to-base
|
| 94 |
+
Conversion exists only in the description of implicit conversion
|
| 95 |
+
sequences. — *end note*]
|
| 96 |
+
|
| 97 |
+
A derived-to-base Conversion has Conversion rank ([[over.ics.scs]]).
|
| 98 |
|
| 99 |
In all contexts, when converting to the implicit object parameter or
|
| 100 |
when converting to the left operand of an assignment operation only
|
| 101 |
+
standard conversion sequences are allowed.
|
|
|
|
| 102 |
|
| 103 |
If no conversions are required to match an argument to a parameter type,
|
| 104 |
the implicit conversion sequence is the standard conversion sequence
|
| 105 |
consisting of the identity conversion ([[over.ics.scs]]).
|
| 106 |
|
| 107 |
If no sequence of conversions can be found to convert an argument to a
|
| 108 |
+
parameter type, an implicit conversion sequence cannot be formed.
|
|
|
|
| 109 |
|
| 110 |
If several different sequences of conversions exist that each convert
|
| 111 |
the argument to the parameter type, the implicit conversion sequence
|
| 112 |
associated with the parameter is defined to be the unique conversion
|
| 113 |
sequence designated the *ambiguous conversion sequence*. For the purpose
|
| 114 |
of ranking implicit conversion sequences as described in
|
| 115 |
[[over.ics.rank]], the ambiguous conversion sequence is treated as a
|
| 116 |
+
user-defined conversion sequence that is indistinguishable from any
|
| 117 |
+
other user-defined conversion sequence.
|
| 118 |
+
|
| 119 |
+
[*Note 4*:
|
| 120 |
+
|
| 121 |
+
This rule prevents a function from becoming non-viable because of an
|
| 122 |
+
ambiguous conversion sequence for one of its parameters.
|
| 123 |
+
|
| 124 |
+
[*Example 3*:
|
| 125 |
+
|
| 126 |
+
``` cpp
|
| 127 |
+
class B;
|
| 128 |
+
class A { A (B&);};
|
| 129 |
+
class B { operator A (); };
|
| 130 |
+
class C { C (B&); };
|
| 131 |
+
void f(A) { }
|
| 132 |
+
void f(C) { }
|
| 133 |
+
B b;
|
| 134 |
+
f(b); // ill-formed: ambiguous because there is a conversion b → C (via constructor)
|
| 135 |
+
// and an (ambiguous) conversion b → A (via constructor or conversion function)
|
| 136 |
+
void f(B) { }
|
| 137 |
+
f(b); // OK, unambiguous
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
— *end example*]
|
| 141 |
+
|
| 142 |
+
— *end note*]
|
| 143 |
+
|
| 144 |
+
If a function that uses the ambiguous conversion sequence is selected as
|
| 145 |
+
the best viable function, the call will be ill-formed because the
|
| 146 |
+
conversion of one of the arguments in the call is ambiguous.
|
| 147 |
|
| 148 |
The three forms of implicit conversion sequences mentioned above are
|
| 149 |
defined in the following subclauses.
|
| 150 |
|
| 151 |
##### Standard conversion sequences <a id="over.ics.scs">[[over.ics.scs]]</a>
|
| 152 |
|
| 153 |
Table [[tab:over.conversions]] summarizes the conversions defined in
|
| 154 |
Clause [[conv]] and partitions them into four disjoint categories:
|
| 155 |
Lvalue Transformation, Qualification Adjustment, Promotion, and
|
| 156 |
+
Conversion.
|
| 157 |
+
|
| 158 |
+
[*Note 5*: These categories are orthogonal with respect to value
|
| 159 |
category, cv-qualification, and data representation: the Lvalue
|
| 160 |
Transformations do not change the cv-qualification or data
|
| 161 |
representation of the type; the Qualification Adjustments do not change
|
| 162 |
the value category or data representation of the type; and the
|
| 163 |
Promotions and Conversions do not change the value category or
|
| 164 |
+
cv-qualification of the type. — *end note*]
|
| 165 |
|
| 166 |
+
[*Note 6*: As described in Clause [[conv]], a standard conversion
|
| 167 |
+
sequence is either the Identity conversion by itself (that is, no
|
| 168 |
+
conversion) or consists of one to three conversions from the other four
|
| 169 |
+
categories. If there are two or more conversions in the sequence, the
|
| 170 |
+
conversions are applied in the canonical order: **Lvalue
|
|
|
|
| 171 |
Transformation**, **Promotion** or **Conversion**, **Qualification
|
| 172 |
+
Adjustment**. — *end note*]
|
| 173 |
|
| 174 |
Each conversion in Table [[tab:over.conversions]] also has an
|
| 175 |
associated rank (Exact Match, Promotion, or Conversion). These are used
|
| 176 |
to rank standard conversion sequences ([[over.ics.rank]]). The rank of
|
| 177 |
a conversion sequence is determined by considering the rank of each
|
|
|
|
| 232 |
an argument expression, the implicit conversion sequence is the identity
|
| 233 |
conversion, unless the argument expression has a type that is a derived
|
| 234 |
class of the parameter type, in which case the implicit conversion
|
| 235 |
sequence is a derived-to-base Conversion ([[over.best.ics]]).
|
| 236 |
|
| 237 |
+
[*Example 4*:
|
| 238 |
+
|
| 239 |
``` cpp
|
| 240 |
struct A {};
|
| 241 |
struct B : public A {} b;
|
| 242 |
int f(A&);
|
| 243 |
int f(B&);
|
| 244 |
+
int i = f(b); // calls f(B&), an exact match, rather than f(A&), a conversion
|
|
|
|
| 245 |
```
|
| 246 |
|
| 247 |
+
— *end example*]
|
| 248 |
+
|
| 249 |
If the parameter binds directly to the result of applying a conversion
|
| 250 |
function to the argument expression, the implicit conversion sequence is
|
| 251 |
a user-defined conversion sequence ([[over.ics.user]]), with the second
|
| 252 |
standard conversion sequence either an identity conversion or, if the
|
| 253 |
conversion function returns an entity of a type that is a derived class
|
| 254 |
of the parameter type, a derived-to-base Conversion.
|
| 255 |
|
| 256 |
When a parameter of reference type is not bound directly to an argument
|
| 257 |
expression, the conversion sequence is the one required to convert the
|
| 258 |
+
argument expression to the referenced type according to
|
| 259 |
+
[[over.best.ics]]. Conceptually, this conversion sequence corresponds to
|
| 260 |
+
copy-initializing a temporary of the referenced type with the argument
|
| 261 |
+
expression. Any difference in top-level cv-qualification is subsumed by
|
| 262 |
+
the initialization itself and does not constitute a conversion.
|
|
|
|
| 263 |
|
| 264 |
Except for an implicit object parameter, for which see
|
| 265 |
[[over.match.funcs]], a standard conversion sequence cannot be formed if
|
| 266 |
it requires binding an lvalue reference other than a reference to a
|
| 267 |
non-volatile `const` type to an rvalue or binding an rvalue reference to
|
| 268 |
+
an lvalue other than a function lvalue.
|
| 269 |
+
|
| 270 |
+
[*Note 7*: This means, for example, that a candidate function cannot be
|
| 271 |
+
a viable function if it has a non-`const` lvalue reference parameter
|
| 272 |
+
(other than the implicit object parameter) and the corresponding
|
| 273 |
+
argument would require a temporary to be created to initialize the
|
| 274 |
+
lvalue reference (see [[dcl.init.ref]]). — *end note*]
|
| 275 |
|
| 276 |
Other restrictions on binding a reference to a particular argument that
|
| 277 |
are not based on the types of the reference and the argument do not
|
| 278 |
+
affect the formation of a standard conversion sequence, however.
|
| 279 |
+
|
| 280 |
+
[*Example 5*: A function with an “lvalue reference to `int`” parameter
|
| 281 |
+
can be a viable candidate even if the corresponding argument is an `int`
|
| 282 |
+
bit-field. The formation of implicit conversion sequences treats the
|
| 283 |
+
`int` bit-field as an `int` lvalue and finds an exact match with the
|
| 284 |
+
parameter. If the function is selected by overload resolution, the call
|
| 285 |
+
will nonetheless be ill-formed because of the prohibition on binding a
|
| 286 |
+
non-`const` lvalue reference to a bit-field (
|
| 287 |
+
[[dcl.init.ref]]). — *end example*]
|
| 288 |
|
| 289 |
##### List-initialization sequence <a id="over.ics.list">[[over.ics.list]]</a>
|
| 290 |
|
| 291 |
When an argument is an initializer list ([[dcl.init.list]]), it is not
|
| 292 |
an expression and special rules apply for converting it to a parameter
|
| 293 |
type.
|
| 294 |
|
| 295 |
+
If the parameter type is an aggregate class `X` and the initializer list
|
| 296 |
+
has a single element of type cv `U`, where `U` is `X` or a class derived
|
| 297 |
+
from `X`, the implicit conversion sequence is the one required to
|
| 298 |
+
convert the element to the parameter type.
|
| 299 |
+
|
| 300 |
+
Otherwise, if the parameter type is a character array [^11] and the
|
| 301 |
+
initializer list has a single element that is an appropriately-typed
|
| 302 |
+
string literal ([[dcl.init.string]]), the implicit conversion sequence
|
| 303 |
+
is the identity conversion.
|
| 304 |
+
|
| 305 |
+
Otherwise, if the parameter type is `std::initializer_list<X>` and all
|
| 306 |
+
the elements of the initializer list can be implicitly converted to `X`,
|
| 307 |
+
the implicit conversion sequence is the worst conversion necessary to
|
| 308 |
+
convert an element of the list to `X`, or if the initializer list has no
|
| 309 |
+
elements, the identity conversion. This conversion can be a user-defined
|
| 310 |
conversion even in the context of a call to an initializer-list
|
| 311 |
constructor.
|
| 312 |
|
| 313 |
+
[*Example 6*:
|
| 314 |
+
|
| 315 |
``` cpp
|
| 316 |
void f(std::initializer_list<int>);
|
| 317 |
f( {} ); // OK: f(initializer_list<int>) identity conversion
|
| 318 |
f( {1,2,3} ); // OK: f(initializer_list<int>) identity conversion
|
| 319 |
f( {'a','b'} ); // OK: f(initializer_list<int>) integral promotion
|
|
|
|
| 332 |
typedef int IA[3];
|
| 333 |
void h(const IA&);
|
| 334 |
h({ 1, 2, 3 }); // OK: identity conversion
|
| 335 |
```
|
| 336 |
|
| 337 |
+
— *end example*]
|
| 338 |
+
|
| 339 |
+
Otherwise, if the parameter type is “array of `N` `X`”, if there exists
|
| 340 |
+
an implicit conversion sequence for each element of the array from the
|
| 341 |
+
corresponding element of the initializer list (or from `{}` if there is
|
| 342 |
+
no such element), the implicit conversion sequence is the worst such
|
| 343 |
+
implicit conversion sequence.
|
| 344 |
|
| 345 |
Otherwise, if the parameter is a non-aggregate class `X` and overload
|
| 346 |
+
resolution per [[over.match.list]] chooses a single best constructor
|
| 347 |
+
`C` of `X` to perform the initialization of an object of type `X` from
|
| 348 |
+
the argument initializer list:
|
| 349 |
+
|
| 350 |
+
- If `C` is not an initializer-list constructor and the initializer list
|
| 351 |
+
has a single element of type cv `U`, where `U` is `X` or a class
|
| 352 |
+
derived from `X`, the implicit conversion sequence has Exact Match
|
| 353 |
+
rank if `U` is `X`, or Conversion rank if `U` is derived from `X`.
|
| 354 |
+
- Otherwise, the implicit conversion sequence is a user-defined
|
| 355 |
+
conversion sequence with the second standard conversion sequence an
|
| 356 |
+
identity conversion.
|
| 357 |
+
|
| 358 |
+
If multiple constructors are viable but none is better than the others,
|
| 359 |
+
the implicit conversion sequence is the ambiguous conversion sequence.
|
| 360 |
+
User-defined conversions are allowed for conversion of the initializer
|
| 361 |
+
list elements to the constructor parameter types except as noted in
|
| 362 |
+
[[over.best.ics]].
|
| 363 |
+
|
| 364 |
+
[*Example 7*:
|
| 365 |
|
| 366 |
``` cpp
|
| 367 |
struct A {
|
| 368 |
A(std::initializer_list<int>);
|
| 369 |
};
|
|
|
|
| 391 |
};
|
| 392 |
void i(D);
|
| 393 |
i({ {1,2}, {"bar"} }); // OK: i(D(A(std::initializer_list<int>{1,2\), C(std::string("bar"))))}
|
| 394 |
```
|
| 395 |
|
| 396 |
+
— *end example*]
|
| 397 |
+
|
| 398 |
Otherwise, if the parameter has an aggregate type which can be
|
| 399 |
initialized from the initializer list according to the rules for
|
| 400 |
aggregate initialization ([[dcl.init.aggr]]), the implicit conversion
|
| 401 |
sequence is a user-defined conversion sequence with the second standard
|
| 402 |
conversion sequence an identity conversion.
|
| 403 |
|
| 404 |
+
[*Example 8*:
|
| 405 |
+
|
| 406 |
``` cpp
|
| 407 |
struct A {
|
| 408 |
int m1;
|
| 409 |
double m2;
|
| 410 |
};
|
|
|
|
| 412 |
void f(A);
|
| 413 |
f( {'a', 'b'} ); // OK: f(A(int,double)) user-defined conversion
|
| 414 |
f( {1.0} ); // error: narrowing
|
| 415 |
```
|
| 416 |
|
| 417 |
+
— *end example*]
|
| 418 |
+
|
| 419 |
+
Otherwise, if the parameter is a reference, see [[over.ics.ref]].
|
| 420 |
+
|
| 421 |
+
[*Note 8*: The rules in this section will apply for initializing the
|
| 422 |
+
underlying temporary for the reference. — *end note*]
|
| 423 |
+
|
| 424 |
+
[*Example 9*:
|
| 425 |
|
| 426 |
``` cpp
|
| 427 |
struct A {
|
| 428 |
int m1;
|
| 429 |
double m2;
|
|
|
|
| 435 |
|
| 436 |
void g(const double &);
|
| 437 |
g({1}); // same conversion as int to double
|
| 438 |
```
|
| 439 |
|
| 440 |
+
— *end example*]
|
| 441 |
+
|
| 442 |
Otherwise, if the parameter type is not a class:
|
| 443 |
|
| 444 |
+
- if the initializer list has one element that is not itself an
|
| 445 |
+
initializer list, the implicit conversion sequence is the one required
|
| 446 |
+
to convert the element to the parameter type;
|
| 447 |
+
\[*Example 10*:
|
| 448 |
``` cpp
|
| 449 |
void f(int);
|
| 450 |
f( {'a'} ); // OK: same conversion as char to int
|
| 451 |
f( {1.0} ); // error: narrowing
|
| 452 |
```
|
| 453 |
+
|
| 454 |
+
— *end example*]
|
| 455 |
- if the initializer list has no elements, the implicit conversion
|
| 456 |
sequence is the identity conversion.
|
| 457 |
+
\[*Example 11*:
|
| 458 |
``` cpp
|
| 459 |
void f(int);
|
| 460 |
f( { } ); // OK: identity conversion
|
| 461 |
```
|
| 462 |
|
| 463 |
+
— *end example*]
|
| 464 |
+
|
| 465 |
In all cases other than those enumerated above, no conversion is
|
| 466 |
possible.
|
| 467 |
|