C ++ / Qt中的精确宽度整数:typedef int qint32真的正确吗? [英] Exact width integers in C++/Qt: Is typedef int qint32 really correct?

查看:401
本文介绍了C ++ / Qt中的精确宽度整数:typedef int qint32真的正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Qt文档说:


typedef qint8

Typedef用于签名的字符。在Qt支持的所有平台上,保证该类型均为8位。

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

typedef qint16

Typedef表示带符号的缩写。在Qt支持的所有平台上,保证该类型均为16位。

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

typedef qint32

Typedef用于带符号的int。在Qt支持的所有平台上,保证该类型均为32位。

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

typedef qint64

Typedef表示long long int(在Windows上为__int64)。在Qt支持的所有平台上,保证该类型均为64位。

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

这些类型在中定义qglobal.h 如下:

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

但是我想知道(例如) qint32 如何在不能保证 int 为32位长的情况下总是32位长。 据我所知,在64位体系结构上, int s是(或至少可以是)64位长。 [编辑:我当时错误。

But I'm wondering how (for example) qint32 can always be 32 bits long when there is no guarantee that int is 32 bits long. As far as I know, on 64-bit architectures ints are (or at least can be) 64 bits long.

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

How can they guarantee the sizes? Why don't they use stdint (and __intN on windows platforms)?

推荐答案


他们如何保证大小?

How can they guarantee the sizes?

他们针对特定平台,他们知道以上内容定义起作用。 Qt不在乎其他平台,其他平台关心Qt。因此,它并不总是有效。

They target specific platforms that they know that the above definitions work. Qt doesn't care for other platforms and other platforms don't care about Qt. So, it won't always work. But where it won't work neither will Qt.


为什么他们不使用stdint(在Windows平台上为__intN)?

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

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

Sometimes it's simpler to maintain the typedefs in <stdint.h> for your own project rather than conditionally including the standard header on platforms that have it and still maintaining a fallback for platforms that don't (e.g. VS2005).

这篇关于C ++ / Qt中的精确宽度整数:typedef int qint32真的正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆