What is the “-->” operator in C++?


After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4.

Here's the code:

#include <stdio.h>
int main()
{
    int x = 10;
    while (x --> 0) // x goes to 0
    {
        printf("%d ", x);
    }
}

I'd assume this is C, since it works in GCC as well. Where is this defined in the standard, and where has it come from?



Answers


--> is not an operator. It is in fact two separate operators, -- and >.

The conditional's code decrements x, while returning x's original (not decremented) value, and then compares the original value with 0 using the > operator.

To better understand, the statement could be written as follows:

while( (x--) > 0 )



That's a very complicated operator, so even ISO/IEC JTC1 (Joint Technical Committee 1) placed its description in two different parts of the C++ Standard.

Joking aside, they are two different operators: -- and > described respectively in §5.2.6/2 and §5.9 of the C++03 Standard.




Or for something completely different... x slides to 0

while (x --\
            \
             \
              \
               > 0)
     printf("%d ", x);

Not so mathematical, but... every picture paints a thousand words...




What is <— in Java?

<-- is not a new Java operator (even though it may look like it), but there are 2 normal operators: < and --

while (var2 <-- var1) is the same as while(var2 < (--var1)), which can be translated to plain english as:

  1. decrement the var1 variable ( --var is a prefix decrementation, ie. decrement the variable before condition validation)
  2. Validate the condition var2 < var1



<-- There is no such operator in java.

It is var2 < (--var1) A relational + decrement operator.




What does --> means in Java?

--> is not a new operator.

It is just a conjunction of the operators -- and >.

You first compare, and then decrement the variable.

That is,

i --> 0

becomes effectively

i > 0; //Compare
i--; //and decrement



i --> 0 means i-- > 0, i is decrememnted and the previous value of i is compared to 0.




--> is not any operator. It is just the cocatenation of -- and >.

So when you write

i-->0 it means compare the value of i and then decrement it.

So for better readability it can be written as

for (int i = 99; (i--)> 0;) {



How does the minus minus less than “-->” operator work in JavaScript?

It is the post decrement operator -- followed by the greater than operator >. Just the spacing is weird.

while (max-- > min)

So, while max decremented is larger than min