Discussion: Re: sequence points
Afficher un message
Vieux 22/10/2007, 11h54   #8
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sequence points

Greg Herlihy wrote:

> On Oct 15, 7:49 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
>> Let me slightly modify the example so that all subexpressions have
>> side-effects:
>>
>> int a = 0;
>> int b = 0;
>> a = ( ( a = 5 ), ( b = 4 ) );
>>
>> The comma operator introduces a sequence point that clearly separates
>>
>> a=5 from b=4
>>
>> The question at hand is whether this sequence point also separates
>>
>> a = rhs from a=5

>
> The answer at hand is of course, yes - by transitivity: "b=4" is
> evaluated after "a=5" due to the comma operator between them, whereas
> "b=4" is evaluated before "a=rhs" because the evaluated value of "b=4"
> is the "rhs" assigned to "a" to complete the expression. Therefore the
> "a=rhs" assignment must occur after the "a=5" assignment - because we
> know that the evaluation of "b=4" must take place after the latter and
> before the former.


Thanks. I just looked that up, and indeed the comma operator makes a
guarantee that goes farther than I was aware of. It does not only ensure
that the side effects of b = 4 come after the side effects of a = 5; it
even ensures that the evaluation of b = 4 does not commence before the side
effects of a = 5 have taken place.


> In C++, operands and operators guide the evaluation of an expression -
> but do not mandate a single interpretation (like Java does). So
> "sequence points" were invented as a way to describe just how much
> leeway a C++ compiler does have when evaluating an expression - and by
> the same token - to let a C++ programmer recognize when an expression
> has granted too much latitude to the compiler and could therefore lead
> to an unexpected result.
>
> In this example, the semantics of the expression are as follows:
>
> assign 5 to a as the left operand of the comma expression
> assign b to 4 as the right operand of a comma expression
> evaluate b=4 as 4
> evaluate the entire comma expression to the right hand operand: 4
> assign 4 to a
>
> Next we can determine how much latitude the compiler has in following
> this order. We know that assigning 5 to "a" must be done first (and
> "a" must have the value 5) before proceeding to the next step
> (courtesy of comma operator)
>
> We know that right-hand side must completely evaluate to "4" before
> "4" can be assigned to "a". We cannot be sure that "b" will have the
> value "4" before "b=4" evaluates to 4 - but since there is nothing
> that depends on whether a or b gets their assigned value first - the
> uncertain order of the side effects on the right hand side of the
> expression - makes no difference.
>
> In short, the comma operator cleanly divides the evaluation and side
> effects of "a=5" from the assignments and evaluations that follow it.
> And there is no amount of lawyering that will get around that fact.


Right. I was confused because I thaught the comma operator just inserts a
sequence point that separates side effects.

[snip]


Best

Kai-Uwe BUx
  Réponse avec citation
 
Page generated in 0,08098 seconds with 9 queries