views:

667

answers:

6

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 just till now. Of course, everybody knows about the di-graphs and tri-graphs but "and" and "or"? Seriously? 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?

A: 

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.

Greg Hewgill
I'd hardly call them new. They're in the original C++ standard, aren't they?
Rob Kennedy
Ok, they're older than a decade, I'll remove the word "new". :)
Greg Hewgill
A: 

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

Rob Lachlan
They've been in the C++ standard since 1998.
Michael Burr
+12  A: 

From the standard (slightly paraphrased):

2.5/ Alternative tokens.

1/ Alternative token representations are provided for some operators and punctuators. These include "digraphs" and additional reserved words. The term "digraph" (token consisting of two characters) is not perfectly descriptive, since one of the alternative preprocessing tokens is %:%: and of course several primary tokens contain two characters. Nonetheless, those alternative tokens that aren't lexical keywords are colloquially known as "digraphs".

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 the following table. Thus the "stringized" values (16.3.2) of [ and <: will be different, maintaining the source spelling, but the tokens can otherwise be freely interchanged.

This has changed a little in the latest draft C++ standard (n2914), which they appear to have simplified (again slightly paraphrased):

2.6/ Alternative tokens.

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 the following table.

Table (same in both standards):

alternative primary | alternative primary | alternative primary
--------------------+---------------------+--------------------
   <%          {    |    and         &&   |    and_eq      &= 
   %>          }    |    bitor       |    |    or_eq       |= 
   <:          [    |    or          ||   |    xor_eq      ^=
   :>          ]    |    xor         ^    |    not         !
   %:          #    |    compl       ~    |    not_eq      !=
   %:%:        ##   |    bitand      &    |
paxdiablo
It looks like %> got turned into > somehow.
Matt Kane
Thanks, @Matt, Fixed.
paxdiablo
+1  A: 
Adam Rosenfield
They're allowed in C if you include <iso646.h> and have been since 1994.
Jonathan Leffler
A: 

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.

James Matta
Or include <iso646.h>, if your implementation has it (I think most do).
Michael Burr
Or <ciso646> if you're using C++.
Zack Mulgrew
+3  A: 

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

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

Michael Burr