473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

typeid and template class

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
6 4342
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
3
by: David Komanek | last post by:
Hi all, I am trying to learn more about how to use g++/Cygwin to produce dll files on WinXP. And I have a problem which at the first look seems to be an obvious dll-export problem, but I don't...
2
by: Rune Vistnes | last post by:
Hey, I am trying to wrap an unmanaged library in managed c++ so that I can use this library in other .NET languages, such as C#. I've been successful for the most part this far, but I'm having a...
10
by: mast2as | last post by:
Is it possible to limit a template class to certain types only. I found a few things on the net but nothing seems to apply at compile time. template <typename T> class AClass { public:...
7
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
4
by: thomas | last post by:
template <class T> class BufferQueue{ public: int BufferQueue<T>::pushToQueue(T pkt, int timeout); } template<class T> int BufferQueue<T>::pushToQueue(T pkt, ACE_Time_Value *timeout){...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.