C / C ++:如何使用DO-而(0);构建无编译警告像C4127? [英] C/C++: How to use the do-while(0); construct without compiler warnings like C4127?

查看:106
本文介绍了C / C ++:如何使用DO-而(0);构建无编译警告像C4127?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用的做,而(0)在我的#define建设,为上述原因<一href=\"http://stackoverflow.com/questions/154136/why-are-there-sometimes-meaningless-do-while-and-if-else-statements-in-c-c-macr/154138#154138\">in这个答案。我也正在尝试使用尽可能高的警告级别从编译器捕捉更多的潜在问题,并让我的code更强大,跨平台的。所以我通常使用 -Wall 用gcc和 /华尔街用MSVC。

I'm often use do-while(0) construct in my #defines, for the reasons described in this answer. Also I'm trying to use as high as possible warning level from compiler to catch more potential problem and make my code more robust and cross-platform. So I'm typically using -Wall with gcc and /Wall with MSVC.

不幸的是MSVC抱怨做,而(0)结构:

Unfortunately MSVC complain about do-while(0) construct:

foo.c(36) : warning C4127: conditional expression is constant

我应该怎么做这个警告呢?

What should I do about this warning?

只是全球禁用它的所有文件?它不似乎是不错的主意,我。

Just disable it globally for all files? It does not seems to be good idea for me.

推荐答案

摘要:这这种特殊情况下报警(C4127)是一种微妙的编译错误。随意将其禁用。

Summary: This warning (C4127) in this particular case is a subtle compiler bug. Feel free to disable it.

在深度:

这是为了赶情况下,当逻辑EX pression计算为一个常数不明显的情况下(如,如果(A == A和与放大器;!A = A) ,不知何故,它变成,而(真)和其他有用建设成为无效。

It was meant to catch situations when logical expression evaluates to a constant in non-obvious situations (such as, if(a==a && a!=a), and somehow, it turned while(true) and other useful constructs into invalid.

Microsoft建议使用为(;;)的无限循环,如果你想对这个警告,有你的情况下,没有解决方案。这是极少数的4级警告我公司发展的各项公约允许禁用之一。

Microsoft recommends using for(;;) for infinite loop if you want to have this warning on, and there is no solution for your case. This is one of very few Level-4 warnings my company's development conventions allow to disable.

这篇关于C / C ++:如何使用DO-而(0);构建无编译警告像C4127?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆