Re: [SLUG] GCC -- anomaly or feature?

From: Paul Braman (aeon@tampabay.rr.com)
Date: Thu Aug 16 2001 - 17:27:06 EDT


On Wed, 15 Aug 2001, Ed Centanni wrote:

> I found out today that when you call a function with functions as
> arguments:
>
> DoThisThing(with1(thing), with2(thing), with3(thing));
>
> the "with" functions do not get called in left to right (1,2,3) order.

There is nothing in the language that guarantees the order in which the
parameters are evaluated in this situation.

> I had to break them out thus:
>
> int a = with1(thing);
> int b = with2(thing);
> int c = with3(thing);
> DoThisThing(a,b,c);
>
> to ensure that the functions were called in the right order.

This is the correct code to write to get the behavior you want. It is
easier to read to understand that with2() depends on the fact that with1()
has been called first. This should be well documented with these
functions.

A lot of people like to write a bunch of statements inline like your first
example but it is always more readable to create temporary variables so
you can see what order things are done in.

> My question is: Is there a compiler option that forces the arguments to
> be evaluated in the desired (left-to-right) order. I tried -O0 (that's
> dash oh zero -- no optimizations) and the always popular
> -fno-defer-pop. No joy. BTW, that last one is NOT a compiler option
> to tell gcc to dis your dad.

It should not be the responsiblity of the compiler to get this right. It
is the responsibility of the programmer.

Paul Braman
aeon@tampabay.rr.com
Authorized C++ Developer



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 19:34:48 EDT