Is preincrementing the same variable twice valid c++?

E

Eric Lilja

...or does it violate the rule that a variable may not be modified more
than once between two sequence points?

int n = 0;
std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;

- Eric
 
G

Gianni Mariani

Eric said:
..or does it violate the rule that a variable may not be modified more
than once between two sequence points?

int n = 0;
std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;

It's undefined.
 
P

Pete Becker

Eric said:
..or does it violate the rule that a variable may not be modified more
than once between two sequence points?

There's a sequence point before each function call (i.e. before each <<
function), so that rule doesn't apply. But the order of evaluation of
arguments is unspecified, so you can't count on any particular order for
the various values of n.
int n = 0;
std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;

- Eric


--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

There's a sequence point before each function call (i.e. before each <<
function), so that rule doesn't apply. But the order of evaluation of
arguments is unspecified, so you can't count on any particular order for
the various values of n.

That's not true
^^^^^^^^^^^^^^^^^
This has to execute before the << ' ' can, otherwise there won't be a
ostream as first parameter to the << ' '-call.
 
A

Alf P. Steinbach

* Erik Wikström:
That's not true

What isn't true?

^^^^^^^^^^^^^^^^^
This has to execute before the << ' ' can, otherwise there won't be a
ostream as first parameter to the << ' '-call.

Arguments can be evaluated before anything else happens.
 
P

Pete Becker

Erik said:
That's not true

^^^^^^^^^^^^^^^^^
This has to execute before the << ' ' can, otherwise there won't be a
ostream as first parameter to the << ' '-call.

The order of the calls to operator << is well defined, but the order of
evaluation of the arguments is unspecified. The compiler can compute the
value of the first ++n, then the n, then the final ++n. It can also do
them in reverse order, or any other order.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
R

Ron Natalie

Pete said:
There's a sequence point before each function call (i.e. before each <<
function), so that rule doesn't apply. But the order of evaluation of
arguments is unspecified, so you can't count on any particular order for
the various values of n.
That rule does apply. Since the function parameters may be evaluated
in any order, both ++n subexpressions may be executed before ANY
function call occurs. Hence you will have the undefined behavior or
twice modifying a value between sequence points. The rule requires
that be true for ANY ALLOWABLE ORDERING of the expression.
 
R

Ron Natalie

Erik said:
That's not true

Partially true.
^^^^^^^^^^^^^^^^^
This has to execute before the << ' ' can, otherwise there won't be a
ostream as first parameter to the << ' '-call.

The real issue is that the compiler is free to execute both ++n
subexpressions in any order and at any time prior to the call
of the operator<< where they are parameters. Specifically it
can execute them BOTH before either operator << call ,causing
undefined behavior.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top