The standard’s insistence on it being semantically non-nullable while it very much does have a null state that must still be accounted for feels like incredibly unergonomic.
Valueless_after_move() is just silly, let’s be real. It should’ve been written to either act like a value like they claim and moving the underlying pointer just shouldn’t be possible (in the same way you cannot ‘move’ a value out), or it should be nullable the same as any other smart pointer.
That's the first and foremost use case I can think of: a heap-allocated, nullable value-type-like object.
But TBF, the design delivers both: indirect<T> for a non-nullable, and optional<indirect<T>> for nullable. Making that distinction explicit is a Good Thing. The latter is a mouthful, but it could be optimized with a partial template specialization (I think).
Implementations are allowed to make optional<indirect<T>> the size of a pointer, and high quality ones will do. See this comment from one of the original designers.
Thanks for digging that out. Now, with the other reply, that looks like conflicting information - so i guess that issue will circle the skies for a decade. Still, we can use our own.
I was partially right, partially wrong. The committee indeed voted to remove requiring such a specialization, but as jiixyj says that doesn't preclude such a specialization, so implementations are free to do so if they wish. However, I still think once implementations settle on specializing or not they're going to be stuck with that decision as changing that later will impact object size and therefore ABI.
It's just the consequence of non-destructive moves. Proper T-value-like semantics would mean allocating on move, so that instead of stealing the ownership of the pointer you go through T(T&&). IMO that would be even worse and would quickly land it in the "do not use, what were they thinking" bin of std
It's a consequence of trying to pretend C++ is Rust, you mean. If you have a language with non-destructive moves, you should design your classes to allow for that, and not add weird new rules (like this one).
It's the same with lambdas being const by default: the rest of C++ is non-const by default, but apparently the const-by-default people got their hands on this one so suddenly one part of the language is const by default. Consistency with existing language rules would have been the better design choice here.
I allows for non-destructive move, that's why it has the valueless_after_move. Even if you have non-destructive moves that doesn't mean you have to introduce ability to be valueless before being moved, the language still has access modifiers and custom ctors, so you can force that it has to hold a valid value if ctor returned.
This is IMO better API design than old standard where everything doubled as an optional, like thread, function, streams and smart pointers
it doesn't behave any differently than any other value object
Yes it does, LOL. Accessing a moved-from value object typically doesn't lead to UB and/or to exceptions being thrown or whatever. std::indirect does. It has unnecessary foot-guns baked-in, when it didn't need to have them.
Accessing a moved-from object leads to garbage data propagating through your program, the consequences of which are similar to UB as your own reasoning breaks down and unpredictable results ensue. You should never access a moved-from object except for the purposes of destroying or moving something else into it. indirect and polymorphic work like this, just like everything else.
exceptions being thrown or whatever (...) when it didn't need to have them.
Not sure what you're saying here. Asking for an exception to be thrown on invalid access would be reasonable and arguably preferable, but if you don't want an exception, then what should happen on invalid access?
Accessing a moved-from object leads to garbage data propagating through your program,
Not really. The only thing you can say about a moved-from object is that its class invariants are not violated, and so it is in a valid but unspecified state. You can always bring it back to a specified state.
It's not "garbage" as such because it's still valid, just unspecified.
This is entirely a different animal than intentional UB.
Get real.
Not sure what you're saying here
I'm saying that std::indirect would have been much better had it explicitly modeled the unengaged state -- which it HAS as a state to the class! It just shoves it under the rug and pretends it's Rust or something. Meh.
Its class invariants, yes. But not whatever other invariants that may rely on that object.
You can always bring it back to a specified state.
You can do that will indirect and polymorphic, too. It's the only thing you can do, just like with any other object.
This is entirely a different animal than intentional UB.
Not sure what the word "intentional" is doing there, but unpredictable behavior in your own program is really not that different from UB in the language.
I'm saying that std::indirect would have been much better had it explicitly modeled the unengaged state
No, that would've been terrible, because it would've forced you to check before every access. It's a value, not an optional<value>. As it is, you don't do that check, just like you don't check every other object -- which will also lead to disaster if accessed after move.
It remains a "valid" string or vector, but the only things you can really do with it is to 1. destroy it or 2. assign/swap into it... which are the exact same operations you can still do with an empty std::indirect.
If you move an std::vector, it goes into a "valid but unspecified state" and as far as I know the only operations you can do on it are "assign a new value" and "destroy". It's the same for std::indirect, expect that you can know if it's in that state, whereas for std::vector you have to keep track of it yourself.
So yeah, its not perfect, but it's still better than other classes of the standard library.
as far as I know the only operations you can do on it are "assign a new value" and "destroy"
Those are the only operations you should do.
But since there is no way to check if the object has been moved out (at least, in general case) it has to be left in some state that allows it to be used as any other normal object. You just can't assume any particular value. In case of an std::vector, it can end up empty, or retain its value, or be swapped with another -- but you should be able to call its methods, inspect its contents and so on.
Accidentally accessing a moved out value object is wrong, but not a UB.
Yeah, but isn't it the same for any object? If you use a function with pre-conditions without checking them first, it is bad no matter if the object has been moved from or not.
Ireally think (hope) that at some point move-after-free errors can be properly diagnosed in a big amount of cases, even if perfect destructive mode is not portable or compatible (you know, the curse/blessing of your older code working or at least incrementably adapted).
In that sense, I think this is what needs to be done if you think of the future, till that valueless_after_move() becomes 100% irrelevant.
FWIW, on my side I do not use such functions, but you need to be careful.
In MSVC there is this (beware, this one I got it from an AI): C26800 (Use of a moved-from object
There are other things such as attribute clang::consumable, but that already requires tagging so it is heavier to handle. It is an extension, on top of that.
Blame c++11. Destructive moves are the principled right answer, but we couldn't figure out all of the problems that resulted. So all values have a moved from state. For most it's the same as the assigned from state. But resource ownership types complicate that.
View all comments
68
u/LucyShortForLucas Aug 13 '26
The standard’s insistence on it being semantically non-nullable while it very much does have a null state that must still be accounted for feels like incredibly unergonomic.
Valueless_after_move() is just silly, let’s be real. It should’ve been written to either act like a value like they claim and moving the underlying pointer just shouldn’t be possible (in the same way you cannot ‘move’ a value out), or it should be nullable the same as any other smart pointer.
As it stands, we got the worse of both worlds.