Order of expression evaluation

S

Stasa Medjedovic

Hello,

one question from me. Let E1, E2, E3 be expressions. Is the order of
expression evaluation defined by the C++ (or C) standard for these
statements (for example):

1. cout << E1 << E2 << E3;
2. f(E1,E2,E3);
3. E1 + E2;
and similar ...

Or it is arbitrary? And, is it bad programming practise for a program
to depend on this order?

Thanks in advance.
 
G

gnuyuva

Hello,

one question from me. Let E1, E2, E3 be expressions. Is the order of
expression evaluation defined by the C++ (or C) standard for these
statements (for example):

1. cout << E1 << E2 << E3;
2. f(E1,E2,E3);
3. E1 + E2;
and similar ...

Or it is arbitrary? And, is it bad programming practise for a program
to depend on this order?

Thanks in advance.

The expression evaluation is not arbitrary and it hugely depends on
the operators involved in the expression. Search for 'c++ precedence
of operators' or refer to your good old/new c++ book.
 
S

Stasa Medjedovic

The expression evaluation is not arbitrary and it hugely depends on
the operators involved in the expression. Search for 'c++ precedence
of operators' or refer to your good old/new c++ book.

The books say nothing about these situations. Only few of them say
that evaluation of expressions for statements like these is undefined
by the standard, and that it is a bad programming practise to write a
program where results of expressions E1, E2 and E3 depend on each
other. If you are so sure about what you are saying, think of this:

i = 0;
cout << i++ << i++;

What's the output ?
 
G

gnuyuva

The books say nothing about these situations. Only few of them say
that evaluation of expressions for statements like these is undefined
by the standard, and that it is a bad programming practise to write a
program where results of expressions E1, E2 and E3 depend on each
other. If you are so sure about what you are saying, think of this:

i = 0;
cout << i++ << i++;

What's the output ?

Well, I was just talking about the operator precedences and I agree
with you that such constructs result in UB (the headaches ofcourse).
Any good programmer won't write code in this manner ;-)
 
K

Kai-Uwe Bux

Stasa said:
Hello,

one question from me. Let E1, E2, E3 be expressions. Is the order of
expression evaluation defined by the C++ (or C) standard for these
statements (for example):

1. cout << E1 << E2 << E3;
2. f(E1,E2,E3);
3. E1 + E2;
and similar ...

Or it is arbitrary?

It is unspecified. See [5/4] and, for function calls, [5.2.2/8].
And, is it bad programming practise for a program to depend on this
order?

Yes.


Best

Kai-Uwe Bux
 
A

acehreli

Well, I was just talking about the operator precedences and I agree
with you that such constructs result in UB (the headaches ofcourse).

There may be undefined behavior (UB) in general, but in this case the
order is just "unspecified."

In this example 'i' is not modified more than once between sequence
points. Representing the two 'i++'es with a() and b() and the two <<
operations with A() and B(), it boils down to this:

B(A(cout, a()), b());

These are defined:

a() before A()
A() and b() before B()

This is unspecified:

either A() or b() first

Ali
 
K

Kai-Uwe Bux

There may be undefined behavior (UB) in general, but in this case the
order is just "unspecified."
Huh?

In this example 'i' is not modified more than once between sequence
points. Representing the two 'i++'es with a() and b() and the two <<
operations with A() and B(), it boils down to this:

B(A(cout, a()), b());

These are defined:

a() before A()
A() and b() before B()
Correct.

This is unspecified:

either A() or b() first

Correct, but I don't see a sequence point between a() and b(), which
represent the two modifications of i. As you point out, there are sequence
points separating a() from A(), A() from B(), and b() from B(), but none of
these sequence points separates a() from b().


Best

Kai-Uwe Bux
 

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,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top