shared_ptr with template class

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

shared_ptr with template class

Pooyan McSporran
I'm in the process of converting a large pile of legacy code which uses raw
pointers to instead use boost::shared_ptr exclusively.  I've got almost all
of it converted and working, with one exception.

Normally, given a type of Foo I define a smart pointer FooPtr, for example:
  class Foo;
  typedef boost::shared_ptr<Foo> FooPtr;

But some code uses a templated type, for example:
  template<typename T> class Foo;

Ideally, I'd use a templated typedef, for example:
  template<typename T> typedef boost::shared_ptr<Foo<T> > FooTPtr;
but that is currently illegal in C++.

Has anyone found a clean workaround?

For now, I'll just do without a typedef in this case :)

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: shared_ptr with template class

David Abrahams
Pooyan McSporran <[hidden email]> writes:

> I'm in the process of converting a large pile of legacy code which uses raw
> pointers to instead use boost::shared_ptr exclusively.  I've got almost all
> of it converted and working, with one exception.
>
> Normally, given a type of Foo I define a smart pointer FooPtr, for example:
>   class Foo;
>   typedef boost::shared_ptr<Foo> FooPtr;
>
> But some code uses a templated type, for example:
>   template<typename T> class Foo;
>
> Ideally, I'd use a templated typedef, for example:
>   template<typename T> typedef boost::shared_ptr<Foo<T> > FooTPtr;
> but that is currently illegal in C++.
>
> Has anyone found a clean workaround?

How clean it is may be open to question, but:

    template <class T> class FooTPtr
      : boost::shared_ptr<Foo<T> >
    {
       // forwarding ctors here...
    };

should work.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: shared_ptr with template class

Jonathan Biggar
In reply to this post by Pooyan McSporran
Pooyan McSporran wrote:

> I'm in the process of converting a large pile of legacy code which uses raw
> pointers to instead use boost::shared_ptr exclusively.  I've got almost all
> of it converted and working, with one exception.
>
> Normally, given a type of Foo I define a smart pointer FooPtr, for example:
>   class Foo;
>   typedef boost::shared_ptr<Foo> FooPtr;
>
> But some code uses a templated type, for example:
>   template<typename T> class Foo;
>
> Ideally, I'd use a templated typedef, for example:
>   template<typename T> typedef boost::shared_ptr<Foo<T> > FooTPtr;
> but that is currently illegal in C++.
>
> Has anyone found a clean workaround?
>
> For now, I'll just do without a typedef in this case :)

You can drop a definition of the shared pointer inside the template class:

template<typename T> class Foo {
public:

        typedef shared_ptr<Foo> Ptr;

};

and then you could refer to it as Foo<T>::Ptr, although you'd need to
use "typename" in some circumstances.

--
Jonathan Biggar
[hidden email]

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: shared_ptr with template class

Jonathan Biggar
Jonathan Biggar wrote:

> You can drop a definition of the shared pointer inside the template class:
>
> template<typename T> class Foo {
> public:
>
> typedef shared_ptr<Foo> Ptr;
>
> };
>
> and then you could refer to it as Foo<T>::Ptr, although you'd need to
> use "typename" in some circumstances.

And of course that won't work if you need to use the shared_ptr in
contexts where Foo is not defined yet.

--
Jonathan Biggar
[hidden email]

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: shared_ptr with template class

Pooyan McSporran
On 15/01/06, Jonathan Biggar <[hidden email]> wrote:

> >
> > template<typename T> class Foo {
> > public:
> >
> >       typedef shared_ptr<Foo> Ptr;
> >
> > };
>
> And of course that won't work if you need to use the shared_ptr in
> contexts where Foo is not defined yet.

True, but even so it's the approach I've ended up using; seems there's
no one "ideal" solution, but this one is quite usable given the constraints.

Thanks.

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users