Why MSVC generates warning C4127 when constant is used in “while” - C

后端 未结 2 1303
被撕碎了的回忆
被撕碎了的回忆 2021-01-17 19:45

For code,

while(1)
{
   /* ..... */
}

MSVC generates the following warning.

warning C4127: conditional expression is const         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-17 20:16

    for(;;) and while (true) are different in that the former is a special case defined to be an infinite loop, while the latter is sort of an abuse saying "true, always."

    The warning comes up because infinite loops when you don't want them are pretty bad, so it's warning you that you might have one at the first sign. But by using for(;;), you've pretty much explicitly said "loop this forever", and there's nothing to warn about.

    I don't think GCC has an equivalent warning.

提交回复
热议问题