Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost shared_ptr: using unique() to implement copy on write

Could someone explain what boost shared_ptr manual means by this:

If you are using unique() to implement copy on write, do not rely on a specific value when the stored pointer is zero.

Thanks.

like image 680
pic11 Avatar asked Sep 23 '26 00:09

pic11


1 Answers

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.

like image 83
Potatoswatter Avatar answered Sep 25 '26 19:09

Potatoswatter