The line of code in the heading is actually C++. I recently noticed a small section called Alternative tokens in the standard (it’s 2.6, for the curious) which allows this sort of shenanigans.
The following code will probably look slightly alien to you, like a weird C++ knockoff, but it’s a standard conforming C++ program:
%:include <iostream>usingnamespace std;int main()<%int arr<:2:>=<%1, 1%>;if(arr<:0:> not_eq 0 and not false)cout<<"Hey, this works!"<< endl;return0;%>
This program is compiled happily by GCC and Clang. Visual Studio 2010 allows this code when it’s compiled with /Za. Without /Za, it’s still possible to use the alternative operators (but not the digraphs) in Visual Studio by including <iso646.h> (thanks to Vasily Milanich for pointing it out to me).
The C++ standard provides a set of alternative tokens for some operators and other symbols. Here is the complete list:
According to http://www.lysator.liu.se/c/na1.html (thanks to Matt Healy for finding the page), these tokens are the result of a “Danish proposal” to make C programs more visually appealing on terminals that only offered the 7-bit ISO 646 character set which didn’t have curly braces, for example.
The advantages of using the alternative tokens are that they might make code look more appealing to curly brace haters, and the alternative logical operators will make Python programmers feel more at home. They will also come in handy for a code obfuscation contest.
The disadvantages probably include a lot of WTF comments from other C++ developers.
Jokes aside, I haven’t seen any of these in real world code. A few people on r/cpp said they use or advocate them, however.
This made me wonder if the alternative operator tokens can or should be used more widely (I’m pretty sure using the digraph alternatives for curly braces etc. isn’t a good idea). In particular, not, and and or are readable, obvious and not verbose.
For anyone who sees the alternative operators for the first time, I think there’ll be quite a lot of confusion, followed by trying to figure out how they work. I’m not sure people will immediately check the standard, so they might waste some time on investigation, e.g. if they decide the operators work via preprocessor magic within the code base.
After the initial confusion though, it’s really obvious what the alternative operators are. So perhaps it’s not too big a deal if someone just starts using them?
On the other hand, the use of !, && and || is so entrenched that it might be difficult for people to adjust to seeing English words in their place.
Do you think alternative operator tokens should be used more?
Did you like this post? Subscribe to the mailing list for more C++ tidbits. Get two cheatsheets (C++11 STL additions and regex library) and an introduction to lambdas. Also an occasional special deal on the C++11 Rocks book.
Your C++11 Rocks beta book is awesome. Please keep the C++ content coming! - Tom Kirby-Green
Just wanted to say what a pleasant surprise your C++11 Rocks book has been! Very clearly and concisely written, with great depth and coverage. I've been a full-time C++ developer since 1994, so wasn't expecting to pick up as much as I have! - Shaun Fielder
Thanks again for the book! I find myself referring to it frequently as I try to integrate the new C++11 features into my daily work. I can’t imagine how many hours you spent working out all of the idiosyncrasies in the VS2010 implementation but those are hours I get to save! - Angel Marquez, Senior Software Developer (@DebugMonkey)
Excellent C++11 book! I appreciate the craftsmanship that went into your C++11 Rocks book. This is the perfect reference for all the new features that went into the language.
- John Shaffstall
I am so glad that I bought the book. It is a very unconventional book. In no book, what so ever, have I read about compiler bugs, work-arounds, etc. I think your description of common errors, pitfalls, and bugs will have saved me a lot of trouble. - Mattias Högström, System Architect at Visma.com
I would like to say that C++11 Rocks is the shortest, best C++11 feature-telling book, and I can grab C++11 knowledge from it quickly. - Kasicass Tang