C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Compile time testing for "type equality"?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Thu May 11, 2006 10:21 pm    Post subject: Compile time testing for "type equality"? Reply with quote



Hi,

I would like to know if there is a way to test if two types are indeed
the same. The simplest example is

typedef int A;
typedef float B;
[magic statement that results in error iff A is not same type as B]

The ability to convert from A to B and back is not sufficient. The code
above is contrived,
but the situation where it happens is that I have something as follows:

template <class T, class I = unsigned int>
class Alph {
typedef I index_type;
typedef T alphabet_type;
}

// Only makes sense if A1 and A2 are of type Alph<>
template <class A1, class A2>
Alph< std::pair<A1,A2> > cartesian_product(A1& a1, A2& a2)
{
[magic code that verifies that A1::index_type == A2::index_type]

// Construct cartesian product
...
}


Thanks,
Patrick


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
johnchx2@yahoo.com
Guest





PostPosted: Fri May 12, 2006 12:21 am    Post subject: Re: Compile time testing for "type equality"? Reply with quote



pmitran (AT) gmail (DOT) com wrote:

Quote:
I would like to know if there is a way to test if two types are indeed
the same.

You might have a look at is_same, from the Boost Type Traits library:

http://www.boost.org/doc/html/boost_typetraits/reference.html#boost_typetraits.is_same


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
David Abrahams
Guest





PostPosted: Fri May 12, 2006 3:21 am    Post subject: Re: Compile time testing for "type equality"? Reply with quote



pmitran (AT) gmail (DOT) com writes:

Quote:
Hi,

I would like to know if there is a way to test if two types are indeed
the same. The simplest example is

typedef int A;
typedef float B;
[magic statement that results in error iff A is not same type as B]

#include <boost/mpl/assert.hpp>
#include <boost/type_traits/is_same.hpp>

BOOST_MPL_ASSERT((boost::is_same<A,B>));

http://www.boost.org
http://www.boost-consulting.com/mplbook

HTH,
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jeffrey Schwab
Guest





PostPosted: Fri May 12, 2006 4:21 am    Post subject: Re: Compile time testing for "type equality"? Reply with quote

pmitran (AT) gmail (DOT) com wrote:

Quote:
I would like to know if there is a way to test if two types are indeed
the same.

template<typename T>
bool same_type(T const&, T const&) {
return true;
}

template<typename T, typename U>
bool same_type(T const&, U const&) {
return false;
}

#include <iostream>

int main() {
std::cout << "same_type(int, int)\t" << same_type(1, 2) << '\n';
std::cout << "same_type(int, float)\t" << same_type(1, 2.0) <<
'\n';
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Carlos Moreno
Guest





PostPosted: Mon May 15, 2006 12:22 am    Post subject: Re: Compile time testing for "type equality"? Reply with quote

Jeffrey Schwab wrote:

Quote:
template<typename T
bool same_type(T const&, T const&) {
return true;
}

template<typename T, typename U
bool same_type(T const&, U const&) {
return false;
}

#include <iostream

int main() {
std::cout << "same_type(int, int)\t" << same_type(1, 2) << '\n';
std::cout << "same_type(int, float)\t" << same_type(1, 2.0)
'\n';

Tiny nit pick: that should be same_type(int, double), which is really
what you're testing (2.0 is a *double* literal, not a float literal)

Carlos
--

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jon Colverson
Guest





PostPosted: Wed May 17, 2006 10:21 am    Post subject: Re: Compile time testing for "type equality"? Reply with quote

pmitran (AT) gmail (DOT) com wrote:
Quote:
but the situation where it happens is that I have something as follows:

template <class T, class I = unsigned int
class Alph {
typedef I index_type;
typedef T alphabet_type;
}

// Only makes sense if A1 and A2 are of type Alph
template <class A1, class A2
Alph< std::pair<A1,A2> > cartesian_product(A1& a1, A2& a2)
{
[magic code that verifies that A1::index_type == A2::index_type]

// Construct cartesian product
...
}

You can state your requirements more precisely in the template and
function parameters to avoid the need for an explicit check:

template <class T1, class T2, class I>
Alph< std::pair<Alph<T1, I>,Alph<T2, I> > >
cartesian_product(Alph<T1, I>& a1, Alph<T2, I>& a2)
{
// Construct cartesian product
...
}

--
Jon Colverson -- looking for a game programming job in London or the
south-east of England: http://vcxz.co.uk/cv.pdf

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.