r/cpp • • Aug 13 '26

C++26: std::indirect

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

157 comments sorted by

View all comments

Show parent comment

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.