C ++:三元运算符(条件运算符)及其隐式类型转换规则 [英] C++ : Ternary Operator (Conditional Operator) and its Implicit Type Conversion Rules

查看:86
本文介绍了C ++:三元运算符(条件运算符)及其隐式类型转换规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在用于三元运算符的参数的隐式类型转换的规则?

Are there rules for implicit type conversion for arguments of the ternary operator?

三元运算符始终需要返回相同的类型.此类型仅由第二个和第三个参数( 1st?2nd:3rd )确定,因此两个参数都转换为该类型.如何确定这种类型?

The ternary operator always needs to return the same type. This type is determined solely by the second and third argument (1st ? 2nd : 3rd), therefore both arguments are converted to this type. How is this type determined?

更具体地说,我测试了一个示例:

To be more specific, I tested an example:

class pointclass
{
    pointclass();

    pointclass( int i );    // (pointclass)(int)
    operator bool() const;  // (bool)(pointclass)
};

我有一个类( pointclass ),该类可以实现从 int pointclass 的隐式转换以及从 pointclass 的隐式转换code>到 bool .

I have a class (pointclass), which enables implicit conversion from int to pointclass and implicit conversion from pointclass to bool.

int i;
pointclass p;
bool b;

b ? p : i;  // (bool) ? (int)(bool)(pointclass) : (int)
b ? i : p;  // (bool) ? (int) : (int)(bool)(pointclass)

使用三元运算符,比较 pointclass int .编译器使用从 pointclass bool 的隐式转换,然后使用从 bool int 的标准转换.不管我是否交换第二个和第三个参数,都已完成.为什么不将 int 转换为 pointclass ?

Using the ternary operator, I compare pointclass and int. The compiler uses implicit conversion from pointclass to bool and then the standard conversion from bool to int. This is done, no matter if I exchange 2nd and 3rd arguments. Why does it not convert int to pointclass?

使用比较运算符更加简单:

Using a comparison operator is much more straightforward:

p == i;     // (pointclass) == (pointclass)(int)
i == p;     // (int) == (int)(bool)(pointclass)

参数的类型仅由第一个参数确定.

The type of the arguments is simply determined by the first argument.

但是我不理解三元运算符的类型转换规则.对我来说,似乎就像使用大多数转化方式一样.

But I don't understand the type conversion rules of the ternary operator. For me it seems just like using the way of most conversions.

推荐答案

引用 MSDN :

条件表达式具有从右到左的关联性.首先操作数必须是整数或指针类型.以下规则适用到第二和第三操作数:

Conditional expressions have right-to-left associativity. The first operand must be of integral or pointer type. The following rules apply to the second and third operands:

如果两个操作数属于同一类型,则结果为该类型.

If both operands are of the same type, the result is of that type.

如果两个操作数均为算术或枚举类型,则通常的算术转换(在算术转换中介绍)为进行了将它们转换为通用类型的操作.

If both operands are of arithmetic or enumeration types, the usual arithmetic conversions (covered in Arithmetic Conversions) are performed to convert them to a common type.

如果两个操作数都是指针类型,或者一个是指针类型,而另一个是求值为0的常量表达式,则指针进行转换以将它们转换为通用类型.

If both operands are of pointer types or if one is a pointer type and the other is a constant expression that evaluates to 0, pointer conversions are performed to convert them to a common type.

如果两个操作数均为引用类型,则将执行引用转换以将它们转换为通用类型.

If both operands are of reference types, reference conversions are performed to convert them to a common type.

如果两个操作数的类型均为void,则常见类型为void.

If both operands are of type void, the common type is type void.

如果两个操作数属于同一用户定义类型,则常见类型为该类型.

If both operands are of the same user-defined type, the common type is that type.

如果操作数具有不同的类型,并且至少一个操作数具有用户定义的类型,则使用语言规则来确定常见的类型.(请参阅下面的警告.)

If the operands have different types and at least one of the operands has user-defined type then the language rules are used to determine the common type. (See warning below.)

基本上,发生的是C ++编译器为第二个和第三个操作数寻找公用类型.如果可以找到,那就是结果类型.如果找不到,则会导致编译时错误.

Basically what happens is that C++ compiler looks for common type for second and third operand. If it can find it, that's result type. If it can't find it, it results in a compilation time error.

如果您想查看标准位置,则可以查看规则最新标准的工作草案中,第5.16页(第129页).

If you want to see standard position, you can see the rules in working draft for newest standard, 5.16 (page 129).

在不将int转换为pointclass的情况下-一般规则是,您始终沿层次结构前进,而不是向上-想象更高级的类层次结构;在某个地方可能有数​​十种方法可以将两种类型转换为其他类型,但这确实是您想要的吗?而且,确定使用哪个类别可能是不可能的.因此,我们垂头丧气.

As of not converting int to pointclass - general rule is that you always go down the hierarchy, not up - imagine more advanced class hierarchy; somewhere up there could be dozens of ways to convert both types up to some other class, but is that really what you want? Moreover, determining which class to use could be impossible. Therefore, we downcast.

这篇关于C ++:三元运算符(条件运算符)及其隐式类型转换规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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