Karl Tomlinson wrote:

Aryeh Gregor writes:

The compiler is required to use the move constructor (if one exists) instead of 
the copy constructor when constructing the return value of a function, and also 
when initializing an object from the return value of a function, or assigning 
the return value of a function.  So if you have

 Foo GetFoo() { Foo f(1, 2, 7); /* do lots of stuff to f */ return f; }
 void MyFunction() { Foo f = GetFoo(); }

the copy constructor of Foo will not get called anywhere.
I guess that means that with

 struct Bar {
   Bar(Foo f) : mF(f) {}
   Foo GetFoo() { return mF; }

   Foo mF;
 }

GetFoo() would give away what mF owns?

If so, can we require that be more explicit somehow?
No, Aryeh's comment only applies to local variables.

Foo GetFoo()
{
   Foo f1, f2;
   if (rand() % 2)
     return f2;
   return f1;
}

Here C++03 compilers may find it hard to do NRVO because we're returning different locals. But when a C++11 compiler tries and fails to do NRVO, it then tries the move constructor.

Note that using ? : in this case does not work, since that results in a Foo& which therefore gets copy constructed. See http://stackoverflow.com/a/19698477

--
Warning: May contain traces of nuts.
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to