utility::pointer::owning_ptr Class Reference
Detailed Description
owning_ptr is designed to be a shared-ownership intrusive reference counted smart pointer much like boost::intrusive_ptr but with a few modifications.
Differences from intrusive_ptr:
- Construction and assignment from objects, not just pointers to them, is supported for syntactic flexibility.
- Assignment operators use direct assignment and reference updating rather than the swap idiom that intrusive_ptr uses for speed (about 2x faster) since we are not designing for recovery from exceptions in pointer assignment.
-
operator< for owning_ptr to pointer comparisons so you can use the address of a pointee as a lookup key.
Remarks
Ownership of the pointee object can be shared with intrusive_ptr or other smart pointers that use the same reference counting mechanism in the object.
Pointers to const objects: owning_ptr< Type const >
- Can create pointers to const objects
- This allows construction from a temporary giving undefined behavior but this is true of all C++ smart pointers
Virtual functions shouldn't return owning_ptr because, like all template-based smart pointers, it doesn't support covariant return types but raw pointer return values can be assigned to owning_ptr.
Casts: The cast functions are merely a convenience for owning_ptr (unlike for non-intrusive shared ownership pointers) so for:
owning_ptr< Base > pB( new Derived() );
the cast:
dynamic_pointer_cast< Derived >( pB );
is equivalent to:
owning_ptr< Derived >( dynamic_cast< Derived * >( pB() ) );