422,024 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,024 IT Pros & Developers. It's quick & easy.

convert signed int to unsigned int

P: n/a
Hi. Is there a way to convert the type signed int to the type unsigned int,
char to unsigned char, signed char to unsigned char, and so on for all the
fundamental integer types? Something like

template <>
struct to_unsigned<signed int> : public std::unary_function<signed int,
unsigned int>
{
unsigned int operator()(signed int x) const { return x; }
};

template <>
struct to_unsigned<unsigned int> : public std::unary_function<unsigned int,
unsigned int>
{
unsigned int operator()(unsigned int x) const { return x; }
};

template <>
struct to_unsigned<char> : public std::unary_function<char, unsigned char>
{
unsigned char operator()(char x) const { return x; }
};

template <>
struct to_unsigned<signed char> : public std::unary_function<signed char,
unsigned char>
{
unsigned char operator()(signed char x) const { return x; }
};

template <>
struct to_unsigned<unsigned char> : public std::unary_function<unsigned
char, unsigned char>
{
unsigned char operator()(unsigned char x) const { return x; }
};

But I'd like to cover all the fundamental integer types (where do they end:
at long or long long or long long long). Also, is there a way to do it that
involves less typing? Also, I want tounsigned<double> to not have a typedef
result defined.

Thanks.

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Siemel Naran wrote:
Hi. Is there a way to convert the type signed int to the type unsigned int,
char to unsigned char, signed char to unsigned char, and so on for all the
fundamental integer types? Something like


Hum... there's an implicit type conversion between signed and unsigned.
Jonathan
Jul 22 '05 #2

P: n/a
"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:Oosqd.21862
Siemel Naran wrote:

Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like


Hum... there's an implicit type conversion between signed and unsigned.


True, but I want to write a template class as in my previous post, so that I
can write generic code. The generic code will take a reference to a type T,
be it a signed or unsigned integer, and convert it to a reference to an
unsigned integer. So I need to have the type spelled out for the
reinterpret_cast.
Jul 22 '05 #3

P: n/a
Siemel Naran wrote:
True, but I want to write a template class as in my previous post, so that I
can write generic code. The generic code will take a reference to a type T,
be it a signed or unsigned integer, and convert it to a reference to an
unsigned integer. So I need to have the type spelled out for the
reinterpret_cast.

I can not understand why you need these, but what you are essentially
trying to define is a cast. The language provided ones are sufficient
for your case, in case you really want to use a cast.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.