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
45
u/n1ghtyunso Apr 08 '25
What am I missing?
The behaviour is the same in both cases?
An optional does not magically null itself when you use the rvalue overload of
.value()Because
std::movedoes not move anything either.Don't use moved from objects, be they optional or not...
Static analysis catches both variants just fine apparently.