Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why ++++i regular but i++++ not regular in C++?

Tags:

c++

When I use i++++ give compile error :

for (int i=1;i<=10;i++++) {} //a.cpp:63: error: lvalue required as increment operand

or

int i = 0;
i++++; // a.cpp:65: error: lvalue required as increment operand

but when I use ++++i is working. Anybody explain me why ++++i regular but i++++ not regular?

Thanks.

like image 487
Elmi Ahmadov Avatar asked May 13 '11 05:05

Elmi Ahmadov


1 Answers

Since type of x is built-in primitive type, both expressions invoke undefined behaviour, as both attempt to modify the same object twice between two sequence points.

Don't do either of them.

Read this FAQ :

Undefined behavior and sequence points


However if the type of x is a user-defined type and you've overloaded operator++ for both expressions, then both would be well-defined.

For this, see this topic to know the explanation and details:

Undefined behavior and sequence points reloaded

like image 190
Nawaz Avatar answered Sep 30 '22 14:09

Nawaz