|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
I'm stuck with the order of evaluation of operands. With this simple statement : some_type foobar = foo() * bar(); gcc 4.1 executes : bar foo operator* Is the order in which all functions are executed standard ? May I rely on such a detail ? Thanks. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Dec 5, 3:53 pm, Frédéric <m...@il.it> wrote:
> Hi, > > I'm stuck with the order of evaluation of operands. With this simple > statement : > > some_type foobar = foo() * bar(); > > gcc 4.1 executes : > bar > foo > operator* > > Is the order in which all functions are executed standard ? May I rely on > such a detail ? The fact that you're asking this question says a lot about the wisdom of relying on the order of side-effects in a single expression. If you don't know, do you expect the maintenance programmer who follows you to know? If foo() and bar() must be evaluated in a specific order, write the calls to them in separate statements. It's much clearer about what's going on. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Wed, 05 Dec 2007 16:14:15 -0800, Owen Jacobson wrote:
> On Dec 5, 3:53 pm, Frédéric <m...@il.it> wrote: >> Hi, >> >> I'm stuck with the order of evaluation of operands. With this simple >> statement : >> >> some_type foobar = foo() * bar(); >> >> gcc 4.1 executes : >> bar >> foo >> operator* >> >> Is the order in which all functions are executed standard ? May I rely >> on such a detail ? > ------ > > The fact that you're asking this question says a lot about the wisdom of > relying on the order of side-effects in a single expression. If you > don't know, do you expect the maintenance programmer who follows you to > know? The answer was expected, hence the "detail" where evil lies. I was just wondering if I missed something. Thanks for your time. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Frédéric wrote:
> > I'm stuck with the order of evaluation of operands. With this simple > statement : > > some_type foobar = foo() * bar(); > > gcc 4.1 executes : > bar > foo > operator* > > Is the order in which all functions are executed standard ? May I rely on > such a detail ? > ... No and no. Read the FAQ on "sequence points". In practice people sometimes write expressions like int r = rand() + rand() * 2; and when the program starts behaving differently on different platforms, they immediately assume that the problem is caused by different implementations of 'rand()' in each platform's standard library. So they replace the standard 'rand() with their custom 'my_rand()', uniform across all platforms int r = my_rand() + my_rand() * 2; and then observe, puzzled and amazed, that the program still continues to behave differently for different platforms/builds/compiler options ![]() -- Best regards, Andrey Tarasevich |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Dec 6, 12:53 am, Frédéric <m...@il.it> wrote:
> I'm stuck with the order of evaluation of operands. With this > simple statement : > some_type foobar = foo() * bar(); > gcc 4.1 executes : > bar > foo > operator* > Is the order in which all functions are executed standard ? > May I rely on such a detail ? No. It's unspecified, and may vary---even in the same implementation. Change the form of the expression, or the level of optimization, and the order may change. All you're really guaranteed is that bar and foo will be called before operator* (for obvious reasons, and of course, that operator* will be called before operator=), and that all of the functions will be called before the next encompassing sequence point. And that the functions will be called in some order; the compiler is not allowed to run them in parallel on a multi-core machine, or interleaf there execution in any way. (Note that it can interleaf parts of sub-expressions other than function calls. If you write a()*b() + c()/d(), a, b, c and d can be called in any order---in addition, addition, there are no ordering constrains between a(), b() and operator/(), nor between c(), d() and operator*().) -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
|
![]() |
| Outils de la discussion | |
|
|