From -380139707377630035
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,8ba01a147f26fb88
X-Google-Attributes: gidf78e5,public
From: David R Tribble <david@tribble.com>
Subject: Re: ++var and throw
Date: 1999/09/27
Message-ID: <37EFC574.E2ED5E5@tribble.com>#1/1
X-Deja-AN: 530013294
X-NNTP-Posting-Host: dalnt9.beasys.com
Content-Transfer-Encoding: 7bit
Approved: stephen.clamage@sun.com (comp.std.c++)
References: <7sb2gi$qbg3@interserv.etn.com> <7sbd9u$4uk$1@engnews1.eng.sun.com>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: newsmaster@beasys.com
X-Trace: news.beasys.com 938460900 1894 206.189.43.219 (27 Sep 1999 19:35:00 GMT)
Organization: The Vast Right-Wing Conspiracy
Mime-Version: 1.0
NNTP-Posting-Date: 27 Sep 1999 19:35:00 GMT
Newsgroups: comp.std.c++
Originator: clamage@taumet


Ed Brey <brey@afd.mke.etn.com> writes: 
>> In the spirit of the ++var thread, I have a spin-off question (I
>> coincidentally just ran into this while doing real work).  Consider
>> the statement:
>> 
>> *p++ = f();
> 
>> If f() throws, is p guaranteed to not be incremented?  Where does
>> the IS define this or say that it is undefined?
>> ...
>> I hope that does not mean that it can be interrupted by the throw
>> of an exception because that could leave p in an unstable state.

Steve Clamage wrote:
> The expression statement has two side effects:
> 1. assignment to *p
> 2. modifying p by incrementing its value
> 
> Side effects cannot be delayed beyond reaching the applicable
> sequence point. Whether completion occurs sooner than the sequence
> point is unspecified.  [...]
> 
> You therefore should not write code that depends for correctness
> on multiple side effects in expressions, if exceptions can occur.

Exactly; don't do it.  Do something instead that will leave p in a
well-defined state, such as:

    *p = f();    // might throw, p is unchanged
    p++;         // now p is changed safely
                 //  (assuming p is not an iterator)

When you play with fire (exceptions), expect to get burned once in
a while.

-- David R. Tribble, david@tribble.com --


[ 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              ]




