Topic: Temporary object optimization
Author: Frode.Nilsen@mison.no
Date: Wed, 14 Aug 2002 10:04:22 CST Raw View
Hi all.
I was reading some earlier postings about "named return values", and then
the 12.8 15 paragraph in the standard. Result -> total confusion.
My understanding of this was that both the following situations was
optimized by this paragraph:
class A;
A x() { return A; }
A y() { A tmp; if( something) tmp.go() else tmp.no(); return tmp; }
A first = x(); // copy optimized away by first sentence
A second = y(); // copy optimized away by second sentence
After reading those postings I got the impression that only the second are
optimized, and that the first had to be written
A x() { A tmp; return tmp; }
to be optimized.
Anyone with a clear definition of this ?
sincerely
frode
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
Author: jpotter@falcon.lhup.edu (John Potter)
Date: Wed, 14 Aug 2002 21:30:05 GMT Raw View
On Wed, 14 Aug 2002 10:04:22 CST, Frode.Nilsen@mison.no wrote:
> I was reading some earlier postings about "named return values", and then
> the 12.8 15 paragraph in the standard. Result -> total confusion.
> My understanding of this was that both the following situations was
> optimized by this paragraph:
> class A;
Definition assumed.
> A x() { return A; }
return A(); assumed.
> A y() { A tmp; if( something) tmp.go() else tmp.no(); return tmp; }
> A first = x(); // copy optimized away by first sentence
> A second = y(); // copy optimized away by second sentence
> After reading those postings I got the impression that only the second are
> optimized, and that the first had to be written
> A x() { A tmp; return tmp; }
> to be optimized.
No. There are two optimizations in each case.
First is copy constructed from the return value of x. Second is copy
constructed from the return value of y. The first sentence allows the
return value to be considered another name for the variable and
construction of the return value to be performed directly in the
variable. This is normally implemented by translating return by value
into pass a pointer to raw memory.
In x, return A() constructs an A and copy constructs the return value
from it. The first sentence allows removal of this copy by
constructing the A directly in the return value. Since the return
value had been replaced by first, both copies are removed.
In y, return tmp copy constructs the return value from tmp. The
second sentence allows removal of the temporary return value. I have
no idea how to do that, but it is easy to use that space for tmp. I
guess it is a word game. Anyway, for this example, the return value
was replaced by both tmp and second. Interesting that the lifetime of
second starts at the end of the first line of the function y.
The cases which are covered are return of an unnamed temporary and
return of a local variable. Confusion is usually caused by return of
the result of some expression which is a reference.
A operator+ (A const& lhs, A const& rhs) {
return A(lhs) += rhs; // assume that += is a member
}
The A must be copy constructed from lhs and the return value must be
copy constructed from the return of +=. Neither may be removed by
12.8/15. The implementation may do anything using "as if" if my
program can't tell. Since placing a side effect in the dtor would
allow detection, using "as if" is unlikely but I can't tell. :)
John
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]