public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/52901] invalid rvalue reference
Date: Sun, 08 Apr 2012 14:15:00 -0000	[thread overview]
Message-ID: <bug-52901-4-88v4r3VotC@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-52901-4@http.gcc.gnu.org/bugzilla/>

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;
}


  parent reply	other threads:[~2012-04-08 14:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-07 16:35 [Bug c++/52901] New: " dieter.lucking at googlemail dot com
2012-04-08 13:49 ` [Bug c++/52901] " daniel.kruegler at googlemail dot com
2012-04-08 14:15 ` redi at gcc dot gnu.org [this message]
2012-04-08 15:33 ` marc.glisse at normalesup dot org
2012-04-08 18:06 ` redi at gcc dot gnu.org
2012-04-08 18:09 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-52901-4-88v4r3VotC@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).