Re: Guarantee of side-effect free assignment
On Oct 7, 8:01 pm, AlbertoBarb...@libero.it (Alberto Ganesh Barbati)
wrote:
Alf P. Steinbach ha scritto:
From discussions in [comp.lang.c++] and
[comp.lang.c++.moderated], as well as articles on the net
about concurrency in C++, I'm reasonably sure that given
#include <iostream>
#include <ostream>
struct S { S(){ throw 123; } int foo(){ return 666; } };
int main()
{
S* p = 0;
try
{
p = new S();
}
catch( ... )
{}
if( p ) { std::cout << p->foo() << std::endl; }
}
there is no guarantee that this code will not end up in a call to
p->foo() with an invalid pointer p, i.e., that might well happen.
The the latest draft N2369 effectively replaced the controversial
concept of "sequence points" with the new concept of "sequenced before"
(see 1.9/14 for details). Paragraph 5.17/1 has therefore been rewritten
and, if I interpret it correctly, it rules out this possibility: (with
emphasis added)
"In all cases, the assignment is *sequenced after* the value computation
of the right and left operands, and before the value computation of the
assignment expression."
As the assignment is sequenced after the the computation of the right
operand, it should not occur when such computation terminates
prematurely because of a exception.
I'm not sure that that changes anything. The problem is that
calling the constructor isn't part of the "value computation" of
a new expression, it is a side effect. And unless side effects
are "sequenced before", we're back where we started.
--
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
---
[ 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://www.comeaucomputing.com/csc/faq.html ]