From 6208372041029945315
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,7522e5259f7267d6
X-Google-Attributes: gidf78e5,public
From: jpotter@falcon.lhup.edu (John Potter)
Subject: Re: x = y = z;  Undefined?
Date: 2000/01/12
Message-ID: <387b3c32.7816532@news.csrlink.net>#1/1
X-Deja-AN: 571227991
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
References: <MPG.12ddaa92533ae9679896a6@news.supernews.com> <t7so0bqea7.fsf@calumny.jyacc.com> <3876254a.30903561@news.csrlink.net> <derobert-9FC87E.01591309012000@news.erols.com> <3879ca08.3142429@news.csrlink.net> <derobert-8223E1.23372510012000@news.erols.com>
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Trace: mail2news.demon.co.uk 947607787 mail2news:18873 mail2news mail2news.demon.co.uk
Organization: Newscene Public Access Usenet News Service (http://www.newscene.com/)
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
Nntp-Posting-Date: Tue, 11 Jan 2000 09:47:08 CST
Newsgroups: comp.std.c++

On Tue, 11 Jan 2000 20:51:15 CST, Anthony DeRobertis
<derobert@erols.com> wrote:

: In article <3879ca08.3142429@news.csrlink.net>, jpotter@falcon.lhup.edu 
: (John Potter) wrote:
: 
: >On Mon, 10 Jan 2000 08:28:54 CST, Anthony DeRobertis
: ><derobert@erols.com> wrote:
: >
: >: In article <3876254a.30903561@news.csrlink.net>, 
: >: jpotter@falcon.lhup.edu (John Potter) wrote:
: >: 
: >: >could someone give an example of translation which would produce
: >: >something other than expected?
: >: >
: >: >   (i1 = i2) += i3;
: >: 
: >: Well, on my PowerPC this would generate (unoptimized):
: >: -- sequence point --
: >: or i1,i2,i2
: >: add i1,i1,i3
: >: -- sequence point --
: >: 
: >: Which could legally be re-arranged:
: >: add i1,i1,i3
: >: or i1, i2, i2
: >
: >I don't think so.
:  
: <snip>
: 
: The standard, by saying that a value can't be modified twice between 
: sequencing points, has disallowed this dependency. The compiler need not 
: check if a new value for an variables depends on another new value 
: inside the same sequencing point, and there is no reason to expect that 
: it does check.

I disagree.  The standard requires that expressions be evaluated as
written.  The old value of a variable which is modified may only be
used to calculate the new value.  The new value may be used.

   i1 = i2  calculates the value 2, it is an lvalue for i1

   (i1 = i2) += i3  calculates the value 5, it is an lvalue for i1

The undefined behavior involves the final value of i1.  The compiler
is not required to check for undefined behavior and there is no
reason to expect that it does check.  It is required to compute the
value 5.  Your code above does not compute the value 5.

: One could concieve a compiler that would decide 'ok, the final 
: destination is i1' and then add three too it. Then manage the assign 
: i1=i2 after it. Since there can be no more than one assignment to a 
: single variable in a sequencing point, there's nothing wrong with this 
: implementation, either.

No.  The compiler could compute the value 5 store it in i1 and then
store the value 2 from i2 in i1.  It might even store the & or | of
those two values.  Adding 3 to the old value of i1 is not present
in the expression.

You seem to interpret undefined behavior as a license for the compiler
to do stupid things.  I interpret it as a license to do reasonable
things on the assumption that there is no undefined behavior without
needing to verify that.  Any compiler that does anything based upon
detecting undefined behavior better be issuing a diagnostic.

Consider:
   int a[] = { 11, 13 }, i2(2), i3(3);
   int& i1(a[0]);
   int* p;
   *((p = &(i1 = i2)) + 1) += i3;

There is no undefined behavior and a[] == { 2, 16 }.

An interesting point is that in this part of the standard (5) where it
talks about undefined behavior, the examples label it as unspecified
behavior.  I assume that the normative makes the non-normative examples
errors.

John

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]




