[Bf-committers] Style issue

Davide Vercelli davide.vercelli at gmail.com
Fri Apr 14 23:45:22 CEST 2006


> If your functions are truely functions in the mathimatical sense (a set of
> inputs yields exactly one output), then it's fine.
>
> It's just side effects that are troublesome.   If the compiler is going to
> short-circuit the evaluation, then it should do it from left to right, but it
> is not required to short-circuit it.

I'm afraid there's some confusion here. As far as I know, the problem
that you mention is given when many functions are involved in a single
expression, such as:

int a = f(1) + g(2) + h(3);

Now, here there's no guarantee that f() will be executed before g():
the order of execution is undefined to allow implementations to be
smart.

But here:

if (f(1) && g(2)) {/* */ }

f() WILL be executed before g(), *otherwise is not C nor C++*. A
compiler not implementing short circuit would break all the code in
the form:

p && p->member

that is, half the programs in the world I guess.

To make my point even clearer, here's what Bjarne Stroustrup says
about the matter in "The C++ Programming Language", §6.2.2 Evaluation
Order:

"The order of evaluation of subexpression within an expression is
undefined. In particular, you cannot assume that the expression is
evaluated left to right. [...] The operators , (comma), && (logical
and), and || (logical or) guarantee that their left hand operand is
evaluated before their right hand operand."

UncleZeiv


More information about the Bf-committers mailing list