- tmp/tmpg5rfd34z/{from.md → to.md} +170 -88
tmp/tmpg5rfd34z/{from.md → to.md}
RENAMED
|
@@ -147,10 +147,14 @@ handled as candidate functions in the usual way.[^2] A given name can
|
|
| 147 |
refer to one or more function templates and also to a set of overloaded
|
| 148 |
non-template functions. In such a case, the candidate functions
|
| 149 |
generated from each function template are combined with the set of
|
| 150 |
non-template candidate functions.
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
#### Function call syntax <a id="over.match.call">[[over.match.call]]</a>
|
| 153 |
|
| 154 |
In a function call ([[expr.call]])
|
| 155 |
|
| 156 |
``` bnf
|
|
@@ -233,11 +237,11 @@ operators of `T` are obtained by ordinary lookup of the name
|
|
| 233 |
|
| 234 |
In addition, for each non-explicit conversion function declared in `T`
|
| 235 |
of the form
|
| 236 |
|
| 237 |
``` bnf
|
| 238 |
-
'operator' conversion-type-id '( )' attribute-specifier-seqₒₚₜ
|
| 239 |
```
|
| 240 |
|
| 241 |
where *cv-qualifier* is the same cv-qualification as, or a greater
|
| 242 |
cv-qualification than, *cv*, and where *conversion-type-id* denotes the
|
| 243 |
type “pointer to function of (`P1`,...,`Pn)` returning `R`”, or the type
|
|
@@ -307,13 +311,13 @@ struct String {
|
|
| 307 |
};
|
| 308 |
String operator + (const String&, const String&);
|
| 309 |
|
| 310 |
void f(void) {
|
| 311 |
const char* p= "one" + "two"; // ill-formed because neither
|
| 312 |
-
// operand has
|
| 313 |
int I = 1 + 1; // Always evaluates to 2 even if
|
| 314 |
-
//
|
| 315 |
// would perform the operation.
|
| 316 |
}
|
| 317 |
```
|
| 318 |
|
| 319 |
If either operand has a type that is a class or an enumeration, a
|
|
@@ -344,13 +348,14 @@ version is `T1`, and for a binary operator `@` with a left operand of a
|
|
| 344 |
type whose cv-unqualified version is `T1` and a right operand of a type
|
| 345 |
whose cv-unqualified version is `T2`, three sets of candidate functions,
|
| 346 |
designated *member candidates*, *non-member candidates* and *built-in
|
| 347 |
candidates*, are constructed as follows:
|
| 348 |
|
| 349 |
-
- If `T1` is a complete class type
|
| 350 |
-
result of the qualified lookup of
|
| 351 |
-
[[over.call.func]]); otherwise, the set of member
|
|
|
|
| 352 |
- The set of non-member candidates is the result of the unqualified
|
| 353 |
lookup of `operator@` in the context of the expression according to
|
| 354 |
the usual rules for name lookup in unqualified function calls (
|
| 355 |
[[basic.lookup.argdep]]) except that all member functions are ignored.
|
| 356 |
However, if no operand has a class type, only those non-member
|
|
@@ -365,12 +370,12 @@ candidates*, are constructed as follows:
|
|
| 365 |
defined in [[over.built]] that, compared to the given operator,
|
| 366 |
- have the same operator name, and
|
| 367 |
- accept the same number of operands, and
|
| 368 |
- accept operand types to which the given operand or operands can be
|
| 369 |
converted according to [[over.best.ics]], and
|
| 370 |
-
- do not have the same parameter-type-list as any non-
|
| 371 |
-
|
| 372 |
|
| 373 |
For the built-in assignment operators, conversions of the left operand
|
| 374 |
are restricted as follows:
|
| 375 |
|
| 376 |
- no temporaries are introduced to hold the left operand, and
|
|
@@ -396,15 +401,30 @@ void m() {
|
|
| 396 |
a + b; // operator+(a,b) chosen over int(a) + int(b)
|
| 397 |
}
|
| 398 |
```
|
| 399 |
|
| 400 |
If a built-in candidate is selected by overload resolution, the operands
|
| 401 |
-
are converted to the types of the corresponding parameters
|
| 402 |
-
selected operation function
|
|
|
|
|
|
|
| 403 |
corresponding built-in operator and interpreted according to Clause
|
| 404 |
[[expr]].
|
| 405 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
The second operand of operator `->` is ignored in selecting an
|
| 407 |
`operator->` function, and is not an argument when the `operator->`
|
| 408 |
function is called. When `operator->` returns, the operator `->` is
|
| 409 |
applied to the value returned, with the original second operand.[^7]
|
| 410 |
|
|
@@ -449,29 +469,32 @@ the candidate functions are all the converting constructors (
|
|
| 449 |
|
| 450 |
Under the conditions specified in [[dcl.init]], as part of a
|
| 451 |
copy-initialization of an object of class type, a user-defined
|
| 452 |
conversion can be invoked to convert an initializer expression to the
|
| 453 |
type of the object being initialized. Overload resolution is used to
|
| 454 |
-
select the user-defined conversion to be invoked.
|
| 455 |
-
|
| 456 |
-
|
|
|
|
|
|
|
|
|
|
| 457 |
|
| 458 |
- The converting constructors ([[class.conv.ctor]]) of `T` are
|
| 459 |
candidate functions.
|
| 460 |
- When the type of the initializer expression is a class type “*cv*
|
| 461 |
`S`”, the non-explicit conversion functions of `S` and its base
|
| 462 |
classes are considered. When initializing a temporary to be bound to
|
| 463 |
the first parameter of a constructor that takes a reference to
|
| 464 |
possibly cv-qualified `T` as its first argument, called with a single
|
| 465 |
-
argument in the context of direct-initialization
|
| 466 |
-
functions are also considered. Those
|
| 467 |
-
and yield a type whose cv-unqualified
|
| 468 |
-
or is a derived class thereof are
|
| 469 |
-
functions that return “reference to
|
| 470 |
-
depending on the type of reference, of
|
| 471 |
-
considered to yield `X` for this process of
|
| 472 |
-
functions.
|
| 473 |
|
| 474 |
In both cases, the argument list has one argument, which is the
|
| 475 |
initializer expression. This argument will be compared against the first
|
| 476 |
parameter of the constructors and against the implicit object parameter
|
| 477 |
of the conversion functions.
|
|
@@ -514,18 +537,23 @@ applying a conversion function to an initializer expression. Overload
|
|
| 514 |
resolution is used to select the conversion function to be invoked.
|
| 515 |
Assuming that “*cv1* `T`” is the underlying type of the reference being
|
| 516 |
initialized, and “*cv* `S`” is the type of the initializer expression,
|
| 517 |
with `S` a class type, the candidate functions are selected as follows:
|
| 518 |
|
| 519 |
-
- The conversion functions of `S` and its base classes are considered
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
candidate functions.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
|
| 528 |
The argument list has one argument, which is the initializer expression.
|
| 529 |
This argument will be compared against the implicit object parameter of
|
| 530 |
the conversion functions.
|
| 531 |
|
|
@@ -625,12 +653,29 @@ and then
|
|
| 625 |
float x = a; // ambiguous: both possibilities require conversions,
|
| 626 |
// and neither is better than the other
|
| 627 |
```
|
| 628 |
|
| 629 |
or, if not that,
|
| 630 |
-
-
|
| 631 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 632 |
- `F1` and `F2` are function template specializations, and the function
|
| 633 |
template for `F1` is more specialized than the template for `F2`
|
| 634 |
according to the partial ordering rules described in
|
| 635 |
[[temp.func.order]].
|
| 636 |
|
|
@@ -707,20 +752,41 @@ forms:
|
|
| 707 |
|
| 708 |
- a *standard conversion sequence* ([[over.ics.scs]]),
|
| 709 |
- a *user-defined conversion sequence* ([[over.ics.user]]), or
|
| 710 |
- an *ellipsis conversion sequence* ([[over.ics.ellipsis]]).
|
| 711 |
|
| 712 |
-
However,
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
[[over.match.
|
| 721 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 722 |
|
| 723 |
For the case where the parameter type is a reference, see
|
| 724 |
[[over.ics.ref]].
|
| 725 |
|
| 726 |
When the parameter type is not a reference, the implicit conversion
|
|
@@ -838,12 +904,12 @@ conversion function template, the second standard conversion sequence
|
|
| 838 |
shall have exact match rank.
|
| 839 |
|
| 840 |
A conversion of an expression of class type to the same class type is
|
| 841 |
given Exact Match rank, and a conversion of an expression of class type
|
| 842 |
to a base class of that type is given Conversion rank, in spite of the
|
| 843 |
-
fact that a
|
| 844 |
-
|
| 845 |
|
| 846 |
##### Ellipsis conversion sequences <a id="over.ics.ellipsis">[[over.ics.ellipsis]]</a>
|
| 847 |
|
| 848 |
An ellipsis conversion sequence occurs when an argument in a function
|
| 849 |
call is matched with the ellipsis parameter specification of the
|
|
@@ -901,29 +967,27 @@ formation of implicit conversion sequences treats the `int` bit-field as
|
|
| 901 |
an `int` lvalue and finds an exact match with the parameter. If the
|
| 902 |
function is selected by overload resolution, the call will nonetheless
|
| 903 |
be ill-formed because of the prohibition on binding a non-`const` lvalue
|
| 904 |
reference to a bit-field ([[dcl.init.ref]]).
|
| 905 |
|
| 906 |
-
The binding of a reference to an expression that is
|
| 907 |
-
*reference-compatible with added qualification* influences the rank of a
|
| 908 |
-
standard conversion; see [[over.ics.rank]] and [[dcl.init.ref]].
|
| 909 |
-
|
| 910 |
##### List-initialization sequence <a id="over.ics.list">[[over.ics.list]]</a>
|
| 911 |
|
| 912 |
When an argument is an initializer list ([[dcl.init.list]]), it is not
|
| 913 |
an expression and special rules apply for converting it to a parameter
|
| 914 |
type.
|
| 915 |
|
| 916 |
-
If the parameter type is `std::initializer_list<X>`
|
| 917 |
-
|
| 918 |
-
|
| 919 |
-
|
| 920 |
-
conversion can be a user-defined
|
| 921 |
-
call to an initializer-list
|
|
|
|
| 922 |
|
| 923 |
``` cpp
|
| 924 |
void f(std::initializer_list<int>);
|
|
|
|
| 925 |
f( {1,2,3} ); // OK: f(initializer_list<int>) identity conversion
|
| 926 |
f( {'a','b'} ); // OK: f(initializer_list<int>) integral promotion
|
| 927 |
f( {1.0} ); // error: narrowing
|
| 928 |
|
| 929 |
struct A {
|
|
@@ -939,19 +1003,27 @@ g({ "foo", "bar" }); // OK, uses #3
|
|
| 939 |
typedef int IA[3];
|
| 940 |
void h(const IA&);
|
| 941 |
h({ 1, 2, 3 }); // OK: identity conversion
|
| 942 |
```
|
| 943 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 944 |
Otherwise, if the parameter is a non-aggregate class `X` and overload
|
| 945 |
resolution per [[over.match.list]] chooses a single best constructor of
|
| 946 |
`X` to perform the initialization of an object of type `X` from the
|
| 947 |
argument initializer list, the implicit conversion sequence is a
|
| 948 |
-
user-defined conversion sequence
|
| 949 |
-
|
| 950 |
-
the
|
| 951 |
-
|
| 952 |
-
|
|
|
|
| 953 |
|
| 954 |
``` cpp
|
| 955 |
struct A {
|
| 956 |
A(std::initializer_list<int>);
|
| 957 |
};
|
|
@@ -961,11 +1033,11 @@ f( {'a', 'b'} ); // OK: f(A(std::initializer_list<int>)) user-defined
|
|
| 961 |
struct B {
|
| 962 |
B(int, double);
|
| 963 |
};
|
| 964 |
void g(B);
|
| 965 |
g( {'a', 'b'} ); // OK: g(B(int,double)) user-defined conversion
|
| 966 |
-
g( {1.0, 1
|
| 967 |
|
| 968 |
void f(B);
|
| 969 |
f( {'a', 'b'} ); // error: ambiguous f(A) or f(B)
|
| 970 |
|
| 971 |
struct C {
|
|
@@ -973,20 +1045,21 @@ struct C {
|
|
| 973 |
};
|
| 974 |
void h(C);
|
| 975 |
h({"foo"}); // OK: h(C(std::string("foo")))
|
| 976 |
|
| 977 |
struct D {
|
| 978 |
-
|
| 979 |
};
|
| 980 |
void i(D);
|
| 981 |
i({ {1,2}, {"bar"} }); // OK: i(D(A(std::initializer_list<int>{1,2\),C(std::string("bar"))))}
|
| 982 |
```
|
| 983 |
|
| 984 |
Otherwise, if the parameter has an aggregate type which can be
|
| 985 |
initialized from the initializer list according to the rules for
|
| 986 |
aggregate initialization ([[dcl.init.aggr]]), the implicit conversion
|
| 987 |
-
sequence is a user-defined conversion sequence
|
|
|
|
| 988 |
|
| 989 |
``` cpp
|
| 990 |
struct A {
|
| 991 |
int m1;
|
| 992 |
double m2;
|
|
@@ -1066,23 +1139,10 @@ conversion sequences unless one of the following rules applies:
|
|
| 1066 |
sequence is considered to be a subsequence of any non-identity
|
| 1067 |
conversion sequence) or, if not that,
|
| 1068 |
- the rank of `S1` is better than the rank of `S2`, or `S1` and `S2`
|
| 1069 |
have the same rank and are distinguishable by the rules in the
|
| 1070 |
paragraph below, or, if not that,
|
| 1071 |
-
- `S1`
|
| 1072 |
-
and `S2` differ only in their qualification conversion and yield
|
| 1073 |
-
similar types `T1` and `T2` ([[conv.qual]]), respectively, and the
|
| 1074 |
-
cv-qualification signature of type `T1` is a proper subset of the
|
| 1075 |
-
cv-qualification signature of type `T2`.
|
| 1076 |
-
``` cpp
|
| 1077 |
-
int f(const int *);
|
| 1078 |
-
int f(int *);
|
| 1079 |
-
int i;
|
| 1080 |
-
int j = f(&i); // calls f(int*)
|
| 1081 |
-
```
|
| 1082 |
-
|
| 1083 |
-
or, if not that,
|
| 1084 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and neither
|
| 1085 |
refers to an implicit object parameter of a non-static member
|
| 1086 |
function declared without a *ref-qualifier*, and `S1` binds an
|
| 1087 |
rvalue reference to an rvalue and `S2` binds an lvalue reference.
|
| 1088 |
``` cpp
|
|
@@ -1113,15 +1173,30 @@ conversion sequences unless one of the following rules applies:
|
|
| 1113 |
or, if not that,
|
| 1114 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and `S1`
|
| 1115 |
binds an lvalue reference to a function lvalue and `S2` binds an
|
| 1116 |
rvalue reference to a function lvalue.
|
| 1117 |
``` cpp
|
| 1118 |
-
|
| 1119 |
-
|
| 1120 |
void g();
|
| 1121 |
-
int i1 = f(g); // calls
|
| 1122 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1123 |
- `S1`
|
| 1124 |
and `S2` are reference bindings ([[dcl.init.ref]]), and the types
|
| 1125 |
to which the references refer are the same type except for top-level
|
| 1126 |
cv-qualifiers, and the type to which the reference initialized by
|
| 1127 |
`S2` refers is more cv-qualified than the type to which the
|
|
@@ -1145,33 +1220,40 @@ conversion sequences unless one of the following rules applies:
|
|
| 1145 |
b.f(); // calls X::f()
|
| 1146 |
}
|
| 1147 |
```
|
| 1148 |
- User-defined conversion sequence `U1` is a better conversion sequence
|
| 1149 |
than another user-defined conversion sequence `U2` if they contain the
|
| 1150 |
-
same user-defined conversion function or constructor or
|
| 1151 |
-
|
| 1152 |
-
|
|
|
|
| 1153 |
``` cpp
|
| 1154 |
struct A {
|
| 1155 |
operator short();
|
| 1156 |
} a;
|
| 1157 |
int f(int);
|
| 1158 |
int f(float);
|
| 1159 |
int i = f(a); // calls f(int), because short → int is
|
| 1160 |
// better than short → float.
|
| 1161 |
```
|
| 1162 |
- List-initialization sequence `L1` is a better conversion sequence than
|
| 1163 |
-
list-initialization sequence `L2` if
|
| 1164 |
-
`std::initializer_list<X>` for some `X` and `L2`
|
|
|
|
|
|
|
|
|
|
| 1165 |
|
| 1166 |
Standard conversion sequences are ordered by their ranks: an Exact Match
|
| 1167 |
is a better conversion than a Promotion, which is a better conversion
|
| 1168 |
than a Conversion. Two conversion sequences with the same rank are
|
| 1169 |
indistinguishable unless one of the following rules applies:
|
| 1170 |
|
| 1171 |
- A conversion that does not convert a pointer, a pointer to member, or
|
| 1172 |
`std::nullptr_t` to `bool` is better than one that does.
|
|
|
|
|
|
|
|
|
|
| 1173 |
- If class `B` is derived directly or indirectly from class `A`,
|
| 1174 |
conversion of `B*` to `A*` is better than conversion of `B*` to
|
| 1175 |
`void*`, and conversion of `A*` to `void*` is better than conversion
|
| 1176 |
of `B*` to `void*`.
|
| 1177 |
- If class `B` is derived directly or indirectly from class `A` and
|
|
@@ -1185,21 +1267,21 @@ indistinguishable unless one of the following rules applies:
|
|
| 1185 |
C* pc;
|
| 1186 |
int f(A*);
|
| 1187 |
int f(B*);
|
| 1188 |
int i = f(pc); // calls f(B*)
|
| 1189 |
```
|
| 1190 |
-
- binding of an expression of type `C` to a reference
|
| 1191 |
-
better than binding an expression of type `C` to a reference
|
| 1192 |
-
`A
|
| 1193 |
- conversion of `A::*` to `B::*` is better than conversion of `A::*`
|
| 1194 |
to `C::*`,
|
| 1195 |
- conversion of `C` to `B` is better than conversion of `C` to `A`,
|
| 1196 |
- conversion of `B*` to `A*` is better than conversion of `C*` to
|
| 1197 |
`A*`,
|
| 1198 |
-
- binding of an expression of type `B` to a reference
|
| 1199 |
-
better than binding an expression of type `C` to a reference
|
| 1200 |
-
`A
|
| 1201 |
- conversion of `B::*` to `C::*` is better than conversion of `A::*`
|
| 1202 |
to `C::*`, and
|
| 1203 |
- conversion of `B` to `A` is better than conversion of `C` to `A`.
|
| 1204 |
|
| 1205 |
Compared conversion sequences will have different source types only in
|
|
|
|
| 147 |
refer to one or more function templates and also to a set of overloaded
|
| 148 |
non-template functions. In such a case, the candidate functions
|
| 149 |
generated from each function template are combined with the set of
|
| 150 |
non-template candidate functions.
|
| 151 |
|
| 152 |
+
A defaulted move constructor or assignment operator ([[class.copy]])
|
| 153 |
+
that is defined as deleted is excluded from the set of candidate
|
| 154 |
+
functions in all contexts.
|
| 155 |
+
|
| 156 |
#### Function call syntax <a id="over.match.call">[[over.match.call]]</a>
|
| 157 |
|
| 158 |
In a function call ([[expr.call]])
|
| 159 |
|
| 160 |
``` bnf
|
|
|
|
| 237 |
|
| 238 |
In addition, for each non-explicit conversion function declared in `T`
|
| 239 |
of the form
|
| 240 |
|
| 241 |
``` bnf
|
| 242 |
+
'operator' conversion-type-id '( )' cv-qualifier ref-qualifierₒₚₜ exception-specificationₒₚₜ attribute-specifier-seqₒₚₜ ';'
|
| 243 |
```
|
| 244 |
|
| 245 |
where *cv-qualifier* is the same cv-qualification as, or a greater
|
| 246 |
cv-qualification than, *cv*, and where *conversion-type-id* denotes the
|
| 247 |
type “pointer to function of (`P1`,...,`Pn)` returning `R`”, or the type
|
|
|
|
| 311 |
};
|
| 312 |
String operator + (const String&, const String&);
|
| 313 |
|
| 314 |
void f(void) {
|
| 315 |
const char* p= "one" + "two"; // ill-formed because neither
|
| 316 |
+
// operand has class or enumeration type
|
| 317 |
int I = 1 + 1; // Always evaluates to 2 even if
|
| 318 |
+
// class or enumeration types exist that
|
| 319 |
// would perform the operation.
|
| 320 |
}
|
| 321 |
```
|
| 322 |
|
| 323 |
If either operand has a type that is a class or an enumeration, a
|
|
|
|
| 348 |
type whose cv-unqualified version is `T1` and a right operand of a type
|
| 349 |
whose cv-unqualified version is `T2`, three sets of candidate functions,
|
| 350 |
designated *member candidates*, *non-member candidates* and *built-in
|
| 351 |
candidates*, are constructed as follows:
|
| 352 |
|
| 353 |
+
- If `T1` is a complete class type or a class currently being defined,
|
| 354 |
+
the set of member candidates is the result of the qualified lookup of
|
| 355 |
+
`T1::operator@` ([[over.call.func]]); otherwise, the set of member
|
| 356 |
+
candidates is empty.
|
| 357 |
- The set of non-member candidates is the result of the unqualified
|
| 358 |
lookup of `operator@` in the context of the expression according to
|
| 359 |
the usual rules for name lookup in unqualified function calls (
|
| 360 |
[[basic.lookup.argdep]]) except that all member functions are ignored.
|
| 361 |
However, if no operand has a class type, only those non-member
|
|
|
|
| 370 |
defined in [[over.built]] that, compared to the given operator,
|
| 371 |
- have the same operator name, and
|
| 372 |
- accept the same number of operands, and
|
| 373 |
- accept operand types to which the given operand or operands can be
|
| 374 |
converted according to [[over.best.ics]], and
|
| 375 |
+
- do not have the same parameter-type-list as any non-member candidate
|
| 376 |
+
that is not a function template specialization.
|
| 377 |
|
| 378 |
For the built-in assignment operators, conversions of the left operand
|
| 379 |
are restricted as follows:
|
| 380 |
|
| 381 |
- no temporaries are introduced to hold the left operand, and
|
|
|
|
| 401 |
a + b; // operator+(a,b) chosen over int(a) + int(b)
|
| 402 |
}
|
| 403 |
```
|
| 404 |
|
| 405 |
If a built-in candidate is selected by overload resolution, the operands
|
| 406 |
+
of class type are converted to the types of the corresponding parameters
|
| 407 |
+
of the selected operation function, except that the second standard
|
| 408 |
+
conversion sequence of a user-defined conversion sequence (
|
| 409 |
+
[[over.ics.user]]) is not applied. Then the operator is treated as the
|
| 410 |
corresponding built-in operator and interpreted according to Clause
|
| 411 |
[[expr]].
|
| 412 |
|
| 413 |
+
``` cpp
|
| 414 |
+
struct X {
|
| 415 |
+
operator double();
|
| 416 |
+
};
|
| 417 |
+
|
| 418 |
+
struct Y {
|
| 419 |
+
operator int*();
|
| 420 |
+
};
|
| 421 |
+
|
| 422 |
+
int *a = Y() + 100.0; // error: pointer arithmetic requires integral operand
|
| 423 |
+
int *b = Y() + X(); // error: pointer arithmetic requires integral operand
|
| 424 |
+
```
|
| 425 |
+
|
| 426 |
The second operand of operator `->` is ignored in selecting an
|
| 427 |
`operator->` function, and is not an argument when the `operator->`
|
| 428 |
function is called. When `operator->` returns, the operator `->` is
|
| 429 |
applied to the value returned, with the original second operand.[^7]
|
| 430 |
|
|
|
|
| 469 |
|
| 470 |
Under the conditions specified in [[dcl.init]], as part of a
|
| 471 |
copy-initialization of an object of class type, a user-defined
|
| 472 |
conversion can be invoked to convert an initializer expression to the
|
| 473 |
type of the object being initialized. Overload resolution is used to
|
| 474 |
+
select the user-defined conversion to be invoked. The conversion
|
| 475 |
+
performed for indirect binding to a reference to a possibly cv-qualified
|
| 476 |
+
class type is determined in terms of a corresponding non-reference
|
| 477 |
+
copy-initialization. Assuming that “*cv1* `T`” is the type of the object
|
| 478 |
+
being initialized, with `T` a class type, the candidate functions are
|
| 479 |
+
selected as follows:
|
| 480 |
|
| 481 |
- The converting constructors ([[class.conv.ctor]]) of `T` are
|
| 482 |
candidate functions.
|
| 483 |
- When the type of the initializer expression is a class type “*cv*
|
| 484 |
`S`”, the non-explicit conversion functions of `S` and its base
|
| 485 |
classes are considered. When initializing a temporary to be bound to
|
| 486 |
the first parameter of a constructor that takes a reference to
|
| 487 |
possibly cv-qualified `T` as its first argument, called with a single
|
| 488 |
+
argument in the context of direct-initialization of an object of type
|
| 489 |
+
“*cv2* `T`”, explicit conversion functions are also considered. Those
|
| 490 |
+
that are not hidden within `S` and yield a type whose cv-unqualified
|
| 491 |
+
version is the same type as `T` or is a derived class thereof are
|
| 492 |
+
candidate functions. Conversion functions that return “reference to
|
| 493 |
+
`X`” return lvalues or xvalues, depending on the type of reference, of
|
| 494 |
+
type `X` and are therefore considered to yield `X` for this process of
|
| 495 |
+
selecting candidate functions.
|
| 496 |
|
| 497 |
In both cases, the argument list has one argument, which is the
|
| 498 |
initializer expression. This argument will be compared against the first
|
| 499 |
parameter of the constructors and against the implicit object parameter
|
| 500 |
of the conversion functions.
|
|
|
|
| 537 |
resolution is used to select the conversion function to be invoked.
|
| 538 |
Assuming that “*cv1* `T`” is the underlying type of the reference being
|
| 539 |
initialized, and “*cv* `S`” is the type of the initializer expression,
|
| 540 |
with `S` a class type, the candidate functions are selected as follows:
|
| 541 |
|
| 542 |
+
- The conversion functions of `S` and its base classes are considered.
|
| 543 |
+
Those non-explicit conversion functions that are not hidden within `S`
|
| 544 |
+
and yield type “lvalue reference to *cv2* `T2`” (when initializing an
|
| 545 |
+
lvalue reference or an rvalue reference to function) or “ `T2`” or
|
| 546 |
+
“rvalue reference to `T2`” (when initializing an rvalue reference or
|
| 547 |
+
an lvalue reference to function), where “*cv1* `T`” is
|
| 548 |
+
reference-compatible ([[dcl.init.ref]]) with “*cv2* `T2`”, are
|
| 549 |
+
candidate functions. For direct-initialization, those explicit
|
| 550 |
+
conversion functions that are not hidden within `S` and yield type
|
| 551 |
+
“lvalue reference to *cv2* `T2`” or “*cv2* `T2`” or “rvalue reference
|
| 552 |
+
to *cv2* `T2`,” respectively, where `T2` is the same type as `T` or
|
| 553 |
+
can be converted to type `T` with a qualification conversion (
|
| 554 |
+
[[conv.qual]]), are also candidate functions.
|
| 555 |
|
| 556 |
The argument list has one argument, which is the initializer expression.
|
| 557 |
This argument will be compared against the implicit object parameter of
|
| 558 |
the conversion functions.
|
| 559 |
|
|
|
|
| 653 |
float x = a; // ambiguous: both possibilities require conversions,
|
| 654 |
// and neither is better than the other
|
| 655 |
```
|
| 656 |
|
| 657 |
or, if not that,
|
| 658 |
+
- the context is an initialization by conversion function for direct
|
| 659 |
+
reference binding ([[over.match.ref]]) of a reference to function
|
| 660 |
+
type, the return type of `F1` is the same kind of reference (i.e.
|
| 661 |
+
lvalue or rvalue) as the reference being initialized, and the return
|
| 662 |
+
type of `F2` is not
|
| 663 |
+
``` cpp
|
| 664 |
+
template <class T> struct A {
|
| 665 |
+
operator T&(); // #1
|
| 666 |
+
operator T&&(); // #2
|
| 667 |
+
};
|
| 668 |
+
typedef int Fn();
|
| 669 |
+
A<Fn> a;
|
| 670 |
+
Fn& lf = a; // calls #1
|
| 671 |
+
Fn&& rf = a; // calls #2
|
| 672 |
+
```
|
| 673 |
+
|
| 674 |
+
or, if not that,
|
| 675 |
+
- `F1` is not a function template specialization and `F2` is a function
|
| 676 |
+
template specialization, or, if not that,
|
| 677 |
- `F1` and `F2` are function template specializations, and the function
|
| 678 |
template for `F1` is more specialized than the template for `F2`
|
| 679 |
according to the partial ordering rules described in
|
| 680 |
[[temp.func.order]].
|
| 681 |
|
|
|
|
| 752 |
|
| 753 |
- a *standard conversion sequence* ([[over.ics.scs]]),
|
| 754 |
- a *user-defined conversion sequence* ([[over.ics.user]]), or
|
| 755 |
- an *ellipsis conversion sequence* ([[over.ics.ellipsis]]).
|
| 756 |
|
| 757 |
+
However, if the target is
|
| 758 |
+
|
| 759 |
+
- the first parameter of a constructor or
|
| 760 |
+
- the implicit object parameter of a user-defined conversion function
|
| 761 |
+
|
| 762 |
+
and the constructor or user-defined conversion function is a candidate
|
| 763 |
+
by
|
| 764 |
+
|
| 765 |
+
- [[over.match.ctor]], when the argument is the temporary in the second
|
| 766 |
+
step of a class copy-initialization,
|
| 767 |
+
- [[over.match.copy]], [[over.match.conv]], or [[over.match.ref]] (in
|
| 768 |
+
all cases), or
|
| 769 |
+
- the second phase of [[over.match.list]] when the initializer list has
|
| 770 |
+
exactly one element, and the target is the first parameter of a
|
| 771 |
+
constructor of class `X`, and the conversion is to `X` or reference to
|
| 772 |
+
(possibly cv-qualified) `X`,
|
| 773 |
+
|
| 774 |
+
user-defined conversion sequences are not considered. These rules
|
| 775 |
+
prevent more than one user-defined conversion from being applied during
|
| 776 |
+
overload resolution, thereby avoiding infinite recursion.
|
| 777 |
+
|
| 778 |
+
``` cpp
|
| 779 |
+
struct Y { Y(int); };
|
| 780 |
+
struct A { operator int(); };
|
| 781 |
+
Y y1 = A(); // error: A::operator int() is not a candidate
|
| 782 |
+
|
| 783 |
+
struct X { };
|
| 784 |
+
struct B { operator X(); };
|
| 785 |
+
B b;
|
| 786 |
+
X x({b}); // error: B::operator X() is not a candidate
|
| 787 |
+
```
|
| 788 |
|
| 789 |
For the case where the parameter type is a reference, see
|
| 790 |
[[over.ics.ref]].
|
| 791 |
|
| 792 |
When the parameter type is not a reference, the implicit conversion
|
|
|
|
| 904 |
shall have exact match rank.
|
| 905 |
|
| 906 |
A conversion of an expression of class type to the same class type is
|
| 907 |
given Exact Match rank, and a conversion of an expression of class type
|
| 908 |
to a base class of that type is given Conversion rank, in spite of the
|
| 909 |
+
fact that a constructor (i.e., a user-defined conversion function) is
|
| 910 |
+
called for those cases.
|
| 911 |
|
| 912 |
##### Ellipsis conversion sequences <a id="over.ics.ellipsis">[[over.ics.ellipsis]]</a>
|
| 913 |
|
| 914 |
An ellipsis conversion sequence occurs when an argument in a function
|
| 915 |
call is matched with the ellipsis parameter specification of the
|
|
|
|
| 967 |
an `int` lvalue and finds an exact match with the parameter. If the
|
| 968 |
function is selected by overload resolution, the call will nonetheless
|
| 969 |
be ill-formed because of the prohibition on binding a non-`const` lvalue
|
| 970 |
reference to a bit-field ([[dcl.init.ref]]).
|
| 971 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 972 |
##### List-initialization sequence <a id="over.ics.list">[[over.ics.list]]</a>
|
| 973 |
|
| 974 |
When an argument is an initializer list ([[dcl.init.list]]), it is not
|
| 975 |
an expression and special rules apply for converting it to a parameter
|
| 976 |
type.
|
| 977 |
|
| 978 |
+
If the parameter type is `std::initializer_list<X>` and all the elements
|
| 979 |
+
of the initializer list can be implicitly converted to `X`, the implicit
|
| 980 |
+
conversion sequence is the worst conversion necessary to convert an
|
| 981 |
+
element of the list to `X`, or if the initializer list has no elements,
|
| 982 |
+
the identity conversion. This conversion can be a user-defined
|
| 983 |
+
conversion even in the context of a call to an initializer-list
|
| 984 |
+
constructor.
|
| 985 |
|
| 986 |
``` cpp
|
| 987 |
void f(std::initializer_list<int>);
|
| 988 |
+
f( {} ); // OK: f(initializer_list<int>) identity conversion
|
| 989 |
f( {1,2,3} ); // OK: f(initializer_list<int>) identity conversion
|
| 990 |
f( {'a','b'} ); // OK: f(initializer_list<int>) integral promotion
|
| 991 |
f( {1.0} ); // error: narrowing
|
| 992 |
|
| 993 |
struct A {
|
|
|
|
| 1003 |
typedef int IA[3];
|
| 1004 |
void h(const IA&);
|
| 1005 |
h({ 1, 2, 3 }); // OK: identity conversion
|
| 1006 |
```
|
| 1007 |
|
| 1008 |
+
Otherwise, if the parameter type is “array of `N` `X`”[^12], if the
|
| 1009 |
+
initializer list has exactly `N` elements or if it has fewer than `N`
|
| 1010 |
+
elements and `X` is default-constructible, and if all the elements of
|
| 1011 |
+
the initializer list can be implicitly converted to `X`, the implicit
|
| 1012 |
+
conversion sequence is the worst conversion necessary to convert an
|
| 1013 |
+
element of the list to `X`.
|
| 1014 |
+
|
| 1015 |
Otherwise, if the parameter is a non-aggregate class `X` and overload
|
| 1016 |
resolution per [[over.match.list]] chooses a single best constructor of
|
| 1017 |
`X` to perform the initialization of an object of type `X` from the
|
| 1018 |
argument initializer list, the implicit conversion sequence is a
|
| 1019 |
+
user-defined conversion sequence with the second standard conversion
|
| 1020 |
+
sequence an identity conversion. If multiple constructors are viable but
|
| 1021 |
+
none is better than the others, the implicit conversion sequence is the
|
| 1022 |
+
ambiguous conversion sequence. User-defined conversions are allowed for
|
| 1023 |
+
conversion of the initializer list elements to the constructor parameter
|
| 1024 |
+
types except as noted in [[over.best.ics]].
|
| 1025 |
|
| 1026 |
``` cpp
|
| 1027 |
struct A {
|
| 1028 |
A(std::initializer_list<int>);
|
| 1029 |
};
|
|
|
|
| 1033 |
struct B {
|
| 1034 |
B(int, double);
|
| 1035 |
};
|
| 1036 |
void g(B);
|
| 1037 |
g( {'a', 'b'} ); // OK: g(B(int,double)) user-defined conversion
|
| 1038 |
+
g( {1.0, 1.0} ); // error: narrowing
|
| 1039 |
|
| 1040 |
void f(B);
|
| 1041 |
f( {'a', 'b'} ); // error: ambiguous f(A) or f(B)
|
| 1042 |
|
| 1043 |
struct C {
|
|
|
|
| 1045 |
};
|
| 1046 |
void h(C);
|
| 1047 |
h({"foo"}); // OK: h(C(std::string("foo")))
|
| 1048 |
|
| 1049 |
struct D {
|
| 1050 |
+
D(A, C);
|
| 1051 |
};
|
| 1052 |
void i(D);
|
| 1053 |
i({ {1,2}, {"bar"} }); // OK: i(D(A(std::initializer_list<int>{1,2\),C(std::string("bar"))))}
|
| 1054 |
```
|
| 1055 |
|
| 1056 |
Otherwise, if the parameter has an aggregate type which can be
|
| 1057 |
initialized from the initializer list according to the rules for
|
| 1058 |
aggregate initialization ([[dcl.init.aggr]]), the implicit conversion
|
| 1059 |
+
sequence is a user-defined conversion sequence with the second standard
|
| 1060 |
+
conversion sequence an identity conversion.
|
| 1061 |
|
| 1062 |
``` cpp
|
| 1063 |
struct A {
|
| 1064 |
int m1;
|
| 1065 |
double m2;
|
|
|
|
| 1139 |
sequence is considered to be a subsequence of any non-identity
|
| 1140 |
conversion sequence) or, if not that,
|
| 1141 |
- the rank of `S1` is better than the rank of `S2`, or `S1` and `S2`
|
| 1142 |
have the same rank and are distinguishable by the rules in the
|
| 1143 |
paragraph below, or, if not that,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1144 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and neither
|
| 1145 |
refers to an implicit object parameter of a non-static member
|
| 1146 |
function declared without a *ref-qualifier*, and `S1` binds an
|
| 1147 |
rvalue reference to an rvalue and `S2` binds an lvalue reference.
|
| 1148 |
``` cpp
|
|
|
|
| 1173 |
or, if not that,
|
| 1174 |
- `S1` and `S2` are reference bindings ([[dcl.init.ref]]) and `S1`
|
| 1175 |
binds an lvalue reference to a function lvalue and `S2` binds an
|
| 1176 |
rvalue reference to a function lvalue.
|
| 1177 |
``` cpp
|
| 1178 |
+
int f(void(&)()); // #1
|
| 1179 |
+
int f(void(&&)()); // #2
|
| 1180 |
void g();
|
| 1181 |
+
int i1 = f(g); // calls #1
|
| 1182 |
```
|
| 1183 |
+
|
| 1184 |
+
or, if not that,
|
| 1185 |
+
- `S1`
|
| 1186 |
+
and `S2` differ only in their qualification conversion and yield
|
| 1187 |
+
similar types `T1` and `T2` ([[conv.qual]]), respectively, and the
|
| 1188 |
+
cv-qualification signature of type `T1` is a proper subset of the
|
| 1189 |
+
cv-qualification signature of type `T2`.
|
| 1190 |
+
``` cpp
|
| 1191 |
+
int f(const volatile int *);
|
| 1192 |
+
int f(const int *);
|
| 1193 |
+
int i;
|
| 1194 |
+
int j = f(&i); // calls f(const int*)
|
| 1195 |
+
```
|
| 1196 |
+
|
| 1197 |
+
or, if not that,
|
| 1198 |
- `S1`
|
| 1199 |
and `S2` are reference bindings ([[dcl.init.ref]]), and the types
|
| 1200 |
to which the references refer are the same type except for top-level
|
| 1201 |
cv-qualifiers, and the type to which the reference initialized by
|
| 1202 |
`S2` refers is more cv-qualified than the type to which the
|
|
|
|
| 1220 |
b.f(); // calls X::f()
|
| 1221 |
}
|
| 1222 |
```
|
| 1223 |
- User-defined conversion sequence `U1` is a better conversion sequence
|
| 1224 |
than another user-defined conversion sequence `U2` if they contain the
|
| 1225 |
+
same user-defined conversion function or constructor or they
|
| 1226 |
+
initialize the same class in an aggregate initialization and in either
|
| 1227 |
+
case the second standard conversion sequence of `U1` is better than
|
| 1228 |
+
the second standard conversion sequence of `U2`.
|
| 1229 |
``` cpp
|
| 1230 |
struct A {
|
| 1231 |
operator short();
|
| 1232 |
} a;
|
| 1233 |
int f(int);
|
| 1234 |
int f(float);
|
| 1235 |
int i = f(a); // calls f(int), because short → int is
|
| 1236 |
// better than short → float.
|
| 1237 |
```
|
| 1238 |
- List-initialization sequence `L1` is a better conversion sequence than
|
| 1239 |
+
list-initialization sequence `L2` if
|
| 1240 |
+
- `L1` converts to `std::initializer_list<X>` for some `X` and `L2`
|
| 1241 |
+
does not, or, if not that,
|
| 1242 |
+
- `L1` converts to type “array of `N1` `T`”, `L2` converts to type
|
| 1243 |
+
“array of `N2` `T`”, and `N1` is smaller than `N2`.
|
| 1244 |
|
| 1245 |
Standard conversion sequences are ordered by their ranks: an Exact Match
|
| 1246 |
is a better conversion than a Promotion, which is a better conversion
|
| 1247 |
than a Conversion. Two conversion sequences with the same rank are
|
| 1248 |
indistinguishable unless one of the following rules applies:
|
| 1249 |
|
| 1250 |
- A conversion that does not convert a pointer, a pointer to member, or
|
| 1251 |
`std::nullptr_t` to `bool` is better than one that does.
|
| 1252 |
+
- A conversion that promotes an enumeration whose underlying type is
|
| 1253 |
+
fixed to its underlying type is better than one that promotes to the
|
| 1254 |
+
promoted underlying type, if the two are different.
|
| 1255 |
- If class `B` is derived directly or indirectly from class `A`,
|
| 1256 |
conversion of `B*` to `A*` is better than conversion of `B*` to
|
| 1257 |
`void*`, and conversion of `A*` to `void*` is better than conversion
|
| 1258 |
of `B*` to `void*`.
|
| 1259 |
- If class `B` is derived directly or indirectly from class `A` and
|
|
|
|
| 1267 |
C* pc;
|
| 1268 |
int f(A*);
|
| 1269 |
int f(B*);
|
| 1270 |
int i = f(pc); // calls f(B*)
|
| 1271 |
```
|
| 1272 |
+
- binding of an expression of type `C` to a reference to type `B` is
|
| 1273 |
+
better than binding an expression of type `C` to a reference to type
|
| 1274 |
+
`A`,
|
| 1275 |
- conversion of `A::*` to `B::*` is better than conversion of `A::*`
|
| 1276 |
to `C::*`,
|
| 1277 |
- conversion of `C` to `B` is better than conversion of `C` to `A`,
|
| 1278 |
- conversion of `B*` to `A*` is better than conversion of `C*` to
|
| 1279 |
`A*`,
|
| 1280 |
+
- binding of an expression of type `B` to a reference to type `A` is
|
| 1281 |
+
better than binding an expression of type `C` to a reference to type
|
| 1282 |
+
`A`,
|
| 1283 |
- conversion of `B::*` to `C::*` is better than conversion of `A::*`
|
| 1284 |
to `C::*`, and
|
| 1285 |
- conversion of `B` to `A` is better than conversion of `C` to `A`.
|
| 1286 |
|
| 1287 |
Compared conversion sequences will have different source types only in
|