6

What the title says. For C++, (++a)++ does compile. Strangely enough, though, ++(a++) does not:

int main() {
    int a = 0;
    ++a++; // does not compile
    (++a)++; // does compile
    ++(a++); // does not compile
}

But in Java, it does not for all three:

public class Test {
    public static void main(String[] args) {
        int a = 0;
        ++a++; // does not compile
        (++a)++; // does not compile
        ++(a++); // does not compile
    }
}

Is there any reason why C++ compiles this but not in Java?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Ryan Dougherty
  • 528
  • 6
  • 21
  • What C++ compiler? And in what sense do you think `a++` is a `void` statement? – Fred Larson Nov 19 '14 at 16:13
  • @FredLarson I used the `C++11` compiler on ideone.com, but I would think it is allowable on any C++ compiler. I think it is a `void` statement because it is equivalent (functionally) to: `a = a+1;` which doesn't give an actual value. – Ryan Dougherty Nov 19 '14 at 16:14
  • `"...a parser using only one-token lookahead and no semantic analysis during the parse would not be able to tell, when ++ is the lookahead token, whether (p) should be considered a Primary expression or left alone for later consideration as part of a CastExpression."` – azurefrog Nov 19 '14 at 16:15
  • @Ryan: You're incorrect about that. `a++` returns the value of `a` before the increment. – Fred Larson Nov 19 '14 at 16:15
  • @FredLarson Ok yeah I see what you mean. But then why is `++(a++)` not compile-able then, since `a++` returns the value of `a` before the increment? – Ryan Dougherty Nov 19 '14 at 16:16
  • It is time for these operators to be deprecated anyway... IMO its much better to write out calculations without '++' or '--' as the performance gain is minimal (especially with modern compilers / code optimizers) and doesn't validate making the code harder to read – ControlAltDel Nov 19 '14 at 16:20
  • Of course you can always do `++ ++a` if you do not need 'a-1` afterwards. – Joop Eggen Nov 19 '14 at 16:50
  • @bmargulies that can not be a duplicate since it only covers C++ and the question is clearly not solely about C++. Conceptually they are similar issue but the answers are different for Java than in C++ which is actually the point of the question. – Shafik Yaghmour Nov 19 '14 at 20:13
  • @ControlAltDel imagine how much code would have to be changed in order to deprecate those operators, pretty much any C and C++ code out there uses those operators. – Shafik Yaghmour Nov 20 '14 at 22:25

1 Answers1

11

None of the examples work in Java because both postfix and prefix increment operations return values not variables we can see this by going to the JLS section on Postfix Increment Operator ++ for an example and it says:

The result of the postfix increment expression is not a variable, but a value.

The JLS section for Prefix Increment Operator ++ says the same thing.

This would be like trying to increment a literal value (see it live):

2++ ;
++3 ;

which gives the following error:

required: variable
found:    value

Which is the same error we receive for your examples.

In C++ prefix increment returns an lvalue but postfix increment returns an prvalue and both prefix and postfix increment in C++ require an lvalue. So your first and third C++ example:

++a++;
++(a++)

fails because you are attempting to apply prefix increment to a prvalue. While the second C++ example:

(++a)++;

is okay because prefix increment returns an lvalue.

For reference the draft C++ standard in section 5.2 Postfix expressions says:

The value of a postfix ++ expression is the value of its operand [...] The operand shall be a modifiable lvalue

and:

The result is a prvalue

and section 5.3 Unary expressions says:

The operand of prefix ++ is modified [...] The operand shall be a modifiable lvalue

and:

The result is the updated operand; it is an lvalue

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740