Alex Rivera | Logout

Macro to test whether an integer type is signed or unsigned

Asked 2008-09-15T17:19:52.340
19

How would you write (in C/C++) a macro which tests if an integer type (given as a parameter) is signed or unsigned?


      #define is_this_type_signed (my_type) ...

c++ c
Edit
Report

2 Answers

28

If what you want is a simple macro, this should do the trick:

#define is_type_signed(my_type) (((my_type)-1) < 0)
answered 2008-09-15T17:27:07.453
-1

You could do this better with a template function, less macro nasty business.

    template <typename T>
        bool IsSignedType()
        {
           // A lot of assumptions on T here
           T instanceAsOne = 1;

           if (-instanceAsOne > 0)
           {
               return true;
           }
           else
           {
               return false;
           }
}

Forgive the formatting...

I would try this out and see if it works...

answered 2008-09-15T17:32:42.027

Your Answer