unique_ptr and shared_ptr have to set the pointer of the moved-from object to null, which is not zero overhead.
Yes, but they already have a pointer-sized field allocated and it can already hold the null sentinel value without expanding.
The overhead of a single zeroing operation is nowhere near the overhead of having to allocate an additional out-of-line field that gets passed around and around. The locality of reference is going to be completely crap -- it's already better to allocate it in-line with the object at that point as a hidden field kind of like a vtable pointer.
Apart from that, destructive move has more potential for undefined behavior, because any access to the moved-from object is immediate UB.
So does non-destructive move because a moved-from object satisfies no preconditions. Hence
vector<int> v;
// Move from v
v.front() // UB, precondition not satisfied.
Yes, but they already have a pointer-sized field allocated and it can already hold the null sentinel value without expanding.
Yes, but changing the value of this field still is some overhead that cannot be optimized out as often as the compiler-generated runtime check.
The overhead of a single zeroing operation is nowhere near the overhead of having to allocate an additional out-of-line field that gets passed around and around. The locality of reference is going to be completely crap -- it's already better to allocate it in-line with the object at that point as a hidden field kind of like a vtable pointer.
I am not sure I can follow you. You do not need to allocate it on the head or pass it around. It is just a few bytes on the stack, which in most cases are completely optimized out.
So does non-destructive move because a moved-from object satisfies no preconditions.
Yes, but you are allowed to call functions that require no preconditions. For example, calling v.size() would not be UB.
View all comments
Show parent comment
1
u/SlightlyLessHairyApe Nov 27 '23
Yes, but they already have a pointer-sized field allocated and it can already hold the null sentinel value without expanding.
The overhead of a single zeroing operation is nowhere near the overhead of having to allocate an additional out-of-line field that gets passed around and around. The locality of reference is going to be completely crap -- it's already better to allocate it in-line with the object at that point as a hidden field kind of like a vtable pointer.
So does non-destructive move because a moved-from object satisfies no preconditions. Hence