When were the 'and' and 'or' alter

2019-01-01 16:09发布

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?

8条回答
唯独是你
2楼-- · 2019-01-01 16:16

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.

查看更多
不流泪的眼
3楼-- · 2019-01-01 16:19

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

查看更多
高级女魔头
4楼-- · 2019-01-01 16:20

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

查看更多
其实,你不懂
5楼-- · 2019-01-01 16:21

From the C++11 standard, 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 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      &    |
查看更多
余生无你
6楼-- · 2019-01-01 16:22

To actually answer the question :

They were defined in the first C++ standard.

查看更多
后来的你喜欢了谁
7楼-- · 2019-01-01 16:28

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.

查看更多
登录 后发表回答