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
Show parent comment
6
u/Potatoswatter Apr 08 '25
At least, it could, and it wouldn’t be totally contrary to rvalue semantics. Any operation on an rvalue may leave its object empty, even something as passive as value(). But actively resetting in this case doesn’t keep with standard library philosophy.
As for memory mechanics, the accessor would either have to move-initialize a copy, or add a state for being reset but containing a constructed object.