Thread: How to tell a type is signed or unsigned?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    How to tell a type is signed or unsigned?

    Code:
    typedef int mytype;
    //typedef unsigned int mytype;
    
    bool isSigned(mytype x)
    {
    	//how to tell if mytype is a signed type or not?
    }
    Suppose I have mytype that could be defined as either signed (int, short, long) or unsigned. And I don't know what the type is passed in.
    How do I tell if mytype is a signed type or not?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I guess you can use the knowledge that the minimum value an unsigned type can hold is 0. In addition the compiler knows what mytype is typedefed as, therefore:
    Code:
    #include <limits>
    bool isSigned(mytype n)
    {
        return std::numeric_limits<mytype>::min() != 0;
    }
    (Although, it seems somewhat strange to determine this at run-time.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Or:
    Code:
    return std::numeric_limits<mytype>::is_signed;

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by anon View Post
    (Although, it seems somewhat strange to determine this at run-time.)
    It does seem strange to use numeric_limits<T>::is_signed() for something other than a template, which is the only time I've ever used it.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    sometype test = 0;
    if(test - 1 > test)
    {
        /* The type is unsigned */
    }
    It looks like it happens at runtime, but the compiler will probably optimize it out.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    That's undefined behavior.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    How is it undefined behavior?

    - test is a number set to zero.
    - Subtracting one from test produces a value.
    - If the value is larger than one (or underflows), then test is unsigned.

    But as brewbuck pointed out, without being denoted as volatile or something, the block of code will most likely be removed by your optimized.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I've previously used:
    Code:
    if ((sometype)-1 < (sometype)0)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Code:
    template<typename T>
    bool isSigned(const T)
    {
    	return std::numeric_limits<T>::is_signed;
    }

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by master5001 View Post
    How is it undefined behavior?
    underflows are undefined behavior.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Location
    Brisbane, Australia
    Posts
    1
    Quote Originally Posted by iMalc View Post
    I've previously used:
    Code:
    if ((sometype)-1 < (sometype)0)
    this is probably the most simple (for both you and the compiler) to use as operations between constants are always optimised.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by robwhit View Post
    underflows are undefined behavior.
    An integer must be negative, zero, or positive. Regardless of any undefined corners, this logical fact remains. It is clear that an unsigned type cannot hold a negative value. Thus the result, defined or not, must be either zero or some positive value.

    So, change it slightly just to catch the bizarre corner case:

    Code:
    if(test - 1 >= test)
    Whether the actual behavior of the underflow is defined or not doesn't enter into it. The only way this method could not work is if an unsigned variable could take on a negative value, which is a contradiction.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    So you're just ignoring the fact that its ub? once it's ub, it doesn't matter what the laws of mathematics are. the program could do whatever it wanted.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by robwhit View Post
    So you're just ignoring the fact that its ub? once it's ub, it doesn't matter what the laws of mathematics are. the program could do whatever it wanted.
    Yes, I'm ignoring it.

    EDIT: Here's why. Either the operation will result in a result, or it won't. In the first case, the result could not possibly be negative if the type is unsigned. In the second case, the program must have terminated abnormally (otherwise how could you not have gotten a result), which means the platform is ridiculous. So yes, I'm deliberately stepping into undefined territory, and I feel quite safe in doing so.
    Last edited by brewbuck; 05-06-2008 at 11:13 PM.

  15. #15
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Does it still work when you're not on a platform using 2-complement??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. Cross platform portability, about data types...
    By gaah in forum C++ Programming
    Replies: 9
    Last Post: 01-21-2005, 10:32 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM