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

++++i;

108 views
Skip to first unread message

Mr Flibble

unread,
Dec 2, 2014, 12:49:24 PM12/2/14
to
Hi,

Is the following undefined behaviour?

int i = 0;
++++i;

Here i is modified twice in the same expression so I think it is UB but
I'm not 100% sure.

/Flibble

Mr Flibble

unread,
Dec 2, 2014, 1:14:30 PM12/2/14
to
Some are saying that it is possibly UB in C++03 but fine in C++11.
Mystified.

/Flibble

Chris Vine

unread,
Dec 2, 2014, 2:43:42 PM12/2/14
to
As am I. But whatever, just use the comma operator (++i, ++i), which is
always right.

Chris

Scott Lurndal

unread,
Dec 2, 2014, 4:15:11 PM12/2/14
to
I guess I don't see why either alternative is superior to i += 2

Mr Flibble

unread,
Dec 2, 2014, 6:27:12 PM12/2/14
to
If 'i' is a bi-directional (not random access) iterator the += 2 won't work.

/Flibble


Mr Flibble

unread,
Dec 2, 2014, 6:58:50 PM12/2/14
to
However for such an iterator operator++ is a function call so there
won't be the possibility of UB.

/Flibble

Martijn Lievaart

unread,
Dec 3, 2014, 3:10:40 AM12/3/14
to
Does this also hold if the iterator is implemented as a pointer?

M4

Chris Vine

unread,
Dec 3, 2014, 6:28:54 AM12/3/14
to
Interesting, on looking further at this, I think that although illegal
in C++98, the expression ++++i is valid in C++11/14.

The complete normative reading of §1.9/15 of C++11, with examples and
parenthetical comments removed, is:

"Except where noted, evaluations of operands of individual operators
and of subexpressions of individual expressions are unsequenced. The
value computations of the operands of an operator are sequenced
before the value computation of the result of the operator. If a side
effect on a scalar object is unsequenced relative to either
another side effect on the same scalar object or a value computation
using the value of the same scalar object, the behavior is undefined."

It is the second sentence which is important. ++++i is in effect
identical in C++11 to ++i,++i. Likewise although the behaviour of this
is unspecified in C++98, I think it is required to increment by 2 in
C++11:

i = ++i + 1

However, this is still undefined in C++98 and C++11:

i = i++ + 1

Turning to the examples given in the standard, the reason why in C++11
this:

i = v[i++];

is undefined is that although the assignment to i is sequenced before
the evaluation of v[i] on the right hand side, the subsequent side
effect comprising the increment of i carried out on the right hand side
is not. So the increment of i on the right hand side and the assignment
to i are unsequenced. On the other hand this:

v[++i] = ++i;

is invalid in C++11 (I think) because, although the assignment is
sequenced before the evaluation of the right hand side, the
prior evaluation of the subexpression ++i on the left hand side is
unsequenced with respect to the evaluation of ++i on the right hand
side.

Err, I think.

Chris

Chris Vine

unread,
Dec 3, 2014, 6:38:58 AM12/3/14
to
On Wed, 3 Dec 2014 11:28:40 +0000
^^^^^^
after

Martijn Lievaart

unread,
Dec 3, 2014, 6:50:17 AM12/3/14
to
All this says to me is that the value of ++i must be known, not that the
side effects have taken place. AFAICS the value of ++i must be of
reference type, not value type, so can be known without the side effect
taking effect.

But that is also just my interpretation. Bottomline, don't do this. ++i; +
+i; has the same effect and is clearer.

M4

M4

Chris Vine

unread,
Dec 3, 2014, 7:35:40 AM12/3/14
to
Pre-increment on a scalar is required by the standard to have completed
its side effects by the time its value is known. That is the reason
why the construction appears to be valid. Its value with respect to a
scalar is stated to be a lvalue (§5.3.2/1). Its side effect is, by
rule, to increment the scalar and its value is its value after the
increment. On a user defined type the effect is whatever the user
provides it to be, and because pre- and post-increment are function
calls on user and library types, different sequencing considerations
apply - the return of a function call is always sequenced after
evaluation of the arguments and the evaluation of any side effects
within the body of the function.

On a sub-note, it is interesting to compare the examples given in §5/4
of C++98 and §1.9/15 of C++11. In C++98 pre-increment 'i = ++i + 1' is
given as an example of unspecified behaviour (which by virtue of the
last sentence of §5/4 is also undefined behaviour). In C++11 it is not:
instead the examples substitute the post-increment 'i = i++ + 1' as an
example of undefined behaviour.

> But that is also just my interpretation. Bottomline, don't do this.
> ++i; ++i; has the same effect and is clearer.

I agree. This is for pedagogical purposes only. I hope it would never
be in live code.

Chris

Martijn Lievaart

unread,
Dec 3, 2014, 2:50:20 PM12/3/14
to
Yeah, ++++i feels like some other unspecified computer language. :-)

M4

Jack Chuge

unread,
Dec 27, 2014, 6:54:13 AM12/27/14
to
Martin, I agree with you, lol

--
Jack
0 new messages