http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52901

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-04-08 
14:14:30 UTC ---
This is just a bug in your program, not G++

(In reply to comment #0)
> 
> X&& f() {
>         X x;
>         return std::move(x);
> }

This function is unsafe, it returns a reference to a local variable. You
probably meant it to return X not X&&

It is effectively the same as:

X& f() {
   X x;
   return x;
}

(except G++ warns about that, because it's simpler)


> 
> int main() {
>         cout << "Hello References [1]" << std::endl;
>         X   x0 = f();
>         cout << "x0: " << x0.value << std::endl;
>         X&& x1 = f();

This reference is bound to a variable that went out of scope when f() returned.

>         cout << "No copy construction or assignment expected" << std::endl;
>         cout << "x1: " << x1.value << std::endl;

This accesses deallocated memory.

N.B. you don't even need to use std::move, the compiler will automatically
select the move constructor to create the return value here:

X f()
{
   X x;
   return x;
}

Reply via email to