Prev: How to obtain a typedef for the unsigned version of a signed character type
Next: Arity in C++0x Variadic Class Method
From: Daniel Krügler on 7 Mar 2010 08:34 On 7 Mrz., 23:01, JohnW <john.wilkinso...(a)yahoo.com> wrote: > I want to obtain a typedef for the unsigned version of a signed > character type. The example below demonstrates the problem, I have a > typedef for a signed character type but don't know how to obtain the > corresponding unsigned type. > > template< class string_t > > struct Example > { > typedef typename string_t::value_type char_t; > typedef unsigned char_t uchar_t; // doesn't compile > > // ... > }; You need a mapping for this. Either you define one by your self, e.g. template<typename T> struct make_unsigned; template<> struct make_unsigned<char> { typedef unsigned char type; }; .... or you use std::make_unsigned from a library of a C++0x-compatible compiler or use boost::make_signed provided by the boost library which does the same for you. HTH & Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Arijit on 7 Mar 2010 12:06
On Mar 8, 3:01 am, JohnW <john.wilkinso...(a)yahoo.com> wrote: [snip] > template< class string_t > > struct Example > { > typedef typename string_t::value_type char_t; > typedef unsigned char_t uchar_t; // doesn't compile > > // ... > > }; > > Thanks > John W Use the standard solution - add a level of indirection :) #include <iostream> template <class T> struct make_unsigned { }; template <> struct make_unsigned<signed char> { typedef unsigned char unsigned_type; }; template <> struct make_unsigned<unsigned char> { typedef unsigned char unsigned_type; }; template <> struct make_unsigned<char> { typedef unsigned char unsigned_type; }; template <> struct make_unsigned<int> { typedef unsigned int unsigned_type; }; template <> struct make_unsigned<unsigned int> { typedef unsigned int unsigned_type; }; // other types like short int, long, wchar_t struct s { typedef int value_type; }; int main() { make_unsigned<s::value_type>::unsigned_type a = -1; std::cout << a << std::endl; } Regards Arijit -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |