| View previous topic :: View next topic |
| Author |
Message |
Sam Guest
|
Posted: Thu Nov 17, 2005 2:05 am Post subject: How to typedef a template class? |
|
|
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
|
Posted: Thu Nov 17, 2005 2:16 am Post subject: Re: How to typedef a template class? |
|
|
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
|
Posted: Thu Nov 17, 2005 5:56 am Post subject: Re: How to typedef a template class? |
|
|
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
|
Posted: Thu Nov 17, 2005 7:31 am Post subject: Re: How to typedef a template class? |
|
|
"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
|
Posted: Thu Nov 17, 2005 8:23 pm Post subject: Re: How to typedef a template class? |
|
|
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
|
Posted: Thu Nov 17, 2005 8:27 pm Post subject: Re: How to typedef a template class? |
|
|
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 |
|
 |
|