这应该是模糊还是不? (隐式转换) [英] Should this be ambiguous or not? (implicit casts)

查看:147
本文介绍了这应该是模糊还是不? (隐式转换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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和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.

这篇关于这应该是模糊还是不? (隐式转换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆