📜 ⬆️ ⬇️

New experimental C ++ operators

So often you have to write such code:
x = (y + 1) % 10; x = (y + 1) * (z - 1); x = (double)(f(y) + 1); 


Since the + and - operators have such a low priority, we have to constantly enclose them in brackets, and this leads to a deep nested code that is difficult to understand.
A pair of experimental operators named tadpole operators have been added to Visual Studio 2015 RC. 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 the value. A tilde is a head, and a minus is a tail.
SyntaxValueMnemonics
- ~ yy + 1Tadpole swims to value and makes him bigger
~ -yy - 1Tadpole swims away from the 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 <ios> #include <iostream> #include <istream> 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 the C ++ language, but you can play with them and leave your comments here .

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

From what it is logical it turns out:
 -~x = ~(~x) + 1 = x + 1; ~-x = (~-x + 1) - 1 = -(-x) - 1 = x - 1; 

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

Source: https://habr.com/ru/post/258811/


All Articles