How to overcome pointless C++ compiler warnings elegantly?

后端 未结 7 868
迷失自我
迷失自我 2021-01-01 10:12

This question is not bound to any specific compiler warning, the following is just an example.

Currently when I want a loop that checks an exit condition inside:

7条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-01 11:15

    My philosophy is that if you make the compiler suppress warnings, put a comment in there and tell why. Even if you think it's silly. Pragma stands out and is visible. It's a fine comment in your code.

    When you start skipping comments when suppressing warnings based on your idea on how silly it is, you are heading for potential trouble. Another person working on your code after a year might change it, have trouble and waste precious time hunting it down.

    Btw, you are looking for a way of suppressing the warnings without either

    • Use pragma
    • Use ugly code
    • Use project wide suppression

    That would be a very hidden suppression method.

    If you don't like the look of pragma, use

    bool alwaysTrue = true; // to prevent compiler warning C4127
    while (alwaysTrue) {
        ...
    }
    

提交回复
热议问题