Public Group

templates and checking what class it is

This topic is 5102 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have template <class T> Now with a given T; How do I check wether it is a given class. Something like: if(issameclass(T, CMyClass)) { } -CProgrammer

Share on other sites
if (typeid(T) == typeid(CMyClass)) {} should do it.

EDIT: Fixed. [smile]

[Edited by - Ra on August 27, 2004 11:15:25 AM]

Share on other sites
Yeah, but that requires RTTI. You might want to check out traits at boost.org. Can't give you more details because of my lack of knowledge, but the docs should give enough info.

ah thanks.

-CProgrammer

Share on other sites
Quote:
 Original post by CoffeeMugYeah, but that requires RTTI. You might want to check out traits at boost.org. Can't give you more details because of my lack of knowledge, but the docs should give enough info.

RTTI? could you explain what that is.
-CProgrammer

Share on other sites
Quote:
 Original post by CProgrammerHow do I check wether it is a given class.

You don't - you write a specialisation (or, for a function, an overload). Can you show more code (is it a function, a class... ?)

edit - though you can do a bit of metaprogramming and make a class that will - at compile time - do a type comparison. If your compiler does support partial specialisations (i.e. not VC6)

template<class T, class U> struct IsSameClass{   enum { result = 0 };};template<class T> struct IsSameClass<T,T>{   enum { result = 1 };};...if ( IsSameClass<T, CMyClass>::value ){}

Note that it is a static type check.
Also, see boost::type_traits and boost::mpl, which have workarounds for compilers without PTS.

Share on other sites
Run-Time Type Information

Yeah, template specialization would almost definitely be a better choice. More information on what you're actually doing would really help.

Share on other sites
Quote:
 Original post by FrunyYou don't - you write a specialisation (or, for a function, an overload).

Ahh, yeah, but specialization isn't always an option (although in this case it unarguably is a better solution).

Share on other sites
BTW, Ra, there is no standard support for typeof (I hope that'll change), you probably meant typeid

Quote:
 Original post by CoffeeMugAhh, yeah, but specialization isn't always an option (although in this case it unarguably is a better solution).

When you start playing with templates, RTTI is rarely the right answer. Specialization and metaprogramming generally are. :)

With boost::type_traits, you would do
if (::boost::is_same<T,U>::value){}

Which is about the same thing as I've written, except they've coded it 'right'. [grin]

Share on other sites
boost looks nice.
But after you mentioned overloaded functions I thought odf another solution. I first thought Id end up writing the same code twice but:
Since theres only two types how about an abstract interface class for the two. Then have an overloaded function that both just call a third function which takes a pointer to the abstract class.

-CProgrammer

1. 1
2. 2
frob
15
3. 3
4. 4
5. 5

• 20
• 12
• 13
• 14
• 78
• Forum Statistics

• Total Topics
632142
• Total Posts
3004394

×