首页 / 知识

关于c ++:宏以测试整数类型是带符号还是无符号

2023-04-15 11:30:00

关于c ++:宏以测试整数类型是带符号还是无符号

Macro to test whether an integer type is signed or unsigned

您将如何编写(在C / C ++中)用于测试整数类型(作为参数)是带符号还是无符号的宏?

1
      #define is_this_type_signed (my_type) ...

在C ++中,使用std::numeric_limits::is_signed

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

请参阅http://msdn.microsoft.com/zh-cn/library/85084kd6(VS.80).aspx。


如果您想要的是一个简单的宏,则可以做到这一点:

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


如果您想要一个宏,那么这应该可以解决问题:

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

基本上,将-1强制转换为您的类型,然后查看它是否仍为-1。在C ++中,您不需要宏。只需#include 并且:

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

您的要求并不完全是最好的,但是如果您想一起定义一个定义,则一个选择可能是:

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

但是,无论如何,这都不被认为是好的或可移植的。


目前,typeof尚不合法,但您可以使用模板推导。请参见下面的示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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;
}

此代码将打印

1
  1 0 1

在C ++中,您可以执行以下操作:

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

numeric_limits在标头中??定义。


我实际上只是想知道今天早些时候的同一件事。以下似乎有效:

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

我测试了:

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

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

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

打印:

1
2
int is signed
unsigned int is unsigned

一种更"现代"的方法是使用type_traits

1
2
3
4
5
6
#include <type_traits>
#include <iostream>
int main()
{
    std::cout << ( std::is_signed<int>::value ?"Signed" :"Unsigned") <<std::endl;
}

对于c ++,存在boost :: is_unsigned < T >。我很好奇为什么您需要它,恕我直言,没有什么充分的理由。


在C语言中,您无法编写可用于基本整数类型的迄今未知typedef的宏。

在C ++中,只要您的类型是基本整数类型或基本整数类型的typedef,就可以。这是您在C ++中要做的事情:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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

您可以使用模板功能更好地做到这一点,减少宏观麻烦的业务。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    template <typename T>
        bool IsSignedType()
        {
           // A lot of assumptions on T here
           T instanceAsOne = 1;

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

原谅格式...

我会尝试一下,看看是否可行...


带符号类型用于参数

最新内容

相关内容

猜你喜欢