I tried to understand lvalue and rvalue in C++11. So I wrote a test code:
int x = 10;
int foo() { return x; }
int& bar() { return x; }
int&& baz() { return 10; }
int main() {
int& lr1 = 10; // error: lvalue references rvalue
int& lr2 = x; // ok
int& lr3 = foo(); // error: lvalue references rvalue
int& lr4 = bar(); // ok
int& lr5 = baz(); // error: lvalue references rvalue
int&& rr1 = 10; // ok
int&& rr2 = x; // error: rvalue references lvalue
int&& rr3 = foo(); // ok
int&& rr4 = bar(); // error: rvalue references lvalue
int&& rr5 = baz(); // ok
}
It works pretty well, so I inserted std::cout to print results.
#include <iostream>
int x= 10;
int foo() { return x; }
int& bar() { return x; }
int&& baz() { return 10; }
int main() {
int& lr1 = 10; std::cout << lr1 << std::endl; // error
int& lr2 = x; std::cout << lr2 << std::endl; // ok
int& lr3 = foo(); std::cout << lr3 << std::endl; // error
int& lr4 = bar(); std::cout << lr4 << std::endl; // ok
int& lr5 = baz(); std::cout << lr5 << std::endl; // error
int&& rr1 = 10; std::cout << rr1 << std::endl; // ok
int&& rr2 = x; std::cout << rr2 << std::endl; // error
int&& rr3 = foo(); std::cout << rr3 << std::endl; // ok
int&& rr4 = bar(); std::cout << rr4 << std::endl; // error
int&& rr5 = baz(); std::cout << rr5 << std::endl; // ERROR!?
}
int&& rr5 = baz(); std::cout << rr5;
causes a Runtime Error, but I don't know why it makes an error.
I think the return value of baz()
would be xvalue, so its lifetime is prolonged. But when I tried to access its value, the error occurs. Why?
bar
andbaz
both invoke undefined behaviour, one by returning a reference to a local object and the other by failing to return anything. – Johnajohnathx
is not local. – Sheerbar
returns a reference to a static at file scope. – Sunflowerconst
references. No magic happens for mutable references. – Sirloin