473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

templates and smart pointer

Hi,

I want to write a template _something_ like this:

class baseStoreType {};

template <class storeTypeclass 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.
Jun 27 '08 #1
1 1134
On Apr 15, 12:14*pm, neelag...@hotmail.com wrote:
Hi,

I want to write a template _something_ like this:

class baseStoreType {};

template <class storeTypeclass 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 <classclass Tclass Store {
public:
typedef T<BaseStoreTypeBaseStoreTypePtr;
BaseStoreTypePtr storeData;
};
typedef Store<std::auto_ptrStoreSmartPtr;
typedef Store<ActsLikePtrStoreSimplePtr;

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.
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tobias Langner | last post by:
I try to program a smart-pointer using policies. I rely heavyly on templates during this. The program compiles fine - but I get a linker error: test.o(.text+0x3e): In function `testSmartPtr()':...
8
by: bartek | last post by:
Hello, I've been toying around with expression templates recently while writing a simple static lexical analyser, hence I have some questions which keep bothering me... The expression objects...
1
by: Shiva | last post by:
I saw C++ FAQ LITE on how to go about creating a smart pointer. I tried my hand at creating a smart pointer using templates. I'm thinking of creating just one Pointer class to create pointers for...
0
by: lothar.behrens | last post by:
Hi, a long time ago I had implemented my version - or attempt - of a smart pointer. It is not well and most books I have read afterwards are using templates :-( It is macro based and now I...
10
by: makc.the.great | last post by:
now that I am looking at templates, there's the question. why same effect couldn't/shouldn't be achieved with inheritance? provided the difference of the two, when and where do I use templates...
8
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
1
by: ilieyah | last post by:
Hi! Sorry for the silly title, but I just couldn't come up with anything sensible. I have some test code here to demonstrate an issue I'm having. What I'm trying to do is implement a reference...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.