Boost Shared_ptr:使用unique()实现写时复制
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
写时复制是一种存储方案,其中具有重复值的对象的副本由单个对象表示。仅当您尝试更改一个对象时,它才会被复制到一个新的、唯一的对象中。
Boost 通过告诉您给定的
shared_ptr
是否支持多个引用来支持这一点。如果是,那么写入该对象将需要制作一个副本。手册说,如果在这样的方案中存在 NULL 指针,它们可能会报告是否唯一。事实上,Boost 甚至允许这样的操作,也是很慷慨的。
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.
基本上,它的意思是,如果您有一个不指向任何内容的
shared_ptr
,它可能会或可能不会说它是唯一的。但是,您应该知道 COW 已被几乎所有主要字符串库抛弃,并且在 C++0x 中不允许使用,因为它基本上很糟糕,因此您在执行此操作时要小心。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.