Why Does VS Not Define the Alternative Tokens for Logical Operators

Why does VS not define the alternative tokens for logical operators?

You ask about the rationale. Here's one possible reason, not necessarily the one that most influenced the Visual C++ team:

  1. Those are valid identifiers in C.
  2. Microsoft's recommendation has long been to use C++ mode for both C and C++ code, rather than maintaining a modern C compiler.
  3. Valid C code using these as identifiers would gratuitously break if they were compiled as keywords.
  4. People trying to write portable C++ are mostly using /permissive- or /Za for maximum conformance anyway, which will cause these to be treated as keywords.
  5. The workaround to treat them as keywords in /Ze by including a header file is easy and portable. (G++'s workaround -fno-operator-names isn't bad either, but putting the option in the source code rather than the build system is somewhat nicer.)

using word not as a name of c++ class causes error on VS2019

not is a keyword in standard C++ which can be used as alternative for the ! token.

MSVC does not support the alternative operator tokens by default. Therefore they are available as identifiers and you don't get an error on your declaration.

But in compliance mode with the /permissive- compiler flag, these tokens are considered keywords with their standard meaning and so they are not available as identifiers.

I suggest not using not as identifier, even when not using compliance mode, because that makes your code non-standard and non-portable.

see https://learn.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance

and Why does VS not define the alternative tokens for logical operators?

When were the 'and' and 'or' alternative tokens introduced in C++?

MSVC supports them as keywords only if you use the /Za option to disable extensions; this is true from at least VC7.1 (VS2003).

You can get them supported as macros by including iso646.h.

My guess is they believe that making them keywords by default would break too much existing code (and I wouldn't be surprised if they are right).

Keywords and , or and not not defined

"Include the header file iso646.h, or compile with the /Za (Disable language extensions) compiler option."

MSDN Link

When were the 'and' and 'or' alternative tokens introduced in C++?

MSVC supports them as keywords only if you use the /Za option to disable extensions; this is true from at least VC7.1 (VS2003).

You can get them supported as macros by including iso646.h.

My guess is they believe that making them keywords by default would break too much existing code (and I wouldn't be surprised if they are right).

and, or, not versus &&, ||, !

Because they don't allow mixed C/C++ code without including additional header files, are less known to programmers, and it's not immediately clear whether and is the short-circuit or bitwise version.

When did and become an operator in C++

There are several such alternatives defined in C++. You can probably use switches to turn these on/off.

Which C++ logical operators do you use: and, or, not and the ilk or C style operators? why?

I won't use the alternative operators as they cause more confusion then clearity in my opinion.

If i see an alphabetical name i expect a namespace, class, variable, function or a function style operator - the common operators divide this intuitively into sequences for me. The alternative style just doesn't fit into the C/C++ world for me.

Also, while Greg has a point regarding people new to C or C++, you get used to it pretty soon - i have no problems spotting ! anywhere.

What is the difference between and and && in c++

The C++ standard permits the token && to be used interchangeably with the token and.

Not all compilers implement this correctly (some don't bother at all; others require the inclusion of a special header). As such, code using and can be considered idiosyncratic.

The fact that the equivalence is at the token, rather than the operator, level means that since C++11 (where the language acquired the rvalue reference notation), you can arrange things (without recourse to the preprocessor) such that the statement

int and _int(string and vector);

is a valid function prototype. (It's eqivalent to int&& _int(string&& vector).)

Should I use && or and ?

2.6 Alternative tokens [lex.digraph]

1 Alternative token representations are provided for
some operators and punctuators.16

2 In all respects of the language, each alternative token behaves the
same, respectively, as its primary token
, except for its spelling.17
The set of alternative tokens is defined in Table 2.

Can't paste table 2, but it explicitly states Alternative: and, Primary && (same for or and ||).

So they are absolutely identical.
If you want to try and convince yourself one is "better" than the other, that's your business. If someone else is trying to argue such, they'd better have a good reason.

Edit: The aforementioned Table 2:

Table 2 — Alternative tokens
Alternative Primary
<% {
%> }
<: [
:> ]
%: #
%:%: ##
and &&
bitor |
or ||
xor ˆ
compl ~
bitand &
and_eq &=
or_eq |=
xor_eq ˆ=
not !
not_eq !=

Edit: Maybe worth noting, according to Sebastian Redl, MS break the rules here.



Related Topics



Leave a reply



Submit