Boost logo

Boost :

From: Eric Woodruff (Eric.Woodruff_at_[hidden])
Date: 2002-09-02 09:40:27

Yes, of course, I implemented it with operator shared_ptr<T> () const. The
problem is this:

shared_ptr<AbstractType> someMethod () {
    return New<ConcreteType> (p1, p2, p3);

requires this instead:

shared_ptr<AbstractType> someMethod () {
    return shared_ptr<ConcreteType> (New<ConcreteType> (p1, p2, p3)); //

It is possible to do this:

shared_ptr<AbstractType> someMethod () {
    return New<ConcreteType, AbstractType> (p1, p2, p3);
    // still annoying -- implicit shared_ptr (New<T> n) constructor would be

(I saying that potentially New might return a weak_ptr, a scoped_ptr, or
something else besides a shared_ptr depending on user preference, but that
just gets hairy.)

----- Original Message -----
From: Peter Dimov
Newsgroups: gmane.comp.lib.boost.devel
Sent: Monday, 2002:September:02 9:20
Subject: Re: Re: Re: Proposals: p2moffsets,prebuild<>,squad_ptr<>(ofcourse)

From: "Eric Woodruff" <Eric.Woodruff_at_[hidden]>
> Speaking of shared_ptrs, has anyone considered how annoying it is to have
> _construct_ a shared_ptr using new, when someone is sticking with the
> reference-counted paradigm?


> (I bring this up because I was thinking about the behavior of supporting
> assignment to a pointer, which can probably be done as a policy, but I
> think the copy constructor can.)

Sorry, I don't understand what you mean by this.

> I considered having some global method that would be overloaded to take
> constructor parameters of an object and return a new shared_ptr like
> New<int> (2);
> The problem I had with that is the fact that I may want to force my class
> be used on the heap, non-copyable, etc, so I could privitize all
> constructors, but make New<> a friend.

The typical solution is

class X



    static shared_ptr<X> create(...); // (de)allocation details hidden

> However, New<> is best done as a
> class that to have a single friend statement (instead of one per
> constructor). With that change, New <int> (3) is a temporary instance of
> New<>, not a shared_ptr that is returned. This can be a hassle (the same
> hassle with new ()) when having to use a constructor of a shared_ptr to
> sure it is the right type -- though generally not a problem if shared_ptr
> could be constructed with a New<>.

A possible solution is

template<class T> class New

    operator shared_ptr<T> () const;

> Since I've written New<>, I've used it almost everywhere. I can't think of
> situation where I would want a shared_ptr, unless I wanted some other kind
> of boost::*_ptr. There is potential for some policies as additional
> parameters to the New<> class.

Sorry, the next to last sentence doesn't make sense to me. You wouldn't want
a shared_ptr unless you want some other kind of ptr?

Unsubscribe & other changes:

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