Jump to content
  • Advertisement
Sign in to follow this  
Puzzler183

Checking if two template arguments are the same type [EDIT: And removing references]

This topic is 4842 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

So I've found myself in a situation where I need to check if two template arguments are the same type. I was kind of hoping for some sort of template <typename T, typename U> bool isSame() thing. However, I have no idea how to go about doing this... Any ideas? Oh, and don't post "use boost type traits". I'm not going to bring boost into this project. And also don't post "look at boost type traits" - I have and if you'd ever like to go to macro hell, you should too. :-P [Edited by - Puzzler183 on August 15, 2005 7:00:47 PM]

Share this post


Link to post
Share on other sites
Advertisement
Well here's something I quickly hacked together:
#include <iostream>

template < typename TYPE1, typename TYPE2 >
struct IsSame
{

static bool const result = false;

};

template < typename TYPE >
struct IsSame< TYPE, TYPE >
{

static bool const result = true;

};

template < typename TYPE1, typename TYPE2 >
class Test
{

public:

Test()
{
if (IsSame< TYPE1, TYPE2 >::result)
{
std::cout << "TYPE1 and TYPE2 are identical\n";
}
else
{
std::cout << "TYPE1 and TYPE2 are different\n";
}
}

};

class Test2
{
};

struct Test3
{
};

union Test4
{
};

int main()
{
Test< int, float > t1;
Test< int, int > t2;
Test< float, float > t3;
Test< char *, float * > t4;
Test< void *, int * > t5;
Test< Test2, int > t6;
Test< Test2, Test2 > t7;
Test< Test2, Test3 > t8;
Test< Test3, Test4 > t9;
Test< Test4, Test4 > t10;
}

Not sure that it's foolproof, but it may be sufficient for your needs.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Does your compiler support partial template specialization?
And if not, since you only seem to need the results at runtime, perhaps you could compare their typeids instead.
It seems to work in VC6 at least.

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
Well here's something I quickly hacked together:
*** Source Snippet Removed ***
Not sure that it's foolproof, but it may be sufficient for your needs.

Enigma


Wow, I feel stupid. Thanks.

Share this post


Link to post
Share on other sites
If it does:

template<class T, class U> struct is_same
{
enum { value = false };
};

template<class T> struct is_same<T,T>
{
enum { value = true };
};


if not, things become a bit more complicated...

Of course, remember that int, const int and int& are all different types.

Share this post


Link to post
Share on other sites
Quote:
Original post by doynax
Quote:
Original post by SiCrane
Does your compiler support partial template specialization?
And if not, since you only seem to need the results at runtime, perhaps you could compare their typeids instead.
It seems to work in VC6 at least.


Which is slow. Enigma's way is how you do it.

Share this post


Link to post
Share on other sites
Quote:
Original post by vNistelrooy
Quote:
Original post by doynax
Quote:
Original post by SiCrane
Does your compiler support partial template specialization?
And if not, since you only seem to need the results at runtime, perhaps you could compare their typeids instead.
It seems to work in VC6 at least.


Which is slow. Enigma's way is how you do it.
Perhaps, but it does require not require partial template specialization (for those of us still supporting VC6).
VC6 appears to be intelligent enough to optimize them away though, so the real disadvantage is that it can't be used at compile time.

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!