C++ Alternate Tokens Gone Wild

March 5, 2014

I am in no way condoning the use of these tokens. If I were to ever see any of these tokens used in a C++ code review it would likely get rejected. However, there is at least some importance to knowing that these do indeed exist.

With that said, did you honestly know this C++03 standard code compiles?

#include <iostream>
 
using namespace std;
 
int main()
{
   int x = 1;
   int y = 1;
 
   if (x and y)
      cout << "success" << endl;
 
   if (x bitand y)
      cout << "success" << endl;
 
   if (x bitor y)
      cout << "success" << endl;
 
   if (compl x)
      cout << "success" << endl;
 
   if (x and_eq y)
      cout << "success" << endl;
 
   if (x or_eq y)
      cout << "success" << endl;
 
   if (x or y)
      cout << "success" << endl;
 
   if (x xor_eq y)
      cout << "success" << endl;
 
   if (!(not y))
      cout << "success" << endl;
 
   if (x not_eq y)
      cout << "success" << endl;
 
   if (x xor y)
      cout << "success" << endl;
 
   if (x xor_eq y)
      cout << "success" << endl;
 
   int a<::> = <% 0,1,2,3 %>;
 
   if (a<:1:>)
      cout << "success" << endl;
 
   return 0;
}

It turns out that in C++03 standard, section 2.5:

2.5 Alternative tokens

Alternative token representations are provided for some operators and punctuators. 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
<% {
%> }
<: [
:> ]
%: #
%:%: ##
and &&
bitor |
or ||
xor ˆ
compl ˜
bitand &
and_eq &=
or_eq |=
xor_eq ˆ=
not !
not_eq !=

Related Posts