This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/40295] [C++0x] rvalue-references and builtin types



------- Comment #1 from jwakely dot gcc at gmail dot com  2009-05-29 17:08 -------
This is independent of std::move

#include <cassert>

struct S
{
    S() : buf(5) { }

    int&& get() { return static_cast<int&&>(buf); }

    int buf;
};

int main()
{
    S s;
    int&& r = s.get();
    assert( &r == &s.buf );
}


tmpret.cc: In member function ?int&& S::get()?:
tmpret.cc:7: warning: returning reference to temporary


tmpret: tmpret.cc:16: int main(): Assertion `&r == &s.buf' failed.


Looks like the cast to int&& creates a temporary, which is bound to the
returned reference, triggering the warning.  According to 8.5.3 [dcl.init.ref]
paragraph 5, the reference should be bound to the object, not to a temporary.


-- 

jwakely dot gcc at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|libstdc++                   |c++
           Keywords|                            |wrong-code
            Summary|[C++0x] std::move and       |[C++0x] rvalue-references
                   |builtin types               |and builtin types


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]