r/cpp • u/Little-Reflection986 • Feb 16 '26
Favorite optimizations ??
I'd love to hear stories about people's best feats of optimization, or something small you are able to use often!
131
Upvotes
View all comments
Show parent comment
1
u/max123246 Feb 16 '26
Return a std::optional<T*> for the out parameter.
std::move isn't guaranteed and is only a compiler hint. Even worse, std::move requires the old object to be left in a "valid but unspecified state".
This requires weakening invariants of your custom objects in order for moves to be performant. Every object must now have an empty state, or else std::move is effectively a copy. So the idea of having a Non-empty array as a type is not possible without complicating the internals to represent some empty state only for std::move.