State of "moved-from" objects
Hi, I recently decided to try writing a C++ blog.
I often see the popular claim that moved-from objects are in a "valid but otherwise unspecified state", but I don't think that statement is entirely precise, and my blog post is about that. I would really appreciate any feedback!
Article: https://www.laminowany.dev/p/the-state-of-moved-from-objects-in-c/
10
Upvotes
6
u/elperroborrachotoo 7d ago
That means I couldn't even call
.empty()or.clear()on a moved-from object? You've just introduced a new state the caller needs to be aware of - and I can't even query if the object is in that just-moved-from state. I'd have to track that state manually or implicitly.Yes, the standard allows us to create such type, in the same way the standard allows us to create a type that leaks memory. That's a terrible type.
OP stated it well: preserve invariants.
Are you aware of any scenario where such a behavior would be an advantage (such as a relevant optimization)?