#I might be bad in understanding rvalue references

1 messages · Page 1 of 1 (latest)

dusty summit
#

why it have 3 at deleted memory?

west thistleBOT
#

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

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

ebon pollen
#

this is wrong

#

you are returning a reference to a local object, which will be destroyed by the time the function returns

dusty summit
#

yeah, i know, i just want to try it

ebon pollen
#

well then what is the question about

dusty summit
#

why it works? why 3 is exist

#

when i call move constructor

ebon pollen
#

when an object is destroyed it doesn't magically disappear

hollow hound
#

Undefined behavior 🙂

native quarry
#

You don't need to use an rvalue reference on a return type

T foo();
// the expression foo() is already an rvalue
ebon pollen
#

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

native quarry
#

T&& foo() { return T{}; } returns a dangling reference to the temporary you create

hollow hound
#

Also, you don't want to move out of a function like I saw in getA2()

ebon pollen
#

tl;dr the fact that it "works" is luck and you shouldn't rely on it

native quarry
#

Emphasis on the "works", it really doesn't work.

dusty summit
#

alright, thx)

#

i was just intrested in it

#

!solved

west thistleBOT
#

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

ebon pollen
#

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

dusty summit
#

ok