gpt4 book ai didi

c++ - 是什么导致使用三元而不是短裤将这个有符号整型转换为无符号整型?

转载 作者:行者123 更新时间:2023-12-02 02:03:12 25 4
gpt4 key购买 nike

在列表初始化中使用三元运算符时,是什么导致int隐式转换为unsigned int(对于long long也类似)但不是将 short 转换为 unsigned Short(对于 char 也类似)。

具体来说,我很惊讶 i32v2 函数编译得很好,而其他函数则不然:

unsigned short f16(unsigned short x);
unsigned int f32(unsigned int x);

void i16(short value) {
unsigned short encoded{value}; // narrowing, makes sense
}

void i32(int value) {
unsigned int encoded{value}; // narrowing, makes sense
}

void i16v2(short value) {
unsigned short encoded{false ? value : f16(value)}; // narrowing, makes sense
}

void i32v2(int value) {
unsigned int encoded{false ? value : f32(value)}; // not narrowing, huh?
}

此处的完整示例:https://godbolt.org/z/fVTcrr

我猜测三元运算符隐式地将 int 转换为 unsigned int 但我不明白为什么它无法将 short 转换为 无符号短类似。

我希望,如果int可以实现,那么三元运算符也应该能够将任何其他signed类型转换为unsigned 如果可能的话:

If the destination type is unsigned, the resulting value is the smallest unsigned value equal to the source value modulo 2n where n is the number of bits used to represent the destination type.

( https://en.cppreference.com/w/cpp/language/implicit_conversion )

有人可以解释一下这种行为吗?如果可能的话,请引用标准或适用的 cppreference 页面?

最佳答案

标准说(引用最新草案):

[expr.cond]

Lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard conversions are performed on the second and third operands. After those conversions, one of the following shall hold:

  • The second and third operands have the same type; ... [does not apply]

  • The second and third operands have arithmetic [applies] or enumeration type; the usual arithmetic conversions are performed to bring them to a common type, and the result is of that type.

  • ...

[expr.arith.conv]

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

  • If either operand is of scoped enumeration type ... [does not apply]

  • If either operand is of type long double ... [does not apply]

  • Otherwise, if either operand is double ... [does not apply]

  • Otherwise, if either operand is float ... [does not apply]

  • Otherwise, the integral promotions ([conv.prom]) shall be performed on both operands. Then the following rules shall be applied to the promoted operands:

...

[conv.prom]

A prvalue of an integer type other than bool, char16_­t, char32_­t, or wchar_­t [applies] whose integer conversion rank ([conv.rank]) is less than the rank of int [applies] can be converted to a prvalue of type int if int can represent all the values of the source type [evidently applies1]; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.

These conversions are called integral promotions.

所以,对于 i16v2 来说,第二个和第三个操作数是 shortunsigned short 。显然1都提升为 int在您的系统上,以及 int然后使用条件运算符的结果来初始化 unsigned short .

i32v2为例,不适用促销,普通类型为 intunsigned intunsigned int .

1 我说显然,因为从技术上讲,unsigned short可以晋升为unsigned int在某些大小相同的奇异系统上,在这种情况下 int无法代表 unsigned short 的所有值。您观察到的结果表明您的系统并非如此,这是可以预料的。

关于c++ - 是什么导致使用三元而不是短裤将这个有符号整型转换为无符号整型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60189901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com