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 | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
6 | adamant | 157 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | djm03178 | 153 |
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