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)); //
inconvenient
}

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
better
}

(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
to
> _construct_ a shared_ptr using new, when someone is sticking with the
> reference-counted paradigm?

Yes.

> (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
don't
> 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
the
> 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
to
> 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
{
private:

    X(...);

public:

    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
make
> 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
{
public:

    operator shared_ptr<T> () const;
}

> Since I've written New<>, I've used it almost everywhere. I can't think of
a
> 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:
http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk