是否可以关闭对“和"的支持?/“或"gcc中的布尔运算符用法?

GCC 似乎允许使用and"/or"代替&&"/||"在 C++ 代码中;但是,正如我所料,许多编译器(尤其是 MSVC 7)不支持这一点.GCC 允许这样做的事实给我们带来了一些烦恼,因为我们有不同的开发人员在多个平台上处理相同的代码库,并且偶尔,当人们在 Python 和 C++ 开发之间来回切换时,这些错误"就会溜进来.

GCC seems to allow "and" / "or" to be used instead of "&&" / "||" in C++ code; however, as I expected, many compilers (notably MSVC 7) do not support this. The fact that GCC allows this has caused some annoyances for us in that we have different developers working on the same code base on multiple platforms and occasionally, these "errors" slip in as people are switching back and forth between Python and C++ development.

理想情况下,我们都会记得使用适当的语法,但是对于那些我们偶尔会搞砸的情况,如果 GCC 不让它滑动,那就太好了.有人对此有任何想法吗?

Ideally, we would all remember to use the appropriate syntax, but for those situations where we occasionally mess up, it would be really nice if GCC didn't let it slide. Anybody have any ideas on approaches to this?

如果and"和or"只是#defines,那么在使用GCC时我可以#undef,但我担心它更有可能在更基础的层面内置于编译器中.

If "and" and "or" are simply #defines then I could #undef when using GCC but I worry that it is more likely built into the compiler at more fundamental level.

谢谢.

推荐答案

它们是 C++ 标准的一部分,例如参见 this StackOverflow answer(引用了标准的相关部分).

They are part of the C++ standard, see for instance this StackOverflow answer (which quotes the relevant parts of the standard).

同一个问题的另一个答案提到了如何做相反的事情:让它们在 MSVC 中工作.

Another answer in the same question mentions how to do the opposite: make them work in MSVC.

要在 GCC 中禁用它们,请使用 -fno-operator-names.请注意,这样做实际上是在切换到 C++ 的非标准方言,并且存在您最终编写的代码可能无法在符合标准的编译器上正确编译的风险(例如,如果您声明一个具有通常保留名称的变量).

To disable them in GCC, use -fno-operator-names. Note that, by doing so, you are in fact switching to a non-standard dialect of C++, and there is a risk that you end up writing code which might not compile correctly on standard-compliant compilers (for instance, if you declare a variable with a name that would normally be reserved).

相关文章