#I might be bad in understanding rvalue references
1 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
@dusty summit
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
this is wrong
you are returning a reference to a local object, which will be destroyed by the time the function returns
yeah, i know, i just want to try it
well then what is the question about
when an object is destroyed it doesn't magically disappear
Undefined behavior 🙂
You don't need to use an rvalue reference on a return type
T foo();
// the expression foo() is already an rvalue
it means the memory it occupies can be overwritten at any time, because it's marked as free
3 just hasn't been overwritten yet
T&& foo() { return T{}; } returns a dangling reference to the temporary you create
Also, you don't want to move out of a function like I saw in getA2()
tl;dr the fact that it "works" is luck and you shouldn't rely on it
Emphasis on the "works", it really doesn't work.
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
you can safely extend the lifetime of a temporary by binding to a const T& or T&&
but you should never return a reference to a local variable
ok