Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

prefix increment operator and side-effects

0 views
Skip to first unread message

subramanian100in@yahoo.com, India

unread,
Dec 23, 2009, 10:12:06 AM12/23/09
to
Stroustrup in his book "The C++ Programming Language - Third Edition
(NOT the special third edition)", mentions the following in page 125
in section "6.2.5 Increment and Decrement".
"By definition, ++lvalue means lvalue += 1, which again means lvalue =
lvalue + 1 provided lvalue has no side-effects".

Here I am unable to understand why Stroustrup has mentioned "provided
lvalue has no side-effects". Isn't the expression ++lvalue the same as
lvalue = lvalue + 1 always ? Kindly clarify with an example.

Thanks
V.Subramanian

Niels Dekker - no reply address

unread,
Dec 23, 2009, 11:11:38 AM12/23/09
to
V.Subramanian wrote:
> Stroustrup in his book "The C++ Programming Language - Third Edition
> (NOT the special third edition)", mentions the following in page 125
> in section "6.2.5 Increment and Decrement".
> "By definition, ++lvalue means lvalue += 1, which again means lvalue =
> lvalue + 1 provided lvalue has no side-effects".
>
> Here I am unable to understand why Stroustrup has mentioned "provided
> lvalue has no side-effects".

For example, suppose you have an object v, of a type similar to
std::vector<int>. You would expect ++v[0] to be equivalent to v[0] = v[0] +
1. But clearly that assumes that doing v[0] doesn't have some side-effect,
like doing std::cout << "Hey, operator[] is called".

Does that answer your question?

--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center


James Kanze

unread,
Dec 25, 2009, 7:10:20 AM12/25/09
to
On Dec 23, 3:12 pm, "subramanian10...@yahoo.com, India"

int* p;
++(*p ++); // which is NOT the same as:
*p ++ = *p ++ + 1; // undefined behavior...

--
James Kanze

0 new messages