4 ms·
So, what do you propose std::move<int> should do? Initializing the ‘source’ int to zero seems the only reasonable option, but it would hurt performance. Also,
by Someone 7y ago
So, what do you propose std::move<int> should do?
Initializing the ‘source’ int to zero seems the only reasonable option, but it would hurt performance.
Also, what about std::move<T> in general? If T has a destructor, should that call it on the original? Currently, deciding whether that (or doing its equivalent) is a good idea is up to the implementer of std::move<T>. I don’t see how the compiler could make that decision without sacrificing performance in some cases.
I think that, if you want the compiler to enforce destruction of moved-from values, you also should want the compiler to zero out (or equivalent) all destructed values.
- dragontamer 7y agostd::move has been part of the language for 9 years. It is no longer reasonable to change std::move. To satisfy the programmer, there probably should be a std::dmove(x), which would be a compiler-enforced notation that would make use of x illegal after the use of std::dmove. std::dmove(x) would be implemented as std::move, followed by a destructor, and then the compiler removes variable x from the scope. Pointers and references to that variable will have undefined behavior if they are used (compiler may find it reasonable to recycle x's memory location to another variable) ------ std::move<int> remains the same. There's no performance benefit, or even code-logic benefit, to zeroing out integers. For example, an integer may be a denominator, dividand, modulus, or multiplier. In these cases, "zeroing out" to "x = 1" makes more sense than "x=0". Having a compiler enforced "zero" of "x=0" is arbitrary and doesn't help anybody. Its better to let the integers remain the same value they used to be.
- Someone 7y agoBut the whole point of std::move is to avoid calling, possibly expensive, destructors. The standard example is template<typename T> void swap(T& x, T& y) { T z = std::move(x); x = std::move(y); y = std::move(z); } ... std::string a[1000]; ... std::swap(&a[2], &a[7]) If std::move copy-constructs and destroys, that last line would do three allocations and three frees (recoverable by a significantly advanced compiler, but who has that?). The way std::move is specced, it can do zero allocations and zero frees.
- dragontamer 7y agoA good point. So clearly the way std::move works now is great for that situation. But there's a 2nd situation, with Herb Sutter's IndirectInt, which calls for a different behavior.
- unlinked_dll 7y ago>So, what do you propose std::move<int> should do? Just copy Rust. Trivially copyable types aren't invalidated by moves, and "move" just aliases copy.
- jimbo1qaz 7y agoIs the intention of std::move<unique_ptr> that the "moved-from" pointer no longer has its destructor run? What if you move a unique_ptr from a std::vector? You don't know which elements of the vector need to have their destructors run. I think Rust unconditionally doesn't run the destructor of a moved-from Box, but uses drop flags for "maybe-moved-from" local variables, and doesn't allow maybe-moved-from Vec elements.
- unlinked_dll 7y agoMy point was on handling the moving of trivially copyable types, like int, not those with side effects like unique_ptr. For trivially copyable types, copy and move are (almost) synonymous, and can be made such in Rust with #[derive(Copy)]. But that difference actually gets to your point here. The only difference between copy and move is that a move allows for the new data to overlap the previous. So if you think about what moving from a vector of unique ptr actually means, it must invalidate the vector or remove the element that was moved from the vector. If you want to get the element from the vector without those effects, you have to create a deep copy. Basically moving without invalidating or mutating such that the moved-from alias can no longer be accessed is necessary.
- cormacrelf 7y agoYou’ve referred to two kinds of move there — memmove (overlapping) vs memcpy (non-overlapping memory) and transfers of ownership. In C++ a move appears to always require moving memory with either (depending on aliasing) or via registers. Is that correct? In Rust, the only thing that has to happen is a transfer of ownership, which does not always mean moving bytes. This greatly simplifies the whole “rvalue reference parameter” thing, because ownership abstracts over wherever you might want to move to. So if your new owner wants it in part of its new stack frame, the compiler is free to just put it there before calling or simply call a destructor on the parent stack frame’s memory in the child. (I don’t know exactly what it likes to do, but it’s all fine.) New owners like Box need to move memory, and that’s cool too, and the compiler is free to reorder the steps to avoid initialising and moving when “placement new” is better. This is also something I believe the compiler is getting better at slowly. The compiler won’t let you safely write moves out of a vector without mutating or taking ownership of the vector, each with moving bytes — otherwise it has access to something it no longer owns. The easiest example is `let a = A::new(); let b = a;`, which only needs stack space for one A, and for which line 2 is a noop. Its only effect is in when drop is called, if b is in a smaller scope. Does C++ do this too? I would hope so, but it looks like no. Edit: turns out neither do it. - https://rust.godbolt.org/z/xjA9v4 https://rust.godbolt.org/z/xjA9v4 - https://godbolt.org/z/7Wh_AZ https://godbolt.org/z/7Wh_AZ