
Hi Thorsten! "Thorsten Ottosen" <nesotto@cs.auc.dk> escribió en el mensaje news:c11l60$aql$1@sea.gmane.org...
"Fernando Cacciola" <fernando_cacciola@hotmail.com> wrote in message news:c10gfp$qi0$1@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
because the return object will actually be constructed in place,
result 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