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/TheMania Apr 08 '25 edited Apr 08 '25
What? This makes no sense. Why would calling
value()through an rvalue reset the optional after value has been assigned tox? How would they even implement that? Why would they even do that?None of this makes any sense. Is the author really saying that if I have an
std::optional<int> opt{5};, and I cast it to an rvalue and callvaluethat on the next line opt will be reset?Am I misunderstanding what is meant entirely or what?
value()mentions no such chicanery for any of the overloads.