 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu May 11, 2006 10:21 pm Post subject: Compile time testing for "type equality"? |
|
|
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
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Fri May 12, 2006 3:21 am Post subject: Re: Compile time testing for "type equality"? |
|
|
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
|
Posted: Fri May 12, 2006 4:21 am Post subject: Re: Compile time testing for "type equality"? |
|
|
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
|
Posted: Mon May 15, 2006 12:22 am Post subject: Re: Compile time testing for "type equality"? |
|
|
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
|
Posted: Wed May 17, 2006 10:21 am Post subject: Re: Compile time testing for "type equality"? |
|
|
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 |
|
 |
|
|
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
|
|