Comment

Comment by yolina

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.

parent yolina 7 months ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton