Re: boolian and for contingency
In article <YORrg.11257$j7.315394@news.indigo.ie>, fgothamNO@SPAM.com
says...
[ ... ]
In accordance with operator associativity rules, your expression:
fun1() && fun2() && fun3()
is the same as writing:
( fun1() && fun2() ) && func3()
What is does is:
Call "fun1". If "fun1" evalutes to false, then stop; otherwise, go
ahead and call "fun2". If "fun2" evalutes to false, then stop; otherwise,
go ahead and call "fun3".
Associativity doesn't really have much to do with anything here.
What's relevant is that fact that the '&&' operator establishes a
sequence point, so its left operand is evaluated, then if and only if
its left operand evaluates to 'true' its right operand is evaluated.
Attempting to think of this in terms of associativity leads to
problems. For example, addition is also left associative, so:
x + y + z;
is equivalent to:
(x + y) + z;
The difference is that in this case there's no sequence point during
the evaluation, which means it's entirely possible for z to be
evaluated before x or y. Consider, for example, if x, y and z were
all instances of a rather odd proxy type that lets us see the order
in which the variables are evaluated:
#include <iostream>
class proxy {
char name_;
int value_;
public:
proxy(char name, int value) : name_(name), value_(value) {}
operator int() { std::cout << name; return value; }
};
int main() {
proxy x('x', 1);
proxy y('y', 2);
proxy z('z', 3);
int a = x + y + z;
return 0;
}
Now, even though this is evaluated as '(x+y)+z', the output of the
program is _not_ guaranteed to be 'xyz' -- it could perfectly
legitimately be any of the six possible permutations of the three
letters (e.g. 'zyx').
That's not to say that these are all equally likely -- in fact, I'd
guess most compilers will actually produce 'xyz' more often than not.
It's hard to imagine why a compiler would produce 'yxz' (for one
example) but that's more or less beside the point -- if there was a
reason to do so (even just that the compiler author was feeling
perverse) it would be perfectly legitimate to do so.
--
Later,
Jerry.
The universe is a figment of its own imagination.