On Thu, Jan 7, 2010 at 2:50 PM, Albert Wong (王重傑) <ajw...@google.com> wrote:
> Is there something wrong with returning by copy, and relying on the > compiler to execute a return value optimization? > I'm not totally sure what your comment is saying. If you are saying that everywhere in the code can return by value instead of by const ref and the compiler will optimize it equivalently, you are wrong; I was under the same misapprehension until yesterday. We've verified that even in the best case (full optimizations, all functions visible to the compiler, simple bodies), returning a member by value instead of by const ref takes more code. If you are saying that the RVO exists and matters, then of course you're correct. When you write code like this: std::string foo() { std::string a; // Calculations with a return a; } ...The compiler uses the RVO to avoid copying |a| to the return value at EOF, and instead just allocate |a| directly to the return slot. This is why we prefer return-by-value to return-by-outparam where possible: RVO makes it just as cheap, and clearer. But neither is as cheap as return-by-const-ref if you've already got the referenced object sitting around, as you do on class member accessors; it's one copy versus zero. PK
-- Chromium Developers mailing list: chromium-dev@googlegroups.com View archives, change email options, or unsubscribe: http://groups.google.com/group/chromium-dev