Why is lvalue for rvalue link binding allowed in g ++ 4.4.6?

I am learning the rvalue function in C ++ 11. C ++ Primer 5th edition says that the rvalue link can only be attached to the rvalue, but when I tried to compile this program, it passed and the output was 1 1 .

I do not understand why. I am using g ++ 4.4.6 and compiled it with

g ++ -Wall -std = C ++ 0x test.cpp -o test

 #include <iostream> using namespace std; int main() { int i = 0; int &&rr = i; rr = 1; std::cout << rr << std::endl; std::cout << i << std::endl; return 0; } 
+7
c ++ c ++ 11 rvalue
source share
2 answers

Your code is invalid, lvalue cannot bind to an rvalue reference, as the book says. g ++ 5.1 rejects your code with error message

  main.cpp:8:16: error: cannot bind 'int' lvalue to 'int&&' int &&rr = i; ^ 

g ++ 4.4.6 was released in April 2011, less than a month after the final draft for C ++ 11 was approved . The behavior you see is either a gcc bug or a behavior defined by some early C ++ 11 standard working draft.

The current rule is described in N2844 and implemented in gcc 4.5 .

+6
source share

The GCC 4.4 series was first released in April 2009 , and the barrel was closed to all but the regression corrections and documentation long before that, in November 2008.

A rule that prevents references to rvalues ​​from being bound to lvalues ​​was voted in the standard in March 2009 as document N2844 , after being first proposed in December 2008, in document N2812 .

Thus, the new rule cannot be implemented in time for GCC 4.4 (this was actually implemented for GCC 4.5).

+6
source share

All Articles