▲ | deagle50 3 days ago | ||||||||||||||||||||||
I got that, but the I don't see what the example of move semantics has to do with RAII or defer. | |||||||||||||||||||||||
▲ | kbr- 3 days ago | parent [-] | ||||||||||||||||||||||
It makes things easier. Usually the move constructor (or move assignment operator) will cause the moved-from object to stop being responsible for releasing a resource, moving the responsibility to the moved-to object. Simplest example: move- construct unique-ptr X from unique-ptr Y. When X is destroyed it will free the memory, when Y is destroyed it will do nothing. So you can allocate resource in one function, then move the object across function boundaries, module boundaries, into another object etc. and in the end the resource will be released exactly once when the final object is destroyed. No need to remember in each of these places along the path to release the resource explicitly if there's an error (through defer or otherwise). | |||||||||||||||||||||||
|