Boost logo

Boost :

From: Larry Evans (cppljevans_at_[hidden])
Date: 2006-03-24 22:41:11

On 03/24/2006 06:43 PM, Joe Gottman wrote:
> Another possible advantage of new_shared_ptr mentioned by the article is
> that it could reduce the number of news required to create the shared_ptr
> from 2 to 1. In the code
> shared_ptr<int> p(new int(7));
> new is called twice: when the int is created and inside the shared_ptr's
> constructor to construct an object of a type inheriting from
> boost::sp_counted_base. The constructor is careful to maintain exception
> safety, but some people might find the two calls to new to be inefficient.
> The article suggested that new_shared_ptr could create an object containing
> an int and an intrusive count, so that it would be unnecessary to call new
> to create the int pointer.
Avoiding 2 new calls is one of the applications for the:

   template<typename T, template<typename>class >OverHead

template mentioned in the post I bcc'ed to David. I bcc'ed because
I was having trouble posting. It wasn't mentioned in so I'll
repeat it here:

> The auto_overhead<Type,template<typename>class Overhead> template class
> in:
> does *almost* this. If the Overhead<Type> were an empty class (i.e.
> no members) then it would essentially create a Type* with the given
> arguments. Then new_shared_ptr would simply create an
> auto_overhead<Type,...> and pass the Type* (using
> auto_overhead::referent()) to the share_ptr<Type> CTOR. IOW, instead
> of:
> return shared_ptr<Type>(new Type(a1))
> there would be:
> return shared_ptr<Type>(auto_overhead<Type,Empty>().referent());

The Overhead<Type> could create, with a single call to new, an object
containing a refcount followed by Type. The auto_overhead::reference()
returns the Type* while the auto_overhead::overhead() returns
overhead_type* where overhead_type could be a refcount or any other
overhead, including strong_count and weak_count, depending on the
Overhead template argument.

Boost list run by bdawes at, gregod at, cpdaniel at, john at