templates and smart pointer

Discussion in 'C++' started by neelagain@hotmail.com, Apr 15, 2008.

  1. Guest

    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.
    , Apr 15, 2008
    #1
    1. Advertising

  2. Guest

    On Apr 15, 12:14 pm, wrote:
    > 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.
    , Apr 15, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page