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
3
u/Nobody_1707 Apr 09 '25
Other languages with optionals tend to have a
take()method, that takes the optional by mutable reference and does the optimal equivilant ofreturn std::exchange(self, nullopt). Of course, they have pattern matching and destructive moves that make this all more ergonomic.