New experimental C ++ operators

Original author: Raymond Chen
  • Transfer
So often you have to write code like this:
x = (y + 1) % 10;
x = (y + 1) * (z - 1);
x = (double)(f(y) + 1);


Since the operators + and - have such a low priority, you have to constantly enclose them in brackets, and this leads to a deep embedded code, which is difficult to understand.
Visual Studio 2015 RC adds a couple of experimental statements called tadpole statements. They allow you to add and subtract a unit without having to resort to brackets.
x = -~y % 10;
x = -~y * ~-z;
x = (double)-~f(y);


They are so named because they resemble tadpoles that float to or from a meaning. Tilda is the head, and minus is the tail.
SyntaxValueMnemonics
- ~ yy + 1Tadpole floats to value and makes it bigger
~ -yy - 1The tadpole floats on value and makes it smaller


To enable experimental support for tadpole operators, add at the beginning of the C ++ file:
#define __ENABLE_EXPERIMENTAL_TADPOLE_OPERATORS

Here is a simple program that demonstrates the use of tadpole operators:

#define __ENABLE_EXPERIMENTAL_TADPOLE_OPERATORS 
#include 
#include 
#include 
int __cdecl main(int, char**)
{
   int n = 3;
   std::cout << "3 + 1 = " << -~n << std::endl;
   std::cout << "(3 - 1) * (3 + 1) " << ~-n * -~n << std::endl;
   return 0;
}

Remember that these operators are still experimental. They are not an official part of C ++, but you can play with them and leave your comments here .

From translator
Nevertheless, I decided to add an explanation of what is happening directly in the article.
When representing numbers in an additional code , the relation between arithmetic and bitwise operations is performed:
-x = ~x + 1;

From which it turns out logically:
-~x = ~(~x) + 1 = x + 1;
~-x = (~-x + 1) - 1 = -(-x) - 1 = x - 1;

That is, no new tadpole operators are introduced in C ++, this is just a joke from Raymond Chen.
Other non-obvious operations in C ++ can be viewed on this page .

Also popular now: