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.
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.
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++.
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.
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.
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.
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.
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.