[intrusive_ptr] Constructor not explicit.

Just wondered why intrusive_ptr(T * p, bool add_ref = true); isn't explicit? The second arg defaults so (using bcc32-5.6.4), an intrusive_ptr can be explicitly constructed from a raw ptr. Is this intended when shared_ptr is designed to prevent this? Cheers Russell

Russell Hind wrote:
Just wondered why intrusive_ptr(T * p, bool add_ref = true); isn't explicit? The second arg defaults so (using bcc32-5.6.4), an intrusive_ptr can be explicitly constructed from a raw ptr. Is this intended when shared_ptr is designed to prevent this?
shared_ptr can only be constructed from a raw pointer that has been freshly obtained via 'new T'. Constructing a shared_ptr from an arbitrary raw pointer is usually a recipe for disaster. intrusive_ptr's strength is that it can freely interoperate with raw pointers, including those that are already managed by another intrusive_ptr. It is much safer to construct an intrusive_ptr from an arbitrary raw pointer.
participants (2)
-
Peter Dimov
-
Russell Hind