PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Evaluation order and operator precedance
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Evaluation order and operator precedance

Réponse
 
LinkBack Outils de la discussion
Vieux 05/12/2007, 23h53   #1
Frédéric
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Evaluation order and operator precedance

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.
  Réponse avec citation
Vieux 06/12/2007, 00h14   #2
Owen Jacobson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Evaluation order and operator precedance

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.
  Réponse avec citation
Vieux 06/12/2007, 03h01   #3
Frédéric
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Evaluation order and operator precedance

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.
  Réponse avec citation
Vieux 06/12/2007, 06h28   #4
Andrey Tarasevich
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Evaluation order and operator precedance

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


  Réponse avec citation
Vieux 06/12/2007, 16h09   #5
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Evaluation order and operator precedance

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h21.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14306 seconds with 13 queries