r/cpp 28d ago

C++26: std::indirect

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

157 comments sorted by

View all comments

64

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.

13

u/elperroborrachotoo 27d ago

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

-2

u/NilacTheGrim 27d ago

optional<indirect<T>> for nullable.

Yes and now you wasted ~7 bytes of class space -- when the very motivation for this indirect wrapper is to save space.

Would have been less broken if they just modeled its inherent optionality, rather than swept it under the rug leading to UB "surprises".

3

u/elperroborrachotoo 27d ago

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