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

关于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;
           }
}

原谅格式...

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


推荐阅读

    linux过滤命令参数?

    linux过滤命令参数?,工具,数据,标准,地址,命令,设备,系统,信息,指令,文件,如

    linux下ls命令参数?

    linux下ls命令参数?,时间,系统,数据,命令,文件,信息,标准,密码,工作,名字,lin

    linux参数设置命令?

    linux参数设置命令?,网络,系统,地址,工作,信息,管理,服务,名称,状态,命令,在l

    linux命令du参数?

    linux命令du参数?,工作,信息,系统,情况,单位,地址,命令,报告,时间,数据,在LIN

    linux命令参数r与k?

    linux命令参数r与k?,工作,地址,系统,信息,命令,目录,管理,标准,基础,控制台,3

    linuxrm命令参数?

    linuxrm命令参数?,系统,命令,文件,目录,环境,档案,文件夹,终端,参数,子目录,l

    查看linux类型命令?

    查看linux类型命令?,系统,信息,命令,状态,数据,数字,情况,地址,类型,文件,lin

    linux删除类型命令?

    linux删除类型命令?,系统,档案,命令,文件,名称,环境,数据,不了,目录,文件夹,

    linux命令行定义参数?

    linux命令行定义参数?,系统,信息,名称,实时,命令,百分比,工作,周期,选项,参

    linux命令查询参数?

    linux命令查询参数?,网络,信息,设备,系统,服务,状态,情况,工作,地址,命令,Lin

    linux网络参数命令?

    linux网络参数命令?,网络,地址,系统,工作,信息,工具,情况,服务,状态,命令,Lin

    linux的cd命令参数?

    linux的cd命令参数?,工作,地址,系统,命令,一致,目录,信息,基础,名称,管理,lin

    查看linux库类型命令?

    查看linux库类型命令?,系统,工作,信息,状态,电脑,命令,工具,代码,地址,发行,

    linux下cd命令参数?

    linux下cd命令参数?,工作,命令,系统,一致,名称,目录,用户,缩写,意思,参数,lin

    linux网卡类型命令?

    linux网卡类型命令?,网络,系统,地址,信息,设备,状态,服务,名称,名字,网卡,如

    linux各种命令的参数?

    linux各种命令的参数?,网络,信息,工作,地址,系统,情况,服务,命令,软件,数据,l

    linux命令行参数长度?

    linux命令行参数长度?,系统,信息,实时,工具,工作,名称,环境,百分比,标准,情

    linux添加命令参数?

    linux添加命令参数?,系统,管理,工作,基础,命令,情况,网络,工具,代码,环境,别

    linux命令参数长度?

    linux命令参数长度?,工作,系统,信息,基础,网络,环境,情况,管理,长度,命令,Lin

    linux安装参数命令?

    linux安装参数命令?,工作,系统,软件,地址,命令,中心,在线,地方,位置,信息,Lin