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

8

44

I've just read this nice piece from Reddit.

They mention and and or being "Alternative Tokens" to && and ||

I was really unaware of these until now. Of course, everybody knows about the di-graphs and tri-graphs, but and and or? Since when? Is this a recent addition to the standard?

I've just checked it with Visual C++ 2008 and it doesn't seem to recognize these as anything other than a syntax error. What's going on?

Fixation answered 17/2, 2009 at 4:8 Comment(5)
I like how seven years later, still none of the answers given actually answer this question. lolDropping
@LightnessRacesinOrbit sometimes I think it does not matter much, as long as it answers Google searches ;-)Gleiwitz
I've now added an answer which answers the question!Nudi
Actually, @MartinBonner answers your question more precisely than the chosen answer.Gradely
I just noticed that these are in VS2017... Don't know if they have been in previous versions.Straightway
R
24

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

Reitz answered 17/2, 2009 at 4:21 Comment(3)
So Python is 50x cool because you can write and, meanwhile C had this as a feature all along.Psychosocial
@Psychosocial It does seem a bit frustrating that we have to #include <ciso646> everywhere we want to use these keywords in Visual Studio. It's probably no surprise that g++ supports them out of the box since they are in the standard.Straightway
Note Visual Studio 2019 recognizes them as keywords - I didn't need to include any extra headers.Felten
X
37

From the first ISO C++ standard C++98, this is described in 2.5/ Alternative tokens [lex.digraph]:


  1. Alternative token representations are provided for some operators and punctuators.
  2. In all respects of the language, each alternative token behaves the same, respectively, as its primary token, except for its spelling. The 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      &    |

So it's been around since the earliest days of the C++ standardisation process. The reason so few people are aware of it is likely because the main use case was for people operating in environments where the full character set wasn't necessarily available. For example (and this is stretching my memory), the baseline EBCDIC character set on the IBM mainframes did not have the square bracket characters [ and ].

Xever answered 17/2, 2009 at 4:18 Comment(0)
R
24

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

Reitz answered 17/2, 2009 at 4:21 Comment(3)
So Python is 50x cool because you can write and, meanwhile C had this as a feature all along.Psychosocial
@Psychosocial It does seem a bit frustrating that we have to #include <ciso646> everywhere we want to use these keywords in Visual Studio. It's probably no surprise that g++ supports them out of the box since they are in the standard.Straightway
Note Visual Studio 2019 recognizes them as keywords - I didn't need to include any extra headers.Felten
N
15

To actually answer the question :

They were defined in the first C++ standard.

Nudi answered 15/7, 2016 at 20:32 Comment(0)
B
5

See the C++ standard. The committee draft #2 is freely available at ftp://ftp.research.att.com/dist/c++std/WP/CD2/body.pdf, although it's non-authoritative, out-of-date, and partially incorrect in a few places. Specifically, in section 2.5, Alternative Tokens, the following are defined:

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

Though honestly, I've never seen any of them ever used except for and, or, and not, and even then, those are rare. Note that these are NOT allowable by default in plain C code, only in C++. If you want to use them in C, you'll have to either #define them yourself as macros, or #include the header <iso646.h>, which defines all of the above except for <% >% <: :> %: %:%: as macros (see section 7.9 of the C99 standard).

Bensen answered 17/2, 2009 at 4:19 Comment(1)
They're allowed in C if you include <iso646.h> and have been since 1994.Furculum
G
2

Although the question is old, I'd want to provide it with more or less full answer: Alternative tokens were already a part of the currently withdrawn C++98 (ISO/IEC 14882:1998, which, I believe, was the first ISO standard for C++). While not a proof in itself (and I don't own a copy of ISO for c++98), here's a link - see C++ section.

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

Gradely answered 19/7, 2017 at 15:29 Comment(0)
C
1

You may be surprised to learn about the rest of them:

and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq

List from C++ Keywords.

I believe recent versions of GCC support these keywords.

Cytaster answered 17/2, 2009 at 4:14 Comment(2)
I'd hardly call them new. They're in the original C++ standard, aren't they?Kindergarten
Ok, they're older than a decade, I'll remove the word "new". :)Cytaster
C
1

The GNU compiler g++ has them, but I don't know about MS VC++.

You can get the same functionality by putting this 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 !=

Though this is kinda hackish, it should work.

Carven answered 17/2, 2009 at 4:19 Comment(2)
Or include <iso646.h>, if your implementation has it (I think most do).Reitz
Or <ciso646> if you're using C++.Chanterelle
F
-1

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

Frontward answered 17/2, 2009 at 4:15 Comment(1)
They've been in the C++ standard since 1998.Reitz

© 2022 - 2024 — McMap. All rights reserved.