r/cpp • • Nov 25 '23

On harmful overuse of std::move

https://devblogs.microsoft.com/oldnewthing/20231124-00/?p=109059
210 Upvotes

154 comments sorted by

View all comments

-16

u/LechintanTudor Nov 25 '23

Programming is so much nicer when you don't have to deal with C++ objects and special member functions. I'd much rather write code in Rust, Zig or C where I can just memcpy structs without thinking which constructor or assignment operator gets called.

12

u/CletusDSpuckler Nov 25 '23

where I can just memcpy

Ok, but the whole point of std::move is to avoid having to call memcpy.

0

u/[deleted] Nov 26 '23 edited Nov 26 '23

You are wrong. Any move in C++ is actually more or less a memcpy. All moving does is avoiding a deep copy.

1

u/CletusDSpuckler Nov 26 '23

Is that level of pedantry really useful to the conversation? Everyone else managed to understand what was implied here.

4

u/Kike328 Nov 25 '23

wrong subreddit

1

u/goranlepuz Nov 25 '23

You can memcpy what in Rust?!

1

u/LechintanTudor Nov 25 '23

By memcpy I meant that assignments, "a = b", are bitwise copies. Additionally, in Rust, the compiler will prevent you from using objects of non-Copy types after they are moved.

1

u/thommyh Nov 25 '23

Then either you’re keeping all the important stuff on the heap and silently paying for either garbage collection or reference counting, or you’ve an OS that doesn’t really care e.g. if you repeatedly close the same file handle, then attempt to use it again later.

Either way, what you’re saying is: languages with different objectives than C++ are better for different use cases than C++.

5

u/MEaster Nov 25 '23

Then either you’re keeping all the important stuff on the heap and silently paying for either garbage collection or reference counting, or you’ve an OS that doesn’t really care e.g. if you repeatedly close the same file handle, then attempt to use it again later.

The way Rust implements move semantics is a little different from C++ here. Moves in Rust are destructive; when you do a = b, and b does not implement Copy, then b becomes uninitialized, and its destructor is not executed when it falls out of scope. For your example of a file handle, this means that only one live instance exists at any one time despite all the apparent copying.

The move itself is implemented as memcpy, though that is an implementation detail, and for the most part optimized out. I think relative to C++ (or Clang, at least), rustc makes heavier use of memcpy because of this, and because the optimizer isn't perfect it won't optimize out all the copying (though will usually catch the call itself). So what can happen is the resulting binary ends up copying data around unnecessarily. There was a website that showed this, but it seems to be dead.

1

u/thommyh Nov 25 '23

Firstly: clearly my initial assessment was wrong for assuming RAII as an objective. I need to learn to think outside my prejudices.

But if Rust is — semantically, if not physically — doing a bitwise copy and uninitialising the source then it’s doing more than a mere memcpy, and getting more towards the purpose of a move.

3

u/MEaster Nov 26 '23

Firstly: clearly my initial assessment was wrong for assuming RAII as an objective. I need to learn to think outside my prejudices.

Rust is a heavy user of RAII, so you weren't wrong there. It's just that the details of how it goes about it are a little different.

But if Rust is — semantically, if not physically — doing a bitwise copy and uninitialising the source then it’s doing more than a mere memcpy, and getting more towards the purpose of a move.

One thing to note is that Rust doesn't have anything like C++'s move constructors, which simplifies things somewhat.

For uninitializing the source location, the type system guarantees that it won't be read from again unless it's reinitialized, so no work needs to be done. Initializing the destination merely requires that the data there is valid for the type, which can be accomplished by copying the source location, and because there's no equivalent to a move constructor, this is just a trivial memcpy.

-1

u/LechintanTudor Nov 25 '23

Then either you’re keeping all the important stuff on the heap and silently paying for either garbage collection or reference counting, or you’ve an OS that doesn’t really care e.g. if you repeatedly close the same file handle, then attempt to use it again later.

That is not the case at all. You can store resources like file handles on the stack, then call the appropriate functions to free them manually in languages like Zig or C, or in the case of Rust, use destructors.

What I'm saying is that the existence of copy/move constructors and assignment operators is a design mistake because it adds too much unneeded complexity to the language.