I might be confusing here but what I did understand is that :
- COW will return a "fake" copy until one of the callers want to make some modifications.
- Move Semantic will return a "fake" copy in all cases.
So if my purpose is to have an object that only need to be read and not updated, I should only wait for move semantics support in my C++ compilers ?
Am I right ?
Move semantics does not return a “fake” copy. Instead, in a copy operation, it gives the copyee permission to trash the original copy.
A motivating example may be instructive:
Without move semantics, you’d need to copy
bigDatato insert it into listOfData, or manually twiddle withswap()into a new empty vector that’s in listOfData. But with move semantics, the rvalue-reference-constructor (aka move constructor) ofstd::vector, invoked bypush_backas it copies in the new data, has permission to trash the old contents ofbigData– thus, it’s allowed to stealbigData‘s internal pointer and resetbigDatato an empty vector, for example.Move semantics are usually faster than COW semantics as they don’t need to maintain reference counts to shared, read-only data. They are, however, more limited – you can’t create multiple reference-counted aliases to your data with move semantics, you can just easily and conveniently shuffle data between containers.
Also note that both recent versions of GCC and Microsoft Visual C++ support rvalue references and move semantics (GCC with
--std=c++0x), so there’s no reason not to start using them today.