expression evaluation order

N

n.torrey.pines

It's my understanding that the standard guarantees that h() returns 10
here because of the "sequence points". Is this right?

int f(int& i) { i += 1; return 1; }

int g(int& i) { i += 2; return 2; }

int h() {
int x = 0;
int y = f(x) + g(x) + g(x);
return x+y;
}
 
A

Alf P. Steinbach

* (e-mail address removed):
It's my understanding that the standard guarantees that h() returns 10
here because of the "sequence points". Is this right?

No, the order of evaluation does not matter here.

int f(int& i) { i += 1; return 1; }

int g(int& i) { i += 2; return 2; }

int h() {
int x = 0;
int y = f(x) + g(x) + g(x);
return x+y;
}

Try to avoid side effects.
 
A

Andrew Koenig

Alf P. Steinbach said:
* (e-mail address removed):
No, the order of evaluation does not matter here.

I think the point is that because f and g are functions, in the expression

f(x) + g(x) + g(x)

each function must be evaluated in its entirety before any other function is
evaluated. Moreover, there is a sequence point before each function is
called. Therefore, although it is indeterminate in what order the functions
are called, that fact doesn't matter because the result is the same
regardless of the order in which they are called.

On the other hand, the foregoing doesn't affect my opinion that the program
should be taken out and shot :)
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top