C / Qt中的精确宽度整数:typedef int qint32真的正确吗?

Exact width integers in C++/Qt: Is typedef int qint32 really correct?

Qt文档说:

typedef qint8

Typedef for signed char. This type is guaranteed to be 8-bit on all platforms supported by Qt.

typedef qint16

Typedef for signed short. This type is guaranteed to be 16-bit on all platforms supported by Qt.

typedef qint32

Typedef for signed int. This type is guaranteed to be 32-bit on all platforms supported by Qt.

typedef qint64

Typedef for long long int (__int64 on Windows). This type is guaranteed to be 64-bit on all platforms supported by Qt.

qglobal.h中定义的类型如下:

1
2
3
4
5
6
7
8
9
10
11
/*
   Size-dependent types (architechture-dependent byte order)
   Make sure to update QMetaType when changing these typedefs
*/

typedef signed char qint8;         /* 8 bit signed */
typedef unsigned char quint8;      /* 8 bit unsigned */
typedef short qint16;              /* 16 bit signed */
typedef unsigned short quint16;    /* 16 bit unsigned */
typedef int qint32;                /* 32 bit signed */
typedef unsigned int quint32;      /* 32 bit unsigned */
//....

但是我想知道,如果不能保证int为32位长,qint32可以总是为32位长。 据我所知,在64位体系结构上,int的长度为(或至少可以是)64位。 [编辑:我错了。请参阅下面的评论。]

如何保证尺寸?他们为什么不使用stdint(在Windows平台上为__intN)?


How can they guarantee the sizes?

他们以他们知道上述定义有效的特定平台为目标。 Qt不在乎其他平台,其他平台也不在乎Qt。因此,它并不总是有效。但是在无法正常工作的地方,Qt。

Why don't they use stdint (and __intN on windows platforms)?

有时候,为自己的项目在<stdint.h>中维护typedef更简单,而不是在有条件的平台上有条件地包括标准标头,而对于没有平台的平台仍保持后备状态(例如VS2005)。


Qt支持许多平台,但重要的是该平台的64位数据模型(或更正确地说,是编译器加平台的数据模型)。??

大多数常见的64位平台使用32位整数,因为它们实现某种形式的LP64数据模型。基于ILP64的数据模型(ILP64,SILP64等)将整数定义为64位。但是我不相信Qt支持任何这些平台。

查看ILP64 Wiki页面以及此PVS-Studio支持页面,其中包含一些良好的技术信息。