switch-case如何在“case”中处理常量的值? [英] How does switch-case handle the value of the constant in a "case"?

查看:479
本文介绍了switch-case如何在“case”中处理常量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有

  typedef unsigned char BYTE; 
#define CPUTYPE_INVALID((BYTE)-1)

  BYTE m_CPUTYPE; 
m_CPUTYPE = 0xff;

switch(m_CPUTYPE){
case 255:
cout< hit 1;

case -1:
cout<< hit 2;

case CPUTYPE_INVALID:
cout<< hit 3;
break;

默认值:
cout<< 无命中;
break;
}

结果会是什么?

解决方案


警告



您的代码可能有错误,如果我们正在处理一个平台,其中 unsigned char 由8位组成







标准说什么?



在标准中规定,两个常量常量转换后具有相同的值 $ c> switch 语句 [stmt .switch]


条件应为整数类型,枚举类型或类类型。如果是类类型,则条件被上下文隐式转换(第4条)为一个整数或枚举类型。执行积分促销。 switch 语句中的任何语句可以标记一个或多个案例标签,如下所示:

  case constant-expression:

其中teh 常量表达式应为转换条件的提升类型的转换常数表达式(5.19)。




在同一个开关中的两个常量不会有相同的值。

这意味着如果整型促销从 m_CPUTYPE 类型的积分类型产出,处理 static_cast< int> ((BYTE)-1) static_cast< int> (255)作为相同的值..该代码段是错误的。






>

整体提升是一种将整数类型提升为另一种类型的方法,



<$> <$> <$> <$> c> 整合促销 [conv.prom]


除了整数转换等级(4.13)小于等于 int 的整数类型的bool,char16_t,char32_t或wchar_t的prvalue可以转换为prvalue int 如果 int 可以表示源类型的所有值;否则,源prvalue可以转换为 unsigned int 类型的prvalue。



标准中规定 unsigned char 有少于 rank int ,并且这种开关条件的类型将是 int ,这进一步意味着; static_cast< int> (-1)!= static_cast< int> (255)



但是,在大多数平台上, unsigned char 的最大值为 255 ,这意味着(BYTE)-1)将生成 unsigned char c $ c> 255 。这意味着 255 == CPUTYPE_INVALID

  switch(m_CPUTYPE){ 
case 255://(A)
cout<<hit 1;
case -1:
cout<<hit 2;
case CPUTYPE_INVALID://(B),与(A)相同的值
cout<<hit 3;
break;
default:
cout<<no hit;
break;
}

您有两个大小写常数,相同的值:您的代码格式错误


Let's say we have

typedef unsigned char       BYTE;
#define     CPUTYPE_INVALID  ((BYTE)-1)

Which case will the switch statement hit?

BYTE m_CPUTYPE;
m_CPUTYPE = 0xff;

switch (m_CPUTYPE) {
    case 255:
        cout << "hit 1";

    case -1:
        cout << "hit 2";

    case CPUTYPE_INVALID:
        cout << "hit 3";
        break;

    default:
        cout << "no hit";
        break;
}

What will be the outcome then? And please elaborate.

解决方案

WARNING

Your code is potentially ill-formed, and if we are dealing with a platform where unsigned char consists of 8 bits (which is most often the case), it definitely is.


What does the standard say?

It's is stated in the standard that two case constants shall not have the same value after conversion to integral type used by the switch-condition.

6.4.2p2 The switch statement [stmt.switch]

The condition shall be of intergral 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 teh 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.

This means that if the integral type yield by the integral promotion from the type of m_CPUTYPE, treats static_cast<int> ((BYTE)-1) and static_cast<int> (255) as being the same value.. the snippet is ill-formed.


So, am I safe?

Integral promotion is the method of which an integral type is promoted to another type,if that is necessary to easy implementation and logic when dealing with two integral values that are potentially of different types.

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

It's stated in the standard that unsigned char has less rank than int, and with this the type of the switch condition will be int, which further means; static_cast<int> (-1) != static_cast<int> (255).

But, on most platforms the maximum value of a unsigned char is 255 which means that (BYTE)-1) will yield a unsigned char with the value of 255. This means that 255 == CPUTYPE_INVALID.

switch (m_CPUTYPE) {
case 255:               // (A)
  cout<<"hit 1";
case -1:
  cout<<"hit 2";
case CPUTYPE_INVALID:   // (B), same value as (A)
  cout<<"hit 3";
  break;
default:
  cout<<"no hit";
  break;
}

You have two case constants which after conversions yields the same value: your code is ill-formed.

这篇关于switch-case如何在“case”中处理常量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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