Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++.moderated > #2611

Re: C++11 rvalue reference question

From Daniel Krügler <daniel.kruegler@googlemail.com>
Newsgroups comp.lang.c++.moderated, comp.lang.c++
Subject Re: C++11 rvalue reference question
Date 2011-12-05 15:44 -0800
Organization A noiseless patient Spider
Message-ID <jbhp6t$3mg$1@dont-email.me> (permalink)
References <6b790921-3729-462f-8821-00dce04b5420@g7g2000vbd.googlegroups.com>

Cross-posted to 2 groups.

Show all headers | View raw


On 2011-12-05 06:38, Andrew Tomazos wrote:
> class O {...};
> class I {...};
> class E {...};
>
> Suppose we have some function f that takes an instance of class I as
> input and produces an instance of class O as output (throwing an
> instance of class E on an error):
>
> O f(const I&  i)
> {
>      O result;
>      ... // mutate result based on i
>      if (...)
>         throw E(...);
>
>      return result;
> }

OK, so far.

> I i = ...;
> O o = f(i);
>
> 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.
> The downside is that you then can't do things like:
>
> f(i).callSomeOFunction()
>
> or
>
> someFunctionTakingAnO(f(i))

Yes.

> My question is with the advent of move constructors, xvalues and
> rvalue references in C++11, is there anyway to address this issue?
>
> What would this mean/do:
>
> O&&  f(const I&  i)
> {
>      O result;
>      ... // mutate result based on i
>      if (...)
>         throw E(...);
>
>      return result; // or std::forward(result) ?
> }
>
> or is it broken?

This *is* broken, for the simple reason that this will produce a
"dangling" reference to an automatic object that has been locally
created in the function and that has been destroyed before the function
call ends. Don't do that!

Make it simple, just use return type O (without cv-qualifiers). The
language has intentionally been adapted to enforce the compiler to first
check whether the local object "result" can be be provided as an rvalue
to the return statement to enable move semantics if the type provides some.

HTH & Greetings from Bremen,

Daniel Krügler


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

Back to comp.lang.c++.moderated | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

C++11 rvalue reference question Andrew Tomazos <andrew@tomazos.com> - 2011-12-04 21:38 -0800
  Re: C++11 rvalue reference question Daniel Krügler <daniel.kruegler@googlemail.com> - 2011-12-05 15:44 -0800
  Re: C++11 rvalue reference question Arne L <a.luenser@googlemail.com> - 2011-12-05 15:51 -0800
    Re: C++11 rvalue reference question SG <s.gesemann@gmail.com> - 2011-12-06 12:00 -0800
  Re: C++11 rvalue reference question Dave Abrahams <dave@boostpro.com> - 2011-12-06 11:59 -0800
    Re: C++11 rvalue reference question Ebenezer <woodbrian77@gmail.com> - 2011-12-11 00:52 -0800

csiph-web