convert signed int to unsigned int

S

Siemel Naran

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.
 
J

Jonathan Mcdougall

Siemel said:
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
 
S

Siemel Naran

Jonathan Mcdougall said:
Siemel Naran wrote:

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.
 
I

Ioannis Vranos

Siemel said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top