Re: Unsequenced/Unspecified
rkldabs@gmail.com wrote:
int f(){cout << "1"; return 0;}
int g(){cout << "2"; return 1;}
void h(int x, int y){}
typedef void (*FP)(int, int);
int main(){
FP fp = h;
(fp)(f(), g()); // Line 1
}
1. With reference to N2798, in the code above, my understanding is
that the function call in Line 1 is a postfix expression and is
treated as a full expression consisting of subexpressions
(fp), f() and g() - $5.2.2.
Is my understanding correct? Is this the correct way to interpret a
function call expression?
Just about. I think there is a fourth component as well, the actual
call of h. The fp part could be more complicated than just a function
pointer, so we first have to evaluate fp and then apply the function
call operator () to the result.
2. The expressions (fp), f() and g() are all evaluated in an
unsequenced manner $5.2.2/8 & $1.9/16. Does this mean that (fp),
f(), g () can be evaluated in any order, or in parallel (e.g. on a
multi processor machine e.g.) or even interleaved with each other?
Unsequenced has to do with when the possible side effects are
guaranteed to be visible, not when they are actually evaluated.
Function calls are separate, and can not be interleaved.
3. Including the section $1.9/3, many previous posts seem to
indicate that the order of function argument evaluation is
unspecified. Is it unspecified or unsequenced? Are unspecified and
unsequenced same?
The unspecified is from the current standard, which doesn't say
anything about threading. N2798 is the draft of a revised standard.
The "sequencing" of operations relative each other are supposed to
clarify what happens, even in a multithreaded execution.
4. The function call "h" has an unspecified output. It could be 12
or 21 but nothing else. Therefore the output is unspecified (limited
options). So the unspecified behaviour of "h" in the code above is a
result of unsequenced argument evaluation of function arguments. Is
this the correct way to interpret the code above? That is
unsequenced function argument evaluation (as contrast to
unspecified order mentioned widely in the previous posts) the
cause, with the effect as unspecified behavior!.
It's really just unspecified evaluation of subexpressions. (fp), g(),
and h() must all be evaluated before we can call the actual function,
h. We just don't know the order, which can possibly vary from one call
to another (more likely if the code appears in more than one place).
Bo Persson
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]