gpt4 book ai didi

c++ - 哪些提升类型用于 switch-case 表达式比较?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:28:55 26 4
gpt4 key购买 nike

以下程序在使用不同的编译器编译时打印“unknown”。为什么会这样?

#include "stdio.h"

const char OPTION = (char)(unsigned char)253;

int main(int argc, char* argv[])
{
unsigned char c = 253;
switch (c)
{
case OPTION:
printf("option\n");
break;
default:
printf("unknown\n");
break;
}

return 0;
}

在查看 C++ 标准 (N3690 2013-05-05) 时,我看到了 switch 的子句:

6.4.2 The switch statement

2 The condition shall be of integral type, enumeration type, or class type. If of class type, the condition is contextually implicitly converted (Clause 4) to an integral or enumeration type. Integral promotions are performed. Any statement within the switch statement can be labeled with one or more case labels as follows:

case constant-expression :  

where the constant-expression shall be a converted constant expression (5.19) of the promoted type of the switch condition. No two of the case constants in the same switch shall have the same value after conversion to the promoted type of the switch condition.

引用的转换子句:

4 Standard conversions

2 [ Note: expressions with a given type will be implicitly converted to other types in several contexts:
[...]
— When used in the expression of a switch statement. The destination type is integral (6.4).
[...]
—end note ]

变量c的类型是unsigned char,是整型。所以不需要促销!?

如果提升的类型是 unsigned char,我会期望像 c == (unsigned char)OPTION 这样的比较会产生 true。如果提升的类型是 int,我会期待像 (int)c == (int)OPTION) 这样的比较,它显然会产生 false。

我的问题是:上述程序中使用的提升类型是什么? C和C++标准中有哪些相关条款?

最佳答案

涉及哪些类型?

提升的类型将是 int,如以下部分所述:

4.5p1 Integral promotions [conv.prom]

A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.


为什么代码在不同平台上表现不同?

它的实现定义了 char 是有符号的还是无符号的,可以在标准的以下部分中阅读;

3.9.1p1 Fundamental types [basic.fundamental]

It is implementation-defined whether a char can hold negative values. Characters can be explicitly declared signed or unsigned.

...

In any particular implementation, a plain char object can take on either the same values as a signed char or an unsigned char; which one is implementation-defined.


这有什么关系?

前面引用的部分意味着在下一行中转换为 char 不必产生 253 的值。

const char OPTION = (char)(unsigned char)253;

如果 char 能够在 char 为 8 位的平台上保存负值,253 将不适合并且OPTION 的值很可能在初始化后为 -3


换句话说...

在整体提升之后,您帖子中的开关在语义上等同于下面的 if-else-statement,因为我们有一个条件和一个默认情况。

unsigned char c = 253;

// .---------.-------------------- integral promotion
// v v
if ((int)c == (int)OPTION) {
printf ("OPTION\n");
} else {
printf ("DEFAULT\n");
}

根据底层实现,OPTION 可能等于 253-3;产生您描述的行为。


Note: All standard quotations in this post are from the final C++11 Standard (draft) n3337.

关于c++ - 哪些提升类型用于 switch-case 表达式比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24079001/

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