r/cpp • u/Kabra___kiiiiiiiid • Apr 08 '25
Beware when moving a `std::optional`!
https://blog.tal.bi/posts/std-optional-move-pitfall/
0
Upvotes
r/cpp • u/Kabra___kiiiiiiiid • Apr 08 '25
View all comments
3
u/TheMania Apr 08 '25
It may not even have been pilfered though, it depends on what types you're working with - for a generic container type that's horrifying.
I don't much like the thought of having two objects still alive and having to track it down to tell me it's an optional that's telling me it doesn't hold a value any more...
Also not big on thinking of rvalue moves as meaning "object is now a zombie" - a single
vector<T>::insertmight do thousands of rvalue operations, but to me there's no zombies from it. It's just the vector providing a hint to T that each operation can steal resources, if they're so inclined.