convert signed int to unsigned int

Discussion in 'C++' started by Siemel Naran, Nov 28, 2004.

  1. Siemel Naran

    Siemel Naran Guest

    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.
     
    Siemel Naran, Nov 28, 2004
    #1
    1. Advertising

  2. 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
     
    Jonathan Mcdougall, Nov 28, 2004
    #2
    1. Advertising

  3. Siemel Naran

    Siemel Naran Guest

    "Jonathan Mcdougall" <> 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.
     
    Siemel Naran, Nov 29, 2004
    #3
  4. 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
     
    Ioannis Vranos, Nov 29, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Freiberger
    Replies:
    3
    Views:
    958
    Bob Hairgrove
    Oct 30, 2004
  2. G Fernandes
    Replies:
    2
    Views:
    496
    Lawrence Kirby
    Feb 16, 2005
  3. Alex
    Replies:
    3
    Views:
    648
    Michael Mair
    Apr 26, 2006
  4. Fore
    Replies:
    29
    Views:
    15,299
    Rashad
    Sep 21, 2008
  5. pozz
    Replies:
    12
    Views:
    749
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page