如何确定隐式类型转换优先级? [英] How is the implicit type conversion priority determined?

查看:126
本文介绍了如何确定隐式类型转换优先级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

class A{
public:
    int val;
    char cval;
    A():val(10),cval('a'){ }
    operator char() const{ return cval; }
    operator int() const{ return val; }
};
int main()
{
    A a;
    cout << a;
}

我在VS 2013中运行代码,输出值为10,如果我注释掉operator int() const{ return val; },则输出值将变为a.

I am running the code in VS 2013, the output value is 10, if I comment out operator int() const{ return val; }, the output value will then become a.

我的问题是,由于intchar都是<<运算符的可能选择,我的意思是编译器如何确定选择哪种隐式类型转换?

My question is how does the compiler determine which implicit type conversion to choose, I mean since both int and char are possible options for the << operator?

推荐答案

是的,这是模棱两可的,但是造成歧义的原因实际上是相当令人惊讶的.并不是编译器无法区分ostream::operator<<(int)operator<<(ostream &, char);后者实际上是模板,而前者则不是,因此,如果匹配程度相同,则将选择第一个,并且两者之间没有歧义.相反,歧义来自ostream的其他成员operator<<重载.

Yes, this is ambiguous, but the cause of the ambiguity is actually rather surprising. It is not that the compiler cannot distinguish between ostream::operator<<(int) and operator<<(ostream &, char); the latter is actually a template while the former is not, so if the matches are equally good the first one will be selected, and there is no ambiguity between those two. Rather, the ambiguity comes from ostream's other member operator<< overloads.

最小化复制

struct A{
    operator char() const{ return 'a'; }
    operator int() const{ return 10; }
};

struct B {
    void operator<< (int) { }
    void operator<< (long) { }
};

int main()
{
    A a;
    B b;
    b << a;
}

问题在于,along的转换可以通过a.operator char()a.operator int()进行,都遵循由整数转换组成的标准转换序列.该标准规定(§13.3.3.1[over.best.ics]/p10,省略了脚注):

The problem is that the conversion of a to long can be via either a.operator char() or a.operator int(), both followed by a standard conversion sequence consisting of an integral conversion. The standard says that (§13.3.3.1 [over.best.ics]/p10, footnote omitted):

如果存在几个不同的转换序列,则每个转换 参数类型的参数,隐式转换序列 与参数关联的被定义为唯一转换 序列指定为模糊转换序列.为了 排序隐式转换序列的目的,如 13.3.3.2,模棱两可的转换序列被视为用户定义的序列,该序列与其他任何序列都没有区别 用户定义的转换顺序. *

If several different sequences of conversions exist that each convert the argument to the parameter type, the implicit conversion sequence associated with the parameter is defined to be the unique conversion sequence designated the ambiguous conversion sequence. For the purpose of ranking implicit conversion sequences as described in 13.3.3.2, the ambiguous conversion sequence is treated as a user-defined sequence that is indistinguishable from any other user-defined conversion sequence. *

由于aint的转换还涉及用户定义的转换序列,因此它与模棱两可的转换序列along毫无区别,在这种情况下上下文第13.3.3节[over.match.best]中没有其他规则可以用来区分这两个重载.因此,该调用是模棱两可的,并且程序格式错误.

Since the conversion of a to int also involves a user-defined conversion sequence, it is indistinguishable from the ambiguous conversion sequence from a to long, and in this context no other rule in §13.3.3 [over.match.best] applies to distinguish the two overloads either. Hence, the call is ambiguous, and the program is ill-formed.

* 标准中的下一个句子是:如果选择使用歧义转换序列的函数作为最佳可行函数,则该调用将格式错误,因为调用中的参数之一是模棱两可的.",这似乎不一定正确,但是在

* The next sentence in the standard says "If a function that uses the ambiguous conversion sequence is selected as the best viable function, the call will be ill-formed because the conversion of one of the arguments in the call is ambiguous.", which doesn't seem necessarily correct, but detailed discussion of this issue is probably better in a separate question.

这篇关于如何确定隐式类型转换优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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