Bug 34700 - C++0x: rvalue erroneously binding to non-const lvalue reference
Summary: C++0x: rvalue erroneously binding to non-const lvalue reference
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-01-07 05:49 UTC by Eric Niebler
Modified: 2008-01-15 05:38 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Niebler 2008-01-07 05:49:35 UTC
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.
Comment 1 dgregor 2008-01-15 05:38:53 UTC
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/