简体   繁体   中英

Casting enum variable to enum type

I have encountered a code where enum variable is casted to enum type. I am unable to understand the use of such casting. For E. g.

typedef enum{x,y,z} en;

void main(){
  en const v;
  switch(v){
  case (en)y : {
    }
    break;
  case (en)x: {
    }
    break;
  default : {
    foo();
    }
    break;
  }
}

What is the use of casting x or y to enum type that is (en)x in case label?

Such code is symptomatic of sheer paranoia, that's all. In fact, it's possibly harmful since in forcing a cast, you're hinting to the compiler that you know what you're doing so it may not warn you of an errant case label.

switch ing directly on an enum is perfectly valid, as enum s are essentially integral types. You can safely drop the (en) casts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM