Consider the following example:
struct m
{
m(int ) { }
};
class q
{
public:
int i;
q(int i)
{
this->i = i;
}
operator double()
{
return (double) i;
}
operator m()
{
return (m)i;
}
};
int main()
{
q x(1);
(m)x; // error: ambiguous
}
It fails when I call the casting: ambiguous conversion from q to m. Why so? I really don't get it. I explicitly added a casting operator for m! Why should that be ambiguous? If I remove the cast to double, however, this works. Why does that matter? How can I do what I want to do?
Consider the possible paths that the compiler can take when you do:
m temp__(x);
There are three relevant constructors:
m(int ); // (A)
m(const m& ); // (B)
m(m&& ); // (C)
We could call (A) via x --> double --> int, that is a user-defined conversion sequence.
We could call (B) or (C) via x --> m, that is also a user-defined conversion sequence.
A user-defined conversion sequence can only be preferences to another if they end up calling the same constructor. But in this case, they don't - so there's no way for the compiler to prefer one to the other. Hence, ambiguous.
Your alternatives would be to
q inherit from m - the derived-to-base conversion would be preferred to the user-defined conversion sequence; orexplicit - in which case there would only be one viable candidate to begin with, so there would be no ambiguity.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With