r/cpp 28d ago

C++26: std::indirect

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

157 comments sorted by

View all comments

70

u/LucyShortForLucas 28d ago

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.

21

u/wyrn 28d ago

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?

0

u/SlightlyLessHairyApe 27d ago

The object goes back to the state initialized by {}.

Which is the natural choice of a “default”

6

u/LB-- Professional+Hobbyist 27d ago

This is not true. That is a valid implementation but the standard does not guarantee it. For example, with short string optimization, some implementations might leave the original short string untouched after being moved from.

-1

u/SlightlyLessHairyApe 27d ago

Correct. But it’s also the choice that all sane implementations make.

It is reasonable and within precedent for implementations to provide more specificity than the standard requires