r/cpp • • Aug 13 '26

C++26: std::indirect

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

157 comments sorted by

View all comments

Show parent comment

4

u/wyrn Aug 15 '26 edited Aug 15 '26

I need to hand-roll a move ctor for struct S4 to get the value semantics of S1.

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

The general expectation is for types that don't have specific different expectations. If I move out of a std::array<int>, I expect every individual int inside that array to remain in exactly the same location.

That's not guaranteed -- all the standard says is moved-from standard library types will be in a valid but unspecified state --, but even if it was, you're cruising for a bruising if you start relying on case-by-case state of moved-from objects. Why would you use-after-move on purpose?

snns::Wrapper confers value-like semantics on the dynamically-allocated object. If you move from a class that holds a Wrapper<T>, you get the same value behavior as when you move from a class that holds a T as a value.

And, as a result, your move constructor must throw.

those are pointer-like semantics,

No, that is incorrect. I feel you misunderstand what the terms "value semantics" and "pointer semantics" mean.

, it should be called indirect_ptr

No, it very much shouldn't. In fact it was initially called clone_ptr, but upon examination it became clear that a copyable wrapper needs to have value-like semantics, not pointer semantics, because to do otherwise would break the fundamental properties of a copy that I mentioned in my previous post. You'd copy the pointer, but the resulting object compares != to the original (because it points to a different memory region). This is a broken copy operation and is completely unacceptable.

See Sean Parent's explanation: https://youtu.be/cK_kftBNgBc?t=2445

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.