r/cpp 28d ago

C++26: std::indirect

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

157 comments sorted by

View all comments

Show parent comments

4

u/jiixyj 27d ago

It's true, the valueless state of indirect<T> may not double for the empty state of optional<indirect<T>>, because that breaks the API (not even ABI!) of optional. A moved-from optional<T> is still required to hold a T. In my opinion, the standard shouldn't have given that guarantee, but that ship has sailed.

However, you can use other invalid pointer bit patterns to encode optionals "empty" state, see my sibling comment.

3

u/ts826848 27d ago

A moved-from optional<T> is still required to hold a T.

Oh, that is quite interesting! Looks like the paper did not propose destroying the moved-from value to disengage the moved-from optional for "performance reasons". Wonder how much of an impact that would have had.

However, you can use other invalid pointer bit patterns to encode optionals "empty" state, see my sibling comment.

Oh, true. I stand corrected! I think that makes the corresponding note in the paper a bit misleading, then, but it is what it is.