public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/34700]  New: rvalue erroneously binding to non-const lvalue reference
@ 2008-01-07  7:45 eric dot niebler at gmail dot com
  2008-01-15  7:46 ` [Bug c++/34700] C++0x: " dgregor at gcc dot gnu dot org
  0 siblings, 1 reply; 2+ messages in thread
From: eric dot niebler at gmail dot com @ 2008-01-07  7:45 UTC (permalink / raw)
  To: gcc-bugs

Consider the following code, compiled with the latest gcc built from sources,
with the -std=c++0x flag:

#include <iostream>
struct S
{
    S() {}
    S(S &s) { std::cout << "L-value ref" << std::endl; }
    S(S &&s) { std::cout << "R-value ref" << std::endl; }
};

S source() { static S s; return s; }

int main()
{
    std::cout << "here 1 \n";
    S s;
    std::cout << "here 2 \n";
    S t(s);
    std::cout << "here 3 \n";
    S u(source()); // ERROR HERE
}

For me, this prints:

here 1
here 2
L-value ref
here 3
L-value ref

It should print:

here 1
here 2
L-value ref
here 3
R-value ref

An rvalue should under no circumstance bind to a non-const lvalue reference.


-- 
           Summary: rvalue erroneously binding to non-const lvalue reference
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: eric dot niebler at gmail dot com


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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/34700] C++0x: rvalue erroneously binding to non-const lvalue reference
  2008-01-07  7:45 [Bug c++/34700] New: rvalue erroneously binding to non-const lvalue reference eric dot niebler at gmail dot com
@ 2008-01-15  7:46 ` dgregor at gcc dot gnu dot org
  0 siblings, 0 replies; 2+ messages in thread
From: dgregor at gcc dot gnu dot org @ 2008-01-15  7:46 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from dgregor at gcc dot gnu dot org  2008-01-15 05:38 -------
This is not a bug. In the line that prints "L-value ref" where the reporter
expected "R-value ref", the call to the lvalue reference constructor that we're
seeing comes from inside the body of the source() function, which is returning
a reference to a static variable. Since the variable is static, it is treated
as an lvalue, and therefore we do a construction with S's lvalue constructor
(which prints out "L-value ref"). Then, we don't actually need to perform
another construction to build "u" (since we constructed the result of the call
to source() in place), so there is no "R-value ref" at the end of the output/


-- 

dgregor at gcc dot gnu dot org changed:

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


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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-01-15  5:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-07  7:45 [Bug c++/34700] New: rvalue erroneously binding to non-const lvalue reference eric dot niebler at gmail dot com
2008-01-15  7:46 ` [Bug c++/34700] C++0x: " dgregor at gcc dot gnu dot org

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).