Partially disable pedantic warnings in gcc within source

前端 未结 2 992
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 22:57

I am trying to get gcc to shut up about my usage of binary constants. They make the code more readable, but prevent me from using -pedantic which I

2条回答
  •  我在风中等你
    2021-01-01 23:31

    maybe, you could use a macro which can do what you want to achieve in a portable manner. here's a short example:

    #include 
    
    #define BINARY(N) strtol(#N, 0, 2)
    
    int main()
    {
        unsigned int piece = BINARY(10010101);
        printf("%u\n", piece);
    
        return 0;
    }
    

    in theory, gcc should be able to optimize the calls to strtol away and you don't lose readability.

    EDIT: It seems that gcc does NOT optimize the strtol calls away as of now. However, your performance loss should be negligible.

    Cheers!

提交回复
热议问题