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

前端 未结 6 1112
自闭症患者
自闭症患者 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 13:59

    Perhaps you could use an array of function pointers instead of a switch ?

    #include 
    
    typedef void (*func)(void);
    
    static void f0(void) { printf("%s\n", __FUNCTION__); }
    static void f1(void) { printf("%s\n", __FUNCTION__); }
    static void f2(void) { printf("%s\n", __FUNCTION__); }
    static void f3(void) { printf("%s\n", __FUNCTION__); }
    static void f4(void) { printf("%s\n", __FUNCTION__); }
    static void f5(void) { printf("%s\n", __FUNCTION__); }
    static void f6(void) { printf("%s\n", __FUNCTION__); }
    static void f7(void) { printf("%s\n", __FUNCTION__); }
    
    int main(void)
    {
        const func f[8] = { f0, f1, f2, f3, f4, f5, f6, f7 };
        int i;
    
        for (i = 0; i < 8; ++i)
        {
            f[i]();
        }
        return 0;
    }
    

提交回复
热议问题