now I need to hand-hold my object for moves. There's nothing gained (...) to be a good mirror, it should delete the move ctor so you know you have to hand-roll your moves.
I'm not quite sure what you mean with the above but I'm pretty sure it isn't true. std::indirect works just fine with move. You don't need to hand-roll anything.
I'm sure you'd agree that it would be a disaster if we gave unique_ptr a copy ctor and, after a copy, the original unique_ptr is in a state called valueless_after_copy.
Yes, but a copy operation must create a new object that:
is (logically) disjoint with the original
must compare == with the original
Your proposal breaks 2., so it's not a valid copy.
A move, on the other hand, is expected to leave the moved-from object in a state where the only valid operations are to assign/swap into it or destroy it. These are valid operations for a moved-from std::indirect, so it works just fine with the rule of zero.
I'm not quite sure what you mean with the above but I'm pretty sure it isn't true. std::indirect works just fine with move. You don't need to hand-roll anything.
struct S1 {
T t; //T is very large, I want to move it to the heap
};
struct S2 {
T* t; //ouch, now I break rule-of-zero, I need to write
// five special member functions to regain the
// semantics of S1
};
struct S3 {
std::unique_ptr<T> t; //Well, the move ctor is easier to
// write because I can =default it,
// but I still need to write a copy
// ctor if I want the semantics of S1
};
struct S4 {
std::indirect<T> t; //Now the copy ctor is easier to write
// because I can just =default it, but
// now I need to hand roll a move ctor
// to regain the semantics of S1 AND
// the bloody class doesn't even have
// unique_ptr's decency to refuse to
// compile when I need the missing ctor,
// it auto-generates a move ctor FOR me
// that has the wrong semantics
};
struct S5 {
snns::Wrapper<T> t; // It Just Works™
};
That's what I mean.
You don't need to hand-roll anything.
I need to hand-roll a move ctor for struct S4 to get the value semantics of S1.
A move, on the other hand, is expected to leave the moved-from object in a state where the only valid operations are to assign/swap into it or destroy it.
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. And if I move out of a std::indirect, I need it to live up to P3019's abstract:
The class template indirect confers value-like semantics on a dynamically-allocated object
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.
std::indirect does not confer value-like semantics on the dynamically-allocated object. If you move from a class that holds an indirect<T>, you do not get the same value behavior as when you move from a class that holds a T as a value.
That's the problem. If an object is invalid to access after being moved from, those are pointer-like semantics, it should be called indirect_ptr, and it should not be valueless_after_move, it should hold a nullptr.
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.
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.
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.
View all comments
Show parent comment
4
u/wyrn Aug 15 '26
I'm not quite sure what you mean with the above but I'm pretty sure it isn't true.
std::indirectworks just fine with move. You don't need to hand-roll anything.Yes, but a copy operation must create a new object that:
==with the originalYour proposal breaks 2., so it's not a valid copy.
A move, on the other hand, is expected to leave the moved-from object in a state where the only valid operations are to assign/swap into it or destroy it. These are valid operations for a moved-from
std::indirect, so it works just fine with the rule of zero.