--- At Thu, 24 Apr 2003 16:56:40 +0100, Ben Hutchings wrote:
dick.bridges@tais.com wrote:
I don't understand C++ well enough to grok a compiler's interpretation of the code, but I seems like I'm creating an extra copy for the return. <snip>
Logically the local variable is copied to the the caller, but since the type of the returned expression matches the return type the compiler is allowed to optimise away that copy, even though the return type has a user-defined copy constructor. In this case you always return a certain named variable, so the compiler can arrange to construct it in the memory location where the caller expects the returned value. This is called the named-return-value optimisation, or NRVO.
I dont want to start any kind of war, and this is a bit off topic, but I was investigating NVRO and found a few discussions with Scott Meyers that indicated that naming is not relevant to the optimization. That this: shared_ptr<int> maybe_get_a_pointer() { return shared_ptr<int>(); } is perfectly reasonable and can also be optimized. Is there more recent and complete discussion concerning this optimization? Is naming actually required? ...Duane