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

Show parent comment

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.