Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Dave Abrahams Newsgroups: comp.lang.c++.moderated,comp.lang.c++ Subject: Re: C++11 rvalue reference question Date: Tue, 6 Dec 2011 11:59:50 -0800 (PST) Organization: unknown Lines: 39 Sender: lang-cpp-request@vandevoorde.com Approved: musiphil@bawi.org Message-ID: References: <6b790921-3729-462f-8821-00dce04b5420@g7g2000vbd.googlegroups.com> NNTP-Posting-Host: tjFtkqNXHK+C2ckWmoxv1IucHYij6X347ZwhN6yTABo= Content-Type: text/plain X-Trace: news.albasani.net JeT30CSKTewy+XIbNdXcYfE3Sf2kRBk4x6uFyVc1O7V6OvFsB/dLkLRclnzP5FELMfUqDPNbIawKvo2Hu46vgw== X-Complaints-To: abuse@albasani.net NNTP-Posting-Date: Tue, 6 Dec 2011 19:59:52 +0000 (UTC) X-Mailer: Perl5 Mail::Internet v2.05 X-Submission-Address: lang-cpp-submit@vandevoorde.com Cancel-Lock: sha1:CVIdwqPozwndN3nM8AVi3+b1sPI= X-Original-Date: Mon, 05 Dec 2011 22:39:33 -0500 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++.moderated:2622 comp.lang.c++:12103 on Mon Dec 05 2011, Andrew Tomazos wrote: > In C++03 many times it was recommend to write such a function as > follows: > > void f(O& out, const I& i) > { > .... // mutate result based on i > if (,,,) > throw E(...); > } > > I i = ...; > O o; > f(o,i); > > I suspect this is to avoid the (potentially expensive) copy > construction as the temporary O instance is copied from f's stack > frame to the callers. Probably, but the code above has always been a bad idea even in C++03. Every compiler implements copy elision, so O o = f(i); is bound to be just as efficient, if not more so. http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/ Cheers, -- Dave Abrahams BoostPro Computing http://www.boostpro.com [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]