r/cpp • • Aug 13 '26

C++26: std::indirect

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

157 comments sorted by

View all comments

Show parent comment

2

u/SoerenNissen Aug 16 '26 edited Aug 16 '26

No, that is incorrect. You don't need to hand-roll a move constructor. std::indirect already respects value semantics.

The class used to have value-semantics. For example, I could ask for t.size() at all times, because t was a value and would still be there after a move.

With snns::Wrapper, which hoists the underlying T to the heap, I can ask for t->size() at all times, because Wrapper is written to preserver those value semantics. The only pointer-like semantic is the fact that the T is on the heap, but that's what I wanted so it's fine.

With std::indirect, I can no longer ask for t->size() at all times, because indirect has an additional pointer-like semantic: A state where it is invalid to dereference. The semantics of the containing class have changed. The pure value-semantics of the containing class have changed. Because indirect failed to preserve those value semantics.

but upon examination it became clear that a copyable wrapper needs to have value-like semantics, not pointer semantics,

I know it should.

That's why it's a problem that it can be in a state where it is invalid to access, like a pointer. It would be much better if it had value semantics.

4

u/wyrn Aug 16 '26

The class used to have value-semantics.

Like I said before, that's not what value semantics means.

For example, I could ask for t.size() at all times, because t was a value and would still be there after a move.

... and the result you'd get after a move would be meaningless and you'd be able to do nothing with it.

because indirect has an additional pointer-like semantic:

No, it is not pointer-like semantics. It is value semantics. What you're complaining about is that you can't use it after a move. But you can't use anything after a move. That has nothing to do with pointers.

It would be much better if it had value semantics.

It does. You're just confused about how values in general behave.

Like I said. Watch Sean's explanation.