Topic: Implicit copy-ctor on function return
Author: "Manfred von Willich" <manfred@techniroot.co.za>
Date: Tue, 4 Jul 2006 18:26:03 CST Raw View
What is the status of the optional implicit copy-ctor that the compiler
may invoke on a function return?
AFAICT, there should seldomly be any need for an implicit
copy-on-return. If the compiler is mandated to arrange that the target
temporary is the same as the returned reference (e.g. by regarding x
referenced in "return x;" as a name for the returned temporary), the
copy is avoided, with the following exception.
With multiple return statements, implicit copying would be permitted
only where at least one return expression is named object of exactly
the target type declared locally, and at least one is not the same
expression.
Good optimising compilers may generally do this, but it is sometimes
necessary to be able to ensure that the unwanted overhead does not
sneak in.
An alternative (my preference) would be to have an explicit indication
of which locally declared object in a function refers to the returned
temporary, e.g.:
myclass f () {
// some code
myclass return x;
// more code
return x; // neither copied nor destroyed
}
Manfred
---
[ 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 ]