|
|
|
|
|
by yolina
78 days ago
|
|
destructive moves are required to make moves zero cost. Currently move semantics in C++ requires that A is left in a 'moved from, but valid state' which means that: 1. The compiler must still generate code that calls the destructor. 2. Every destructor need have to have some flag and a test in it like:
if(moved_from) // do nothing
else { free_resources(); } (Granted, for some simple types the compiler might inline and removed redundant checks so it ends up with no extra code, but that is not guaranteed) With destructive moves the compiler can just forget about the object completely, no need to call it, destructurs can be written as normal and only care about the invariants established in the constructor. |
|
A = 0; B = 0; A = move(B); ++B; ++A;
What should happen at ++B, and what should A be at the end? How would the compiler enforce this? I can see this being complicated.
The compiler can forget about it, but the code doesn't, so you mismatch between what is on screen and what the compiler does, which seems even more confusing.