Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to turn off support for "and" / "or" boolean operator usage in gcc?

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.

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?

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.

Thanks.

like image 651
Joe Corkery Avatar asked Jul 01 '09 14:07

Joe Corkery


People also ask

How do I turn off warnings in GCC?

To suppress this warning use the unused attribute (see Variable Attributes). This warning is also enabled by -Wunused , which is enabled by -Wall . Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall .

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

What is the job of werror option in GCC?

1. What is the job of -Werror option in gcc? Explanation: None.

How do I enable warnings in GCC?

GCC 4.3+ now has -Q --help=warnings , and you can even specify --help=warnings,C to just print out the C related warnings.


2 Answers

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

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

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).

like image 126
CesarB Avatar answered Oct 25 '22 00:10

CesarB


The words are standard in C++ without the inclusion of any header.

The words are standard in C if you include the header <iso646.h>.

MSVC is doing you no service by not supporting the standards.

You could, however, use tools to enforce the non-use of the keywords. And it can be a coding guideline, and you can quickly train your team not to make silly portability mistakes. It isn't that hard to avoid the problem.

like image 20
Jonathan Leffler Avatar answered Oct 25 '22 00:10

Jonathan Leffler