__shivam_kumar's blog

By __shivam_kumar, history, 3 years ago, In English

you cannot do nesting of post-increment operator whereas pre-increment nesting does not cause any problem

conclusion : never do post increment nesting

eg :

int a = 10;

 cout<<(++(++a))<<"\n";// valid in c++

 cout<<((a++)++); // in-valid in c++
Tags c++
  • Vote: I like it
  • -21
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Why is it so? What error can it cause?

  • »
    »
    3 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    It is not possible to post-increment an r-value.

    The value obtained by applying a postfix ++ is the value that the operand had before applying the operator. The operand shall be a modifiable lvalue. The result is an rvalue.
    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks, just googled what r-value and l-value are? We cannot increment a++. Thanks!

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I encountered this error in Java while trying to nest pre- and post-increment operators and came across this thread while trying to find the reason. I want to add that nesting of both pre-increment and post-increment operators is not allowed in Java. E.g. ~~~~~ int a = 5; System.out.println(++(++a)); // required: variable found: value System.out.println((a++)++); // required: variable found: value ~~~~~

Reference — 1. Postfix Expressions — https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.14 2. Prefix Expressions — https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.15.1