Boost logo

Boost :

From: Eric Woodruff (Eric.Woodruff_at_[hidden])
Date: 2002-09-03 10:17:40


Thanks Peter!

(Implementing all the various constructors of New was not fun, const,
nonconst, const, etc. I only support 4 parameters for now :/)

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

From: "Larry Evans" <jcampbell3_at_[hidden]>
> Peter Dimov wrote:
>
> >From: "Eric Woodruff" <Eric.Woodruff_at_[hidden]>
> >
> >
> >>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);
> >>}
> >>
> >>
> >
> >You can use
> >
> >template<class Y> operator shared_ptr<Y>() const;
> >
> >but actually it seems to me that the above should work even with a
> >non-templated operator. This is the auto_ptr_ref idiom that somehow
manages
> >to sneak two user-defined conversions:
> >
> >auto_ptr<X> someMethod()
> >{
> > return auto_ptr<X>(new X);
> >}
> >
> >goes through operator auto_ptr_ref(). Of course some compilers frown at
it.
> >
> >
> More explicitly, do you mean:
>
> auto_ptr<ConcreteType> New<ConcreteType>::someMethod()
> { return auto_ptr<ConcreteType>( new ConcreteType(p1,p2,p3));}
>
> And then:
>
> auto_ptr<ConcreteType> --converted to--> auto_ptr_ref<AbstractType>
> via auto_ptr<ConcreteType>::operator auto_ptr_ref<AbstractType>()
>
> and then:
>
> auto_ptr_ref<AbstractType> --converted to-->auto_ptr<AbstractType>
> via auto_ptr<AbstractType>::auto_ptr(auto_ptr_ref<AbstractType>)
>
> and finally, the auto_ptr<AbstractType> is used to created the
> shared_ptr<AbstractType>?

I meant that in

auto_ptr<ConcreteType> New<ConcreteType>::someMethod()
   { return auto_ptr<ConcreteType>( new ConcreteType(p1,p2,p3));}

the result value is initialized from the temporary (I think) by using

  operator auto_ptr_ref<ConcreteType>()

and then

  auto_ptr(auto_ptr_ref<ConcreteType>)

so it seemed to me that in

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

a similar reasoning can be applied,

  New<ConcreteType>::operator shared_ptr<ConcreteType>

and then

  shared_ptr<AbstractType>(shared_ptr<ConcreteType>)

but the compilers don't agree. I've read about auto_ptr(_ref) a bit and this
gave me a headache, so I don't know what's happening.

Anyway, this works:

#include <boost/shared_ptr.hpp>

using boost::shared_ptr;

template<class T> struct New
{
    template<class Y> operator shared_ptr<Y>() const
    {
        return shared_ptr<T>();
    }
};

struct A
{
};

struct C: public A
{
};

shared_ptr<A> f()
{
    return New<C>();
}

int main()
{
    f();
}

_______________________________________________
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