Boost logo

Boost Users :

Subject: [Boost-users] Returning a shared_ptr or raw pointer from a template function
From: Ryan McConnehey (mccorywork_at_[hidden])
Date: 2009-03-29 17:49:27


I want to allow the user to specify if a function returns a raw pointer
or a shared_ptr from a creation type function. The code I've started
with is as follows:

class Track{};

class Tm{};

template <class R, class T>
R create(void)
{
 R temp(new T());
 return temp;
}

This allows to the user to call the function like:

Tm* tm3 = create<Tm*, Tm>();
boost::shared_ptr<Tm> tm4 = create<boost::shared_ptr<Tm>, Tm>();

I would like to have a class that has this create function and upon
creation of the class specify if the create function returns a raw
pointer or shared_ptr.

template <class U> //The U specifies is the create function returns a
raw or shared pointer
class Rdi
{
 template <class S>
 U create(void)
 {
   U temp(new S());
   return temp;
 }
};

How would I modify this class to allow the user to make the following
calls?

Rdi<boost::shared_ptr> fun1;
Tm* tm3 = fun1.create<Tm>();

Rdi<raw Pointer???> fun2;
boost::shared_ptr<Tm> tm4 = fun2.create<Tm>();

Any help would be appreciated.

Ryan


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net