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++
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3839 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3612 |
7 | Geothermal | 3569 |
8 | ecnerwala | 3494 |
9 | Um_nik | 3396 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | Um_nik | 164 |
2 | -is-this-fft- | 161 |
3 | maomao90 | 160 |
4 | atcoder_official | 158 |
4 | cry | 158 |
4 | awoo | 158 |
7 | adamant | 155 |
8 | nor | 154 |
9 | TheScrasse | 153 |
10 | Dominater069 | 152 |
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++
Name |
---|
Why is it so? What error can it cause?
It is not possible to post-increment an r-value.
Thanks, just googled what r-value and l-value are? We cannot increment a++. Thanks!
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