Re: typeid and template class
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().