Implicit conversion sequences (C++ only)
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.
Standard conversion sequences
- Exact match: This rank includes the following conversions:
- Identity conversions
- Lvalue-to-rvalue conversions
- Array-to-pointer conversions
- Qualification conversions
- Promotion: This rank includes integral and floating point promotions.
- Conversion: This rank includes the following conversions:
- Integral and floating-point conversions
- Floating-integral conversions
- Pointer conversions
- Pointer-to-member conversions
- Boolean conversions
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.
User-defined conversion sequences
- A standard conversion sequence
- A user-defined conversion
- A second standard conversion sequence
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.
Ellipsis conversion sequences
An ellipsis conversion sequence occurs when the compiler matches an argument in a function call with a corresponding ellipsis parameter.
Ranking implicit conversion sequences
Ranking standard conversion sequencesS1 and S2 are
two standard conversion sequences. The compiler checks whether S1 and S2 satisfy
the following conditions in sequence. If one of the conditions is
satisfied, S1 is a better standard conversion sequence
than S2.S2involves a qualification conversion, butS1does not involve qualification conversions. See Example 1.- The rank of
S1is higher than the rank ofS2. See Example 2. - Both
S1andS2involve qualification conversions.T1is the target type ofS1, andT2ofS2.T2is more cv-qualified thanT1. See Example 3. 
S1andS2are reference bindings to an rvalue, and neither of them refers to the implicit object parameter of a nonstatic member function.S1binds an rvalue reference andS2binds an lvalue reference. See Example 4.

S1andS2are reference bindings.S1binds an lvalue reference to a function lvalue, andS2binds an rvalue reference to a function lvalue. See Example 5.
S1andS2are reference bindings.T1is the target type referred byS1, andT2byS2.T1andT2differ only in top-level cv-qualifiers whereT2is more cv-qualified thanT1. See Example 6.
S1 and S2 have
the same rank, S1 is a better standard conversion
sequence than S2 if one of the following conditions
is satisfied:S1converts a pointer, a pointer to member, or a null pointer, andS2does not. See Example 7.- Class
Ais a parent class of classB.S1is a conversion fromB*toA*, andS2is a conversion fromB*tovoid*; orS1is a conversion fromA*tovoid*, andS2is a conversion fromB*tovoid*. See Example 8. - Class
Ais a parent class of classB, and classBis a parent class of classC. One of the following conditions is satisfied:S1is a conversion fromC*toB*, andS2is a conversion fromC*toA*.S1binds an expression of typeCto a reference of typeB&, andS2binds an expression of typeCto a reference of typeA&.S1is a conversion fromA::*toB::*, andS2is a conversion fromA::*toC::*.S1is a conversion fromCtoB, andS2is a conversion fromCtoA.S1is a conversion fromB*toA*, andS2is a conversion fromC*toA*. See Example 9.S1binds an expression of typeBto typeA&, andS2binds an expression of typeCto typeA&.S1is a conversion fromB::*toC::*, andS2is a conversion fromA::*toC::*.S1is a conversion fromBtoA, andS2is a conversion fromCtoA.
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.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. 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.
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. 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.
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.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.//
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.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*.U1 and U2 are
two user-defined conversion sequences. U1 and U2 use
the same user-defined conversion function, constructor, or aggregate
initialization. U1 is a better user-defined conversion
sequence than U2 if the second standard conversion
sequence of U1 is better than that of U2.
See 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.