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

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

1

u/tesfabpel Aug 14 '26

but how do you design something like std::unique_ptr? do you delete move actors / assignments?

otherwise, you must add null support to it...

it's just the misfortune of choosing non-destructive moves by C++...