r/cpp • u/stanimirov • Jul 04 '18
Be Mindful with Compiler-Generated Move Constructors
https://ibob.github.io/blog/2018/07/03/compiler-generated-move/
87
Upvotes
r/cpp • u/stanimirov • Jul 04 '18
View all comments
Show parent comment
8
u/tasminima Jul 04 '18
IMO the move semantics of Rust are way better though on the following crucial point; you can't use a moved-from object. This is what you want 99.99(9999?)% of the time.
Hey, in C++ I've almost been bitten by mindlessly throwing an std::forward in a template to attempt to get perfect forwarding. Who would not do that? Except the std::forward was in a loop. I let you imagine the wreck this could have caused, in some cases, depending on obscure factors. Without even a compiler diagnostic. Oh actually you can't precisely imagine the wreck this could have caused, because you don't know on which kind of object this would have applied, and some standard one have well specified moved-from states, while other standard ones have mostly only valid for destruction but otherwise unspecified states. Well, in this case, why I am even allowed to reuse them? That makes no sense. If the only valid uses are either replaceable by other constructs, or the destruction of the object, or representing an empty state, just destroy it automatically already!
I understand the free perf benefits without any application level source code change the introduction of move constructor and rvalue references gave us. But that came with great associated risks, given how the details were chosen, when all the tricky mechanisms actually began to be used directly in application code...
And we often see that pattern: C++ features are meant to be used by experts, with nasal daemons and other goodies as a punishment is they are "misused" (and with the compiler authors being soo imaginative, the definition of misused is vast, and potentially becoming vaster every day). They end-up being used by everybody, including myself. And we write (nasty) bugs. And even experts sometimes write nasty UB triggering bugs. Or even do not know anymore if some whole class of devices are possible at all to be written in portable C++ anymore (a simple memory allocator, anybody? good luck with that...)