Reputation: 195
Consider this code.
struct A {
int i;
};
struct B {
char c;
};
struct C {
double d;
};
void f(A a);
void f(B b);
void f(C c);
void g()
{
f({5});
}
Here I get an ambiguity in f({5});
. But it seems that struct A
's constructor is an exact match for {5}
, while the second one needs an integral promotion, while the last one needs a floating-point conversion.
So why is there an ambiguity?
Upvotes: 10
Views: 219
Reputation: 1
It is because integer can be converted to double as double is a bigger data type. It is known as implicit conversion.
This function call g()
can go to f(C c)
as well as f(A a)
.
Upvotes: -2
Reputation: 170044
Even if the first conversion in the sequence is of a worse rank, both conversion sequences end up being user defined conversions, since they both convert to a user defined type.
[over.ics.user]
1 A user-defined conversion sequence consists of an initial standard conversion sequence followed by a user-defined conversion ([class.conv]) followed by a second standard conversion sequence.
A user defined conversion anywhere in the implicit conversion sequence gives the entire sequence a "user defined conversion" rank. So the two conversion sequences are in fact of the same rank, and as such neither is better than the other.
The function call is ambiguous on account of that.
Upvotes: 8