By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

typeid and template class

P: n/a
I have defined a class named Rectangle<typename T>.

How do I test for a Rectangle? I just want to know if an object is any type
of Rectangle.

My code compiles for typeid(Rectangle<>) only if I provide a default value
for the template argument - and this is not what I want!

tooc
Jun 12 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Tooc wrote:
I have defined a class named Rectangle<typename T>.

How do I test for a Rectangle? I just want to know if an object is any type
of Rectangle.

You can't, all class templates are unique classes. You could create and
empty or abstract base class for your template and test for that.

--
Ian Collins.
Jun 12 '06 #2

P: n/a
Tooc wrote:
I have defined a class named Rectangle<typename T>.

How do I test for a Rectangle? I just want to know if an object is any type
of Rectangle.


Use a base class, and see if Rectangle<T>* converts to RectangleBase*.
More advanced tricks use SFINAE (google it)

HTH,
Michiel Salters

Jun 12 '06 #3

P: n/a

Mi*************@tomtom.com wrote:
Tooc wrote:
I have defined a class named Rectangle<typename T>.

How do I test for a Rectangle? I just want to know if an object is any type
of Rectangle.


Use a base class, and see if Rectangle<T>* converts to RectangleBase*.
More advanced tricks use SFINAE (google it)


This would only work if you're only dealing with Rectangle<T>'s or a
template function. My guess is its something like:

void do_something(Shape & s)
{

}

Jun 12 '06 #4

P: n/a
Tooc wrote:
I have defined a class named Rectangle<typename T>.

How do I test for a Rectangle? I just want to know if an object is any type
of Rectangle.

My code compiles for typeid(Rectangle<>) only if I provide a default value
for the template argument - and this is not what I want!

tooc


How about a template template parameter? Something like (syntax off the
top of my head, so...)
template <template<class> class T>
struct IsRectangle
{
enum { value = false };
};

template<>
struct IsRectangle<Rectangle>
{
enum { value = true };
};
Jun 12 '06 #5

P: n/a
red floyd wrote:
Tooc wrote:
I have defined a class named Rectangle<typename T>.

How do I test for a Rectangle? I just want to know if an object is any
type
of Rectangle.

My code compiles for typeid(Rectangle<>) only if I provide a default
value
for the template argument - and this is not what I want!

tooc


How about a template template parameter? Something like (syntax off the
top of my head, so...)
template <template<class> class T>
struct IsRectangle
{
enum { value = false };
};

template<>
struct IsRectangle<Rectangle>
{
enum { value = true };
};


And... if you need to check an object rather than a type.

template<template<class> class T, typename U>
bool object_is_rectangle(const T<U>&)
{
return IsRectangle<T>::value;
}
Jun 12 '06 #6

P: n/a
red floyd wrote:
Tooc wrote:
I have defined a class named Rectangle<typename T>.

How do I test for a Rectangle? I just want to know if an object is any
type
of Rectangle.

My code compiles for typeid(Rectangle<>) only if I provide a default
value
for the template argument - and this is not what I want!

tooc


How about a template template parameter? Something like (syntax off the
top of my head, so...)
template <template<class> class T>
struct IsRectangle
{
enum { value = false };
};

template<>
struct IsRectangle<Rectangle>
{
enum { value = true };
};


Even better is this:

template<typename T>
struct IsRectangle { enum { value = false }; };

template<typename T>
struct IsRectangle<Rectangle<T> > { enum { value = true } ; }

template<typename T>
bool is_rect(const T&)
{
return IsRectangle<T>::value;
}

This code doesn't require a template type as the argument to is_rect().
Jun 13 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.