botismarius
botismarius

Reputation: 3035

Macro to test whether an integer type is signed or unsigned

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

Upvotes: 19

Views: 8024

Answers (11)

ChrisN
ChrisN

Reputation: 16933

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

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

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

Upvotes: 40

Mikhail
Mikhail

Reputation: 8028

A more "modern" approach is to use type_traits:

#include <type_traits>
#include <iostream>
int main()
{
    std::cout << ( std::is_signed<int>::value ? "Signed" : "Unsigned") <<std::endl;
}

Upvotes: 0

Bronek
Bronek

Reputation: 176

Althout typeof is not legal C++ at the moment, you can use template deduction instead. See sample code below:

#include <iostream>
#include <limits>

template <typename T>
bool is_signed(const T& t)
{
  return std::numeric_limits<T>::is_signed;
}

int main()
{
  std::cout << 
    is_signed(1) << " " << 
    is_signed((unsigned char) 0) << " " << 
    is_signed((signed char) 0) << std::endl;
}

This code will print

  1 0 1

Upvotes: 1

Calliphony
Calliphony

Reputation: 47

If you want a macro then this should do the trick:

#define IS_SIGNED( T ) (((T)-1)<0)

Basically, cast -1 to your type and see if it's still -1. In C++ you don't need a macro. Just #include <limits> and:

bool my_type_is_signed = std::numeric_limits<my_type>::is_signed;

Upvotes: 4

Kevin
Kevin

Reputation: 25927

In C, you can't write a macro that works on as-yet unknown typedef's of fundamental integer types.

In C++, you can as long as your type is a fundamental integer type or a typedef of a fundamental integer type. Here's what you'd do in C++:

template <typename T>
struct is_signed_integer
{
    static const bool value = false;
};

template <>
struct is_signed_integer<int>
{
    static const bool value = true;
};

template <>
struct is_signed_integer<short>
{
    static const bool value = true;
};

template <>
struct is_signed_integer<signed char>
{
    static const bool value = true;
};

template <>
struct is_signed_integer<long>
{
    static const bool value = true;
};

// assuming your C++ compiler supports 'long long'...
template <>
struct is_signed_integer<long long>
{
    static const bool value = true;
};

#define is_this_type_signed(my_type) is_signed_integer<my_type>::value

Upvotes: -1

Greg Rogers
Greg Rogers

Reputation: 36439

In C++ you can do:


bool is_signed = std::numeric_limits<typeof(some_integer_variable)>::is_signed;

numeric_limits is defined in the <limits> header.

Upvotes: 1

Doug T.
Doug T.

Reputation: 65599

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

Upvotes: -1

Frank Szczerba
Frank Szczerba

Reputation: 5060

I was actually just wondering the same thing earlier today. The following seems to work:

#define is_signed(t)    ( ((t)-1) < 0 )

I tested with:

#include <stdio.h>

#define is_signed(t)    ( ((t)-1) < 0 )
#define psigned(t) printf( #t " is %s\n", is_signed(t) ? "signed" : "unsigned" );

int
main(void)
{
    psigned( int );
    psigned( unsigned int );
}

which prints:

int is signed
unsigned int is unsigned

Upvotes: 1

user7116
user7116

Reputation: 64068

Your requirement isn't exactly the best, but if you'd like to hack together a define, one option could be:

#define is_numeric_type_signed(typ) ( (((typ)0 - (typ)1)<(typ)0) && (((typ)0 - (typ)1) < (typ)1) )

However, this isn't considered nice or portable by any means.

Upvotes: 2

Leon Timmermans
Leon Timmermans

Reputation: 30225

For c++, there is boost::is_unsigned<T>. I'm curious why you need it though, there are few good reasons IMHO.

Upvotes: 0

Fabio Ceconello
Fabio Ceconello

Reputation: 16039

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

#define is_type_signed(my_type) (((my_type)-1) < 0)

Upvotes: 28

Related Questions