Skip to main content

Raymii.org Raymii.org Logo

Quis custodiet ipsos custodes?
Home | About | All pages | Cluster Status | RSS Feed

and & or are valid in C++ (alternative tokens)

Published: 16-12-2019 | Author: Remy van Elst | Text only version of this article


❗ This post is over four years old. It may no longer be up to date. Opinions may have changed.


A post on lobste.rs on the C feature trigraphs triggered me to write this piece on the C++ feature, alternative tokens. I've recently suprised a co- worker by using an if statement with 'or' instead of '||', which works fine, but he never saw it before. It's in C++ since C++ 11, but MSVC requires a specific compiler flag ('/Za') or the "iso646.h" header. This post has a few samples on the usage inclusing other alternative tokens like bitor, xor and or_eq.

Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:

I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!

The or and and feel more natural to me. Even though I perfectly know and understand the || and && operators, I would prefer the alternative tokens when reading code. For bitsets, the bitand, compl and bitor also read better, in my opinion. The trigraph feature of C was removed in C++ 17. Those are weird.

Alternative tokens

The motivation for the alternative tokens can be found [on cppreference][4]:

C++ (and C) source code may be written in any non-ASCII 7-bit character set that includes the ISO 646:1983 invariant character set. However, several C++ operators and punctuators require characters that are outside of the ISO 646 codeset: {, }, [, ], #, , ^, |, ~. To be able to use character encodings where some or all of these symbols do not exist (such as the German DIN 66003), C++ defines the following alternatives composed of ISO 646 compatible characters.

There are alternative spellings for several operators and other tokens that use non-ISO646 characters. In all respects of the language, each alternative token behaves exactly the same as its primary token, except for its spelling (the stringification operator can make the spelling visible). The two-letter alternative tokens are sometimes called "digraphs"

The characters & and ! are invariant under ISO-646, but alternatives are provided for the tokens that use these characters anyway to accomodate even more restrictive historical charsets.

There is no alternative spelling (such as eq) for the equality operator == because the character = was present in all supported charsets.

Some examples

An few examples on the use of alternative tokens, starting with and and or.

bool ex1 { false };
bool ex2 { true };
if ( ex1 or ex2) // instead of ||
{
    std::cout << "Hello, World!" << std::endl;
}

Result:

Hello, World!

Example 2, not and

bool ex1 { false };
bool ex2 { true };
if (not ex1 and ex2) // instead of (!ex && ex2)
{
    std::cout << "Hello, World!" << std::endl;
}

Result:

Hello, World!

Example 3, square brackets and curly braces

bool b<:2:> <%true, true%>; // instead of bool b[2] {true, true}
if (b<:1:>)
{
    std::cout << "Hello, World!" << std::endl;
}

Result:

Hello, World!

Example 4, bitand

std::bitset<4> bs1 { 0011 };
std::bitset<4> bs2 { 0001 };
auto bs3 = bs1 bitand bs2; //instead of | 
std::cout << bs3 << std::endl;

Result:

0001

All tokens

The full list of alternative tokens:

Alternative Primary
<%          {
%>          }
<:          [
:>          ]
%:          #
%:%:        ##
and         &&
bitor       |
or          ||
xor         ^
compl       ~
bitand      &
and_eq      &=
or_eq       |=
xor_eq      ^=
not         !
not_eq      !=
Tags: blog , c++ , cpp , development , digraph , linux , software , trigraph , types