An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration.
The compiler tries to determine an implicit conversion sequence for each argument. It then categorizes each implicit conversion sequence in one of three categories and ranks them depending on the category. The compiler does not allow any program in which it cannot find an implicit conversion sequence for an argument.
The compiler ranks a standard conversion sequence by its lowest-ranked standard conversion. For example, if a standard conversion sequence has a floating-point conversion, then that sequence has conversion rank.
A user-defined conversion sequence A is better than a user-defined conversion sequence B if both A and B have the same user-defined conversion function or constructor, and the second standard conversion sequence of A is better than the second standard conversion sequence of B.
An ellipsis conversion sequence occurs when the compiler matches an argument in a function call with a corresponding ellipsis parameter.
Example 1
void f(int*); // #1 function
void f(const int*); // #2 function
void test() {
// The compiler calls #1 function
f(static_cast<int*>(0));
}
In this example, for the call of f(static_cast<int*>(0)),
the standard conversion sequence S1 of the f(int*) function
is from int* to int*. The standard
conversion sequence S2 of the f(const int*) function
is from int* to const int*. S2 involves
a qualification conversion, but S1 does not, so S1 is
a better standard conversion sequence than S2.Example 2
struct A { };
struct B : A { };
void f(const B*); // #1 function
void f(A*); // #2 function
void test() {
// The compiler calls #1 function
f(static_cast<B*>(0));
}
struct A1 *g(int); // #3 function
struct A2 *g(short); // #4 function
void test2() {
// The compiler calls #4 function
A2* a2 = g(static_cast<short>(0));
// The compiler calls #3 function
A1* a1 = g('\0');
}
In this example, for the call of f(static_cast<B*>(0)),
the standard conversion sequence of the f(const B*) function
is an exact match, and the standard conversion sequence of the f(A*) function
is a conversion. The rank of exact match is higher than that of conversion,
so f(const B*) is chosen by overload resolution.
Similarly, for the call of g(static_cast<short>(0)),
the standard conversion sequence of the g(short) function
is an exact match, and the standard conversion sequence of the g(int) function
is a promotion. The g(short) function is called because
the rank of exact match is higher than that of promotion. For the
call of g('\0'), the standard conversion sequence
of the g(short) function is a conversion, and the
standard conversion sequence of the g(int) function
is a promotion, g(int) is called in this case because
the rank of promotion is higher than that of conversion. Example 3
struct A { };
struct B : A { };
void g(const A*); // #1 function
void g(const volatile A*); // #2 function
void test2() {
// The compiler calls #1 function
g(static_cast<B*>(0));
}
In this example, for the call of g(static_cast<B*>(0)),
the standard conversion sequence S1 of the g(const
A*) function is from B* to const
A*. The standard conversion sequence S2 of
the g(const volatile A*) function is from B* to const
volatile A*. Both S1 and S2 involve
qualification conversions, and const volatile
A* is more cv-qualified than const
A*, so S1 is a better standard conversion
sequence than S2.Example 4
double f1();
int g(const double&&); // #1 function
int g(const double&); // #2 function
// The compiler calls #1 function
int i = g(f1());
struct A {
int operator+(int);
};
int operator+(A &&, int);
A &&f2();
void test() {
f2() + 0; // error
}
In this example, for the call of g(f1()),
the standard conversion sequence of the g(const double&) function
binds an lvalue reference, and the standard conversion sequence for g(const
double&&) binds an rvalue reference. Neither of these
two standard conversion sequences refers to the implicit object parameter
of a nonstatic member function. The g(const double&&) function
is called because its standard conversion sequence is a better one.
For the expression f2() + 0, the class member candidate
involves a reference binding of the implicit object parameter of a
nonstatic member function and hence cannot be ordered with respect
to the namespace scope candidate. Example 5
double f();
int g(double(&&)()); // #1 function
int g(double(&)()); // #2 function
// The compiler calls #2 function
int i = g(f)
In this example, for the call of g(f),
the standard conversion sequence of the g(double(&)()) function
binds an lvalue reference to a function lvalue, and the standard conversion
sequence of the g(double(&&)()) function
binds an rvalue reference to a function lvalue. The g(double(&)()) function
is called because its standard conversion sequence is a better one.Example 6
void f(A&); // #1 function
void f(const A&); // #2 function
void test() {
A a;
// The compiler calls #1 function
f(a);
}
In this example, for the call of f(a),
the standard conversion sequence S1 of the f(A&) function
binds an lvalue reference A& to a,
and the standard conversion sequence S2 of the f(const
A&) function binds a const lvalue reference const
A& to a. Because const
A and A are the same except for top-level
cv-qualifers, and const A is more cv-qualified than A, S1 is
a better standard conversion sequence than S2.Example 7
void f(void*); // #1 function
void f(bool); // #2 function
void test() {
// The compiler calls #1 function
f(static_cast<int*>(0));
}
In this example, for the call of f(static_cast<int*>(0)),
the standard conversion sequence S1 of the f(void*) function
is from int* to void*, and the standard
conversion sequence S2 of the f(bool) function
is from int* to bool. S1 and S2 have
the same rank. However, because S1 does not convert
a pointer, a pointer to member, or a null pointer to a bool,
and S2 converts a pointer to a bool, S1 is
a better standard conversion sequence than S2.Example 8
//
void f(void*); // #1 function
void f(struct A*); // #2 function
struct A { };
struct B : A { };
void test() {
// The compiler calls #2 function
f(static_cast<B*>(0));
}
In this example, for the call of f(static_cast<B*>(0)),
the standard conversion sequence of the f(void*) function
is from B* to void*, and the standard
conversion sequence of the f(struct A*) is from B* to A*.
The f(struct A*) is called because its standard conversion
sequence is a better one.Example 9
void f(struct A *);
struct A { };
struct B : A { };
struct C : B { };
struct S {
operator B*();
operator C*();
}
void test() {
// calls S::operator B*()
f(S());
}
In this example, for the call of f(S()),
the standard conversion sequence is from S() to A*,
and the structure S has two conversion operators.
The operator function operator B* () is called, because
the conversion from B* to A* is
better than from C* to A*.Example 10
void f(void*); // #1 function
void f(bool); // #2 function
struct A {
operator int*();
}
void test() {
// The compiler calls #1 function
f(A());
}
In this example, for the call of f(A()),
the user-defined conversion sequence U1 of the f(void*) function
is from A to void*. The user-defined
conversion sequence U2 of the f(bool) function
is from A to bool. U1 and U2 use
the same user-defined conversion from A to int*.
The standard conversion sequence from int* to void* is
better than the standard conversion sequence from int* to bool,
so U1 is a better user-defined conversion sequence
than U2.