Is this possible with a template?

J

Jamie Burns

Hello,

I have an application which uses a class called "rSharedPointer". I want to
replace it with the Boost shared pointer but would like to keep the old name
and not modify any existing code.

I have tried doing the following:

typedef boost::shared_ptr<T> rSharedPointer;

and:

template<typename T> typedef boost::shared_ptr<T> rSharedPointer;

But it will not compile?

Any ideas?

Jamie.
 
V

Victor Bazarov

Jamie Burns said:
I have an application which uses a class called "rSharedPointer". I want
to replace it with the Boost shared pointer but would like to keep the old
name and not modify any existing code.

I have tried doing the following:

typedef boost::shared_ptr<T> rSharedPointer;

and:

template<typename T> typedef boost::shared_ptr<T> rSharedPointer;

But it will not compile?

Any ideas?

If 'rSharedPointer' is a _class_, you cannot replace it with a template
in your code, the use for classes and templates is different. So, even
if there were (and there isn't) support for template typedefs, how would
you then use your 'rSharedPointer'?

If, contrary to your own words, 'rSharedPointer' is, in fact, a template,
then you could try using a macro:

#define rSharedPointer boost::shared_ptr

V
 
J

Jamie Burns

Thanks Victor.

Yes, it was a template, not a class!

The #define works a treat :eek:)

Jamie.
 
A

Andrey Tarasevich

Jamie said:
...
I have an application which uses a class called "rSharedPointer". I want to
replace it with the Boost shared pointer but would like to keep the old name
and not modify any existing code.

I have tried doing the following:

typedef boost::shared_ptr<T> rSharedPointer;

and:

template<typename T> typedef boost::shared_ptr<T> rSharedPointer;

But it will not compile?

Any ideas?
...

It looks like you need a template 'typedef'. C++ doesn't have template
'typedef's, but there is a well-known idiom that comes pretty close

template<typename T> struct rSharedPointer {
typedef boost::shared_ptr<T> type;
};

However, in this case you'll have to declare your 'rSharedPointer's as
follows

rSharedPointer<int>::type ptr;

This is probably different from what you currently have. If this is
unacceptable for you, you'll probably have to stick with
preprocessor-based solution suggested by Victor.
 

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
474,196
Messages
2,571,036
Members
47,631
Latest member
kukuh

Latest Threads

Top