c++

C++ – Macro to test whether an integer type is signed or unsigned

c++

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

Best Solution

In C++, use std::numeric_limits<type>::is_signed.

#include <limits>std::numeric_limits<int>::is_signed  - returns truestd::numeric_limits<unsigned int>::is_signed  - returns false

See https://en.cppreference.com/w/cpp/types/numeric_limits/is_signed.