GCC 4.4: Avoid range check on switch/case statement in gcc?

前端 未结 6 1111
自闭症患者
自闭症患者 2021-02-05 13:11

This is only an issue on GCC versions prior to 4.4, this was fixed in GCC 4.5.

Is it possible to tell the compiler the variable used in a switch fits within the provided

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 14:00

    Have you tried declaring the switch variable as a bitfield?

    struct Container {
      uint16_t a:3;
      uint16_t unused:13;
    };
    
    struct Container cont;
    
    cont.a = 5;  /* assign some value */
    switch( cont.a ) {
    ...
    }
    

    Hope this works!

提交回复
热议问题