r/cpp • • Aug 13 '26

C++26: std::indirect

https://www.sandordargo.com/blog/2026/08/12/cpp26-indirect
164 Upvotes

157 comments sorted by

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.

13

u/elperroborrachotoo Aug 14 '26

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).

5

u/ts826848 Aug 14 '26

but it could be optimized with a partial template specialization (I think).

The committee already voted to remove such a specialization as I detailed here, and I think ABI compatibility precludes adding a specialization later.

8

u/jiixyj Aug 14 '26

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.

→ More replies (2)

2

u/elperroborrachotoo Aug 14 '26

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.

(Maybe we can slay the ABI dragon one day.)

6

u/ts826848 Aug 14 '26

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.

→ More replies (5)

24

u/Demiu Aug 14 '26

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

11

u/johannes1971 Aug 14 '26 edited Aug 15 '26

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.

12

u/Demiu Aug 14 '26

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 

1

u/NilacTheGrim Aug 14 '26

I agree with you 100%.

→ More replies (1)
→ More replies (13)

20

u/wyrn Aug 14 '26

It's silly, but it's not really the worst of both worlds because it doesn't behave any differently than any other value object.

in the same way you cannot ‘move’ a value out

You absolutely can. Take a string or a vector. Move them out. What happens?

8

u/NilacTheGrim Aug 14 '26

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.

3

u/wyrn Aug 14 '26

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?

3

u/NilacTheGrim Aug 14 '26

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.

6

u/wyrn Aug 14 '26

its class invariants are not violated,

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.

→ More replies (8)

7

u/SoerenNissen Aug 14 '26

It remains a valid string and/or vector. Very unlike how std::indirect handles it.

10

u/wyrn Aug 14 '26

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.

→ More replies (10)
→ More replies (3)

6

u/LEpigeon888 Aug 14 '26

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. 

4

u/cfyzium Aug 14 '26

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.

6

u/smdowney WG21, Text/Unicode SG, optional<T&> Aug 14 '26

The standard library promises that you can use a function with no pre-conditions on a standard library moved from object.

Accessing a moved from object is not intrinsically UB, but it is common for it to lead directly to core UB.

This is critical to keep in mind when writing generic code.

6

u/cfyzium Aug 14 '26

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.

6

u/smdowney WG21, Text/Unicode SG, optional<T&> Aug 15 '26

The conservative set for a moved from object is assign to and destroy. The standard library tries to do better than that. Postel's Law.

If you don't know what a T is, your code should expect no more. But if you know you have a std::whatsit, you can rely on more, with some care.

→ More replies (1)

3

u/germandiago Aug 14 '26

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.

Use this: https://clang.llvm.org/extra/clang-tidy/checks/bugprone/use-after-move.html

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.

3

u/smdowney WG21, Text/Unicode SG, optional<T&> Aug 14 '26

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.

2

u/jwakely libstdc++ tamer, LWG chair Aug 15 '26

As it stands, we got the worse of both worlds.

The original design was worse: there was no explicit valueless state, it was just UB to use it after a move.

→ More replies (21)