Boost logo

Boost :

From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2004-02-20 15:40:46


Hi Thorsten!

"Thorsten Ottosen" <nesotto_at_[hidden]> escribió en el mensaje
news:c11l60$aql$1_at_sea.gmane.org...
>
>
> "Fernando Cacciola" <fernando_cacciola_at_[hidden]> wrote in message
> news:c10gfp$qi0$1_at_sea.gmane.org...
>
> [snip]
> > inline point transform ( point const& p )
> > {
> > return point(2.0* p.x * p.y, 0.5 * p.x * p.y);
> > }
> > void foo()
> > {
> > point p (..whatever...);
> > p = transform(p);
> > }
> >
> > On many C++ implementations, the code above will yield an incorrect
result
> > because the return object will actually be constructed in place,
producing
> > an actual code equivalent to:
> >
> > p.x = 2.0 * p.x * p.y ;
> > p.y = 0.5 * p.x * p.y ;
> >
> > and you can see how the aliasing resulted in the _argument_ "p.x" being
> > modified inside the function.
> >
>
> Please educate me, is that really legal for a C++ implementation to do
what
> you're saying?
>
Well, I checked and I was wrong considering the way I wrote it.
12.2 is the section that says that a result object can be constructed
directly in the place of the caller's LHS.
But, 12.2/2 explicitely says that in the case of: a=f(a) a temporary is
required either for the argument or the result in order to prevent aliasing
problems.

Anyway, it 's easy to see that if the LHS alias the argument the problem I
mentioned still holds:

point a ;
point& ra = a;
ra = transform(a);

Fernando Cacciola
SciSoft


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk