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

I just read this nice little thing from Reddit.

They mention and and or as "Alternate Tokens" for && and ||

I still did not know about it. Of course, everyone knows about di-graphs and tri-graphs , but and and or ? Since when? Is this a recent addition to the standard?

I just tested it with Visual C ++ 2008, and it doesn't seem to recognize them as anything other than a syntax error. What's happening?

+37
c ++ syntax keyword digraphs
Feb 17 '09 at 4:08
source share
8 answers

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

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

I assume that they believe that by default keywords will break too much existing code (and I won’t be surprised if they are right).

This was discussed in a question a couple of weeks ago somewhere here on SO, but I can't get SO search or Google to find this damn thing.

+23
Feb 17 '09 at 4:21
source share

From C ++ 11 2.6/ Alternative tokens standard:

  • For some operators and punctuators, alternative representations of tokens are provided.
  • In all aspects of the language, each alternative token behaves the same, respectively, as the main marker, with the exception of its spelling. A set of alternative tokens is defined in table 2.

Table 2 - Alternative tokens

 alternative primary | alternative primary | alternative primary --------------------+---------------------+-------------------- <% { | and && | and_eq &= %> } | bitor | | or_eq |= <: [ | or || | xor_eq ^= :> ] | xor ^ | not ! %: # | compl ~ | not_eq != %:%: ## | bitand & | 
+32
Feb 17 '09 at 4:18
source share

To answer the question:

They were defined in the first C ++ standard.

+10
Jul 15 '16 at 20:32
source share

See the C ++ standard. Draft Committee No. 2 is freely available at ftp://ftp.research.att.com/dist/c++std/WP/CD2/body.pdf , although it is not authoritative, outdated and partially incorrect in several places. In particular, in section 2.5 Alternative Tokens , the following values ​​are defined:

 Alternative primary
 <% {
 %>}
 <: [
 :>]
 %: #
 %:%: ##
 and &&
 bitor |
 or ||
 xor ^
 compl ~
 bitand &
 and_eq & =
 or_eq | =
 xor_eq ^ =
 not!
 not_eq! =

Although honestly, I have never seen any of them ever used, except and , or and not , and even then it is rare. Please note that they are NOT allowed by default in plain C code, only in C ++. If you want to use them in C, you will either have to #define them yourself as macros, or the #include header <iso646.h> , which defines all of the above except for <% >% <: :> %: %:%: as macros (see section 7.9 of the C99 standard).

+4
Feb 17 '09 at 4:19
source share

You may be surprised to learn about them:

 and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq 

List of C ++ Keywords .

I believe that the latest versions of GCC support these keywords.

0
Feb 17 '09 at 4:14
source share

The GNU g ++ compiler has them, but I do not know about MS VC ++.

You can get the same functionality by placing it at the top of your code file.

 #define and && #define bitor | #define or || #define xor ^ #define compl ~ #define bitand & #define and_eq &= #define or_eq ^= #define xor_eq ^= #define not ! #define not_eq != 

Although this is pretty hacky, this should work.

0
Feb 17 '09 at 4:19
source share

Although the question is old, I would like to provide it with a more or less complete answer: Alternative tokens were already part of the revoked C ++ 98 ( ISO / IEC 14882: 1998 , which, I believe, was the first ISO for C ++). Although this is not a proof in itself (and I do not have a copy of ISO for C ++ 98), here is the link - see the C ++ Section.

As mentioned in other answers, the MSVC compiler violates the [lex.digraph] section of the standard if the / Za flag is not specified.

0
Jul 19. '17 at 15:29
source share

They are in the working paper for the new C ++ standard, on page 14: C ++ Standard

-one
Feb 17 '09 at 4:15
source share



All Articles