struct A
{
A(const A& src);
A(const char* src);
};
struct B
{
operator A();
operator char*();
};
void test()
{
B v;
A s(v);
}
EDG/Comeau and MSVC allows the code while GCC 4.4.4, CLANG and BCC
reject it as ambiguous.
A C++ committee member replied with this (initially):
It's not ambiguous; the A(const A&)
constructor is better than the A(const
char*) constructor. The const A&
parameter binds directly to the result
of the conversion function, so the
conversion sequence is considered to
be a user-defined conversion followed
by an identity conversion
(13.3.3.1.4p1). The const char*
parameter is a user-defined conversion
followed by a qualification
conversion, so it's worse.
Then, he followed up with this.
Actually, I was wrong. While it is
true that the second conversion
sequence in a user-defined conversion
sequence is a tiebreaker, looking more
closely at 13.3.3.2p3, the
next-to-last bullet, reveals that this
tiebreaker only applies if the two
sequences contain the same
user-defined conversion sequence, and
that is not the case in this example.
Because one constructor's conversion
sequence uses B::operator A() and the
other uses b::operator char*(),
there's no tiebreaker between the two
user-defined conversion sequences and
they are ambiguous.
My question is this.
13.3.3.2 p3 states that,
Two implicit conversion sequences of
the same form are indistinguishable
conversion sequences unless one of the
following rules apply.
From my understanding, the keywords are "one of the following rules".
It doesn't mean that the bullet which states "same conversion sequence"
overrides all the ones above. I would've thought "the rank of S1 is better
than the rank of S2" would be applicable instead?
Yes, the expected result is Ambiguity to the best of my interpretation of Clause 13.3.3.2
Matching argument 'v' of type 'B' to the parameters of either of the overloaded constructors of 'A' requires a user defined conversion. There both the sequences are of CONVERSION rank.
My interpretation is that the following quote from $13.3.3.2 applies
[...]User-defined conversion sequence
U1 is a better conversion sequence
than another user-defined conversion
sequence U2 if they contain the same
user-defined conversion function or
constructor and if the second standard
conversion sequence of U1 is better
than the second standard conversion
sequence of U2.
Both of these invoke different conversion functions in class 'B'. Therefore, I think the first condition itself is not satisfied and hence the expected result is Ambiguity as nither of the conversion sequences is better than the other.
Disclaimer : the standard is really complex on these parts, so I may be completely wrong in my understanding.
Standard definition of the best viable function (13.3.3) :
Given these definitions, a viable
function F1 is defined to be a better
function than another viable function
F2 if for all arguments i, ICSi(F1) is
not a worse conversion sequence than
ICSi(F2), and then
[...]
- the context is an initialization by user-defined conversion (see 8.5,
13.3.1.5, and 13.3.1.6) and the standard conversion sequence from the
return type of F1 to the destination
type (i.e., the type of the entity
being initialized) is a better
conversion sequence than the standard
conversion sequence from the return
type of F2 to the destination type.
If I understand correctly, the type of the object being constructed has its importance here, and that would make A::A(const A &)
a better candidate.
Refer to Johannes comments to see why this answer is incorrect : this is indeed ambiguous for the reason pointed out by Chubsdad.