Say,the trailing ++
has no actual effect here?
If you have i+i++
it is actually acting like something like i+i; i = i + 1
This will post-increment l
i.e. compute l+l
and increment l
after that, so it has some effect..
Demo code:
#include <iostream>
int main() {
int l = 1;
std::cout << "l+l++ = " << l+l++ << std::endl;
std::cout << "l = " << l << std::endl;
return 0;
}
EDIT: Note that this compile with a warning (see Pascal's answer):
main.cpp: In function ‘int main()’:
main.cpp:5: warning: operation on ‘l’ may be undefined
done as separe operations so will not work as you might expect
Be warned - many languages don't dictate the order operands are evaluated, so you may see 2*l
or 2*l+1
as the result of that expression. In either case l
will be one higher afterwards.
l+l++
is undefined. There is no sequence point in your expression to separate the access to l
and the post-increment. It can do anything, including having the same effects as l+l
.
EDIT: the question and answers at http://stackoverflow.com/questions/1860461/why-is-i-i-1-unspecified-behavior explain what a sequence point is, quote the relevant parts of the standard and provide links. Let me quote again:
Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. 53) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.
Emphasis mine.
SECOND EDIT: By popular request, the next sentence in the paragraph:
The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.