Re: Guarantee of side-effect free assignment
on Sun Oct 07 2007, alfps-AT-start.no ("Alf P. Steinbach") wrote:
* James Dennett:
Alf P. Steinbach wrote:
I can find no such guarantee in the standard. It
seems the compiler is free to rewrite
p = new S();
as
p = operator new( sizeof( S ) );
new( p ) S();
What would grant the compiler freedom to deviate in such an
observable way from the semantics of the abstract machine (in
which, I hope it is clear, the rhs of an assignment is evaluated
before its result -- if there is one -- is assumed to be known).
Tjat rule is not only not clear, it seems to be non-existent.
Nobody has so far been able to come up with chapter and verse.
And nobody will. It's like expecting to find specific language that
says this function does not return zero:
int f()
{
return 1;
}
The standard says what the language must do, and there is no need to
mark any specific deviation from that behavior as illegal. If there
were, the standard would be infinitely large.
I hate the term "rewrite" to describe the kinds of optimizations being
considered here. "Rewrites" are only allowed inasmuch as they are not
observable from the point of view of the language specification, which
essentially means the language has to do what the standard says, but
don't look at the generated assembly language and expect to see it
doing that in the way you expect. Anyone who says something of the
form "the compiler is allowed to take this C++ code over here and
rewrite it as that C++ code over there" is lying unless the two pieces
of code actually are specified to have precisely identical effects.
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.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://www.comeaucomputing.com/csc/faq.html ]