Boost shared_ptr: using unique() to implement copy on write
Could someone explain what boost shared_ptr manual means by this:
If yo开发者_如何学JAVAu are using unique() to implement copy on write, do not rely on a specific value when the stored pointer is zero.
Thanks.
Copy-on-write is a storage scheme where copies of an object with duplicate values are represented by a single object. Only when you try to change one is it copied to a new, unique object.
Boost supports this by telling you whether a given shared_ptr
is supporting more than one reference. If it is, then writing to the object will require making a copy.
The manual is saying that if you have NULL pointers in such a scheme, they might report being either unique or not. Really, Boost is being generous by even allowing such an operation.
Basically, what it means is that if you have a shared_ptr
that doesn't point to anything, it might or might not say that it is unique. However, you should know that COW has been ditched by almost all major string libraries and disallowed for C++0x because it sucks, basically, so you want to be careful about doing this.
精彩评论