EDG / Comeau和MSVC允许代码,而GCC 4.4.4,
一个C ++委员会成员回答这个问题(最初):
这不明确; A(const A&)构造函数比A(const char *)构造函数更好。 const A& 参数直接绑定到转换函数的结果,因此转换序列被认为是是用户定义的转换,后跟通过标识转换(13.3.3.1.4p1)。 const char * 参数是一个用户定义的转换,后面是一个限定条件的转换,因此更糟。
然后,他跟着这个。
其实我错了。虽然它是真的,用户定义的转换序列中的第二个转换序列是一个tiebreaker,看起来更多的密切在13.3.3.2p3, next-to-last bullet,显示这个 tiebreaker仅适用于两个序列包含相同的用户定义的转换序列,而不是这种情况例。 因为一个构造函数的转换序列使用B :: operator A()和其他使用b :: operator char *(),两个$ b之间没有tiebreak $ b用户定义的转换序列和他们是不明确的。
我的问题是这个。 $ b
13.3.3.2 p3说明
的两个隐式转换序列
根据我的理解,关键字是关键字,它们是不可区分的转换序列,除非遵循规则之一。 以下规则之一。 这并不意味着说明相同的转换序列的项目符号覆盖所有上面的。我认为S1的排名比S2的排名更好b b b
解决方案是,预期结果是我对条款13.3.3.2的最佳解释的Ambiguity
将类型'B'的参数'v'的'A'的重载构造函数需要用户定义的转换。这两个序列都是CONVERSION排名。
我的解释是,$ 13.3.3.2以下的引用适用
...]用户定义的转换序列 U1是一个更好的转换序列比另一个用户定义的转换序列U2 如果它们包含相同的用户定义转换函数或构造函数和,如果U1的第二标准转换序列比U2的第二标准转换序列更好的。
这两个类都在类'B'中调用不同的转换函数。因此,我认为第一个条件本身不满足,因此预期的结果是歧义,因为任何一个转换序列比另一个更好。
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.
更多推荐
这应该是模糊还是不? (隐式转换)
发布评论