templates and smart pointer

N

neelagain

Hi,

I want to write a template _something_ like this:

class baseStoreType {};

template <class storeType> class Store {
storeType storeData;
};

For template parameter storeType, there should be two options :

storeType = baseStoreType * or any other class derived from
baseStoreType

- OR -

storeType = smart_ptr<baseStoreType * or any other class derived from
baseStoreType>

where smart_ptr can be std::auto_ptr, boost::shared_ptr and so on.

The simple solution could be

typedef Store<baseStoreType *> BaseStore;

but that means template parameter storeType can be of any type when my
requirements are different (to allow only types with/without smart
pointers derived from baseStoreType).

Is there a better way?

Thanks in advance,
- Neel.
 
N

neelagain

Hi,

I want to write a template _something_ like this:

class baseStoreType {};

template <class storeType> class Store {
storeType storeData;

};

For template parameter storeType, there should be two options :

storeType = baseStoreType * or any other class derived from
baseStoreType

- OR -

storeType = smart_ptr<baseStoreType * or any other class derived from
baseStoreType>

where smart_ptr can be std::auto_ptr, boost::shared_ptr and so on.

The simple solution could be

typedef Store<baseStoreType *> BaseStore;

but that means template parameter storeType can be of any type when my
requirements are different (to allow only types with/without smart
pointers derived from baseStoreType).

Is there a better way?

Thanks in advance,
- Neel.

Hi,

This is what I came up with:

class BaseStoreType {
public: int dummy;
};


template<class T>
class ActsLikePtr // Are operators enough or need some more?
{
public:
T* operator->() const { return pType; }
T& operator *() const { return (*pType); }
T* get() const { return pType; }
private:
T * pType;
};


template <template <class> class T> class Store {
public:
typedef T<BaseStoreType> BaseStoreTypePtr;
BaseStoreTypePtr storeData;
};


typedef Store<std::auto_ptr> StoreSmartPtr;
typedef Store<ActsLikePtr> StoreSimplePtr;

Am I missing something or there is a better way to do it (using any
template in boost or std for example instead of ActsLikePtr)?

Thanks,
- Neel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top