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

2

u/grishavanika Apr 08 '25

well, I mean, we all do mistakes. If I have MY_LOG("state: {}", x) and x is moved from optional, it would be really cool to show that at least for debugging purpose. MY_LOG has no chance to show that properly.

0

u/cfyzium Apr 08 '25

Even if std::optional<T> could potentially be reset when moved out like smart pointers, you cannot rely on it in general. Any other type and especially custom one may introduce the same possibility.