Jump to content
  • Advertisement
Sign in to follow this  
CProgrammer

templates and checking what class it is

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

If you intended to correct an error in the post then please contact us.

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 this post


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

EDIT: Fixed. [smile]

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by CoffeeMug
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.


RTTI? could you explain what that is.
-CProgrammer

Share this post


Link to post
Share on other sites
Quote:
Original post by CProgrammer
How 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 this post


Link to post
Share on other sites
Quote:
Original post by Fruny
You 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 this post


Link to post
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 CoffeeMug
Ahh, 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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!