typeid and template class

T

Tooc

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
 
I

Ian Collins

Tooc said:
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.
 
M

Michiel.Salters

Tooc said:
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
 
U

u.int.32.t

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)
{

}
 
R

red floyd

Tooc said:
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 };
};
 
R

red floyd

red said:
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;
}
 
R

red floyd

red said:
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().
 

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
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top