Consider the following code snippet:
class A;
class B {
public:
B(){}
B(A&) // conversion constructor that takes cv-unqualified A
{
cout << "called B's conversion constructor" << endl;
}
};
class A {
public:
operator B() const // conversion operator that takes cv-qualified A
{
cout << "called A's conversion operator" << endl;
return B();
}
};
int main()
{
B b = A(); // who gets called here?
return 0;
}
According to this question, the conversion sequence with the least cv-qualified form wins (13.3.3.2/3 in specification):
Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if [...] S1 and S2 are reference bindings (8.5.3), and the types to which the references refer are the same type except for top-level cv-qualifiers, and the type to which the reference initialized by S2 refers is more cv-qualified than the type to which the reference initialized by S1 refers.
Yet in the above snippet, the conversion operator always gets picked, regardless of A being cv-qualified or not in both of the functions. The only exception is that when both the constructor and the operator are cv-qualified, the compiler complains about ambiguity in choosing the conversion sequence, while both cv-unqualified case does not (why?).
So the question is:
- Why the conversion operator always gets picked in this case?
- Why did both cv-qualified causes an ambiguity, while both cv-unqualified does not?