Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-07-19 11:53:20

From: "Navi Singh" <singh_at_[hidden]>
> > > > > ptr<test1> t1(new test1); // ptr is a shared_ptr.
> > > > > ptr<test2> t2(new test2); // ptr is an intrusive ptr.
> > > >
> > > > You could do that, but why not simply restating the above as:
> > > >
> > > > shared_ptr<test1> t1(new test1);
> > > > intrusive_ptr<test2> t2(new test2);
> > > >
> >
> > I have never liked the kind of design where the same class template is
> > "overloaded" to mean different things, depending on what is "best for
> > user." That's like... like... like making vector<T> not a vector for
> T!
> >
> > Let me speak on the user's behalf here: I am perfectly capable of using
> the
> > appropriate pointer type, thank you very much.
> >
> Peter,
> Are you saying that there are philosophical issues with the proposed ptr?
> was thinking along the lines of having everyone in my group use the
> ptr, and have them decide while writing the classes, whether they want ptr
> to be intrusive or not. If its a bad idea, I'd like to hear more about

Not really. I don't want to incorporate the automatic selection into
shared_ptr, but there is nothing wrong in writing and using your own type
selector, if you really need to.

> If there are no technical issues, could you provide some guidance as to
> I need to do to make this work. It would be even nicer if someone posted a
> complete implementation :) I don't have enough expertise to make the
> inclusive_ptr work :(

The easiest way is to use a 'generator':

template<class T> struct pointer_generator
    typedef typename mpl::if_c< is_convertible< T *, counted_base *>::value,
intrusive_ptr<T>, shared_ptr<T> >::type type;

(I'm not sure that I have the syntax right.)

Its use would be:

pointer_generator<test1>::type t1(new test1);

Getting rid of the ::type would expand the implementation a bit:

template<class T> class ptr: public pointer_generator<T>::type
// replicate all necessary constructors

The 'right way' is to use private inheritance and using declarations; it's
also much more verbose.

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