incubator-stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scott Zhong" <sco...@roguewave.com>
Subject type_traits is_integral implementation
Date Sat, 23 Dec 2006 14:58:45 GMT
I try to implement type_traits using partial specialization as follows:

template <class T> struct __remove_s { typedef T type; };
template <class T> struct __remove_s<signed T> { typedef T type; };
template <class T> struct __remove_s<unsigned T> { typedef T type; };
template <class T> struct __remove_s<short T> { typedef T type; };
template <class T> struct __remove_s<long T> { typedef T type; };

template <class T> struct is_integral:
	__is_same<typename __remove_s<typename
__remove_cv<T>::type>::type, int> { };

But signed is actually a type and T is already a type, and signed int is
a compound type.  When compiling the above code in MSVC 8.0, it would
give an error that states "too many template arguments".  I went ahead
and read the book you had pointed to me, the implementation in there is
using 3 enum? with a 3rd struct containing index_of template that would
search though the enum with return value of greater than 0 if found, 0
if not found.  I decided to implement it a different way as follows:

template <typename T> struct __unsigned_ints { typedef T type; };
template <> struct __unsigned_ints<unsigned int> { typedef int type; };
template <> struct __unsigned_ints<unsigned char> { typedef int type; };
template <> struct __unsigned_ints<unsigned short int> { typedef int
type; };
template <> struct __unsigned_ints<unsigned long int> { typedef int
type; };

template <typename T> struct __signed_ints {typedef T type; };
template <> struct __signed_ints<long int> { typedef int type; };
template <> struct __signed_ints<int> { typedef int type; };
template <> struct __signed_ints<signed char> { typedef int type; };
template <> struct __signed_ints<short int> { typedef int type; };

template <typename T> struct __other_ints {typedef T type; };
template <> struct __other_ints<bool> { typedef int type; };
template <> struct __other_ints<char> { typedef int type; };
#ifndef _RWSTD_NO_WCHAR_T
template <> struct __other_ints<wchar_t> { typedef int type; };
#endif

template <class T> struct is_integral:
	__is_same<typename __other_ints<typename
__unsigned_ints<typename __signed_ints<typename
__remove_cv<T>::type>::type>::type>::type, int> { };

Not really sure the internal workings of a template, does it treat each
specialization as an enum? My implementation does generate 5 template
instantiation whenever is_integral is called, would that adversely
affect performance? Please let me know if there is a better
implementation.

Thank you,   

Yu (Scott) Zhong
Consulting Engineer
Rogue Wave Software, a Quovadxtm division
scottz@roguewave.com
ph: 303 545 3182
 

Mime
View raw message