BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to typedef a template class?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Sam
Guest





PostPosted: Thu Nov 17, 2005 2:05 am    Post subject: How to typedef a template class? Reply with quote



Is there a way to typedef a template class so I can do something like this:

#if defined( I_LIKE_BOOST )
typedef boost::smart_ptr<class T> smart_pointer;
#elif defined( I_LIKE_LOKI )
typedef Loki::SmartPtr<class T> smart_pointer;
#endif

int main()
{
smart_pointer<int> aa( new int );
}

Thanks in advance.

--
Please remove "sam_" from email address.
Back to top
liz
Guest





PostPosted: Thu Nov 17, 2005 2:16 am    Post subject: Re: How to typedef a template class? Reply with quote



On Thu, 17 Nov 2005 04:05:37 +0200, Sam wrote:

Quote:
#if defined( I_LIKE_BOOST )
typedef boost::smart_ptr<class T> smart_pointer;

did you try it?
--
liz

Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Thu Nov 17, 2005 5:56 am    Post subject: Re: How to typedef a template class? Reply with quote



Sam <sam_sakarab (AT) yahoo (DOT) com> writes:

Quote:
Is there a way to typedef a template class so I can do something like this:

#if defined( I_LIKE_BOOST )
typedef boost::smart_ptr<class T> smart_pointer;
#elif defined( I_LIKE_LOKI )
typedef Loki::SmartPtr<class T> smart_pointer;
#endif

int main()
{
smart_pointer<int> aa( new int );
}

Thanks in advance.

No. A template is not a type, and you therefore cannot create a
typedef to it. Perhaps in a future revision to the C++ language
they'll make template typedefs, but today it cannot be done. You
would need to instantiate the template, and make a typedef to that
instantiation, which is a type. For example:

#if defined( I_LIKE_BOOST )
typedef boost::smart_ptr<int> smart_pointer;
#elif defined( I_LIKE_LOKI )
typedef Loki::SmartPtr<int> smart_pointer;
#endif

int main()
{
smart_pointer aa(new int);
}

Etc.

--
Chris (TeamB);

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Nov 17, 2005 7:31 am    Post subject: Re: How to typedef a template class? Reply with quote


"Sam" <sam_sakarab (AT) yahoo (DOT) com> wrote


Quote:
Is there a way to typedef a template class so I
can do something like this:

Chris already explained why that will not work. What you can do, however,
is use a macro instead of a typedef:

#if defined( I_LIKE_BOOST )
#define smart_pointer boost::smart_ptr
#elif defined( I_LIKE_LOKI )
#define smart_pointer Loki::SmartPtr
#else
#error "No smart pointer library defined"
#endif

int main()
{
smart_pointer<int> aa( new int );
}


Gambit



Back to top
Sam
Guest





PostPosted: Thu Nov 17, 2005 8:23 pm    Post subject: Re: How to typedef a template class? Reply with quote

liz wrote:
Quote:
On Thu, 17 Nov 2005 04:05:37 +0200, Sam wrote:


#if defined( I_LIKE_BOOST )
typedef boost::smart_ptr<class T> smart_pointer;


did you try it?

Yes, and much more forms.

--
Please remove "sam_" from email address.

Back to top
Sam
Guest





PostPosted: Thu Nov 17, 2005 8:27 pm    Post subject: Re: How to typedef a template class? Reply with quote

Tom Widmer wrote:

Quote:

In addition to everyone else's comments, the standard workaround for a
"templatedef" is this:

template <class T
class smart_ptr_g
{
#if defined( I_LIKE_BOOST )
typedef boost::smart_ptr #elif defined( I_LIKE_LOKI )
typedef Loki::SmartPtr<T> type;
#endif
};

int main()
{
smart_ptr_g<int>::type aa(new int);
}

Tom

This one looks good enough. I should have thought of it myself.

Thank you all for your answers.

--
Please remove "sam_" from email address.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.