Oh, I found one problem in my rvalue-references comprehension. The problem:
int&& foo()
{
int n = 5;
return std::move(n);
}
int bar()
{
int y = 10;
return y;
}
int main()
{
int&& p = foo();
bar();
std::cout << p;
}
The compiler doesn't write error or warning that we return local address from function foo. And I'm going to replace value 5 by 10 in function bar. But result is 5. If I change std::move to static_cast compiler gives error and result is 10. Why is it going so? I use gcc 4.8.1.
Returning a reference to a local variable is undefined behaviour. Anything could happen. Don't do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With