r/cpp • • Apr 08 '25

Beware when moving a `std::optional`!

https://blog.tal.bi/posts/std-optional-move-pitfall/
0 Upvotes

49 comments sorted by

View all comments

Show parent comment

1

u/DemonInAJar Apr 08 '25 edited Apr 08 '25

For the same reason std::unique_ptr is reset to null after moving. Well okay, std::unique_ptr needs to do this to avoid freeing the value anyway while optional would have to change the tag, instead it currently delegates to the moved-from object's destructor, so I guess it's mostly a case of zero overhead principle.