Блог пользователя __shivam_kumar

Автор __shivam_kumar, история, 2 года назад, По-английски

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++
Теги c++
  • Проголосовать: нравится
  • -21
  • Проголосовать: не нравится

»
2 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Why is it so? What error can it cause?

  • »
    »
    2 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    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.
    • »
      »
      »
      2 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

»
5 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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