stint .h中的c++标准类型大小
C++ standard type sizes in stdint.h
我使用std::uint32_t,出于某种原因,它是一种不同的数据类型,而不是unsigned long,这是我的平台上的unsigned 32 (ARM Cortex M4 K20)…什么好主意吗?
没关系,我猜有时候你想要可变大小的类型:
typedef unsigned short Integer16;
typedef std::uint32_t Integer32;
typedef unsigned int FastInteger16;
typedef unsigned long FastInteger32;
有什么问题吗?
类型std::uint32_t
应该是32位的无符号整数。
但这意味着它可以是unsigned int
或unsigned long
。
这两个值可能都是32位长(取决于实现)。
或者禁止任何方式做定义告诉我如果std::uint32_t不是32位,所以我可以有条件地重新定义基本类型?
std::uint32_t
类型定义为32位。此类型是可选定义的,如果您的平台没有32位无符号整数类型,则不可用。
你想要的代码是:
#if (typeid(std::uint32_t) == typeid(unsigned long))
typedef unsigned long Integer32;
#else
typedef std::uint32_t Integer32;
这是不合法的,但你想说的是,换句话说,"如果uint32_t
意味着unsigned long
,那么Integer32
意味着unsigned long
。否则,它表示uint32_t
的含义。
相当于:
typedef std::uint32_t Integer32;
因为在这两种情况下,您将Integer32
混叠为与uint32_t
表示相同的类型。那就写下来吧
保证uint32_t
在符合标准的实现中是无符号32位类型。既然它不是unsigned long
在你的实现,那么它几乎肯定是unsigned int
。这两种类型,unsigned long
和unsigned int
,是不同的类型,即使它们大小相同。相比之下,uint32_t
是一个类型定义,所以它的类型与它的类型定义是相同的。
回答你的新问题:
typedef unsigned short Integer16;
unsigned short
可能不完全是16位(尽管没有很多实现不是这样)。如果你想要一个精确的16位无符号类型,那么这就是uint16_t
的作用。它是一个可选的类型,所以如果没有一个合适的类型,你的代码将导致一个信息错误,这几乎是你所能做的。
typedef std::uint32_t Integer32;
OK,警告uint32_t
是可选类型。如果你喜欢Integer32
这个名字,那么你可以自由地使用它。
typedef unsigned int FastInteger16;
如果你想要一个至少16位的快速无符号整数,使用std::uint_fast16_t
而不是unsigned int
。
typedef unsigned long FastInteger32;
如果你想要一个至少32位的快速无符号整数,使用std::uint_fast32_t
而不是unsigned long
。
- 编译标准库类型
- 铸造标准::有没有回到原来的类型
- 标准 N3337 5.2.10 第 7 条中的C++"类型"是什么意思?
- 标准库类型的赋值运算符的引用限定符
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 根据C++标准的定义实现"is_similar"类型特征
- C++ 模板类型检查标准::is_same不起作用?
- 标准::原子中的自定义类型
- 标准::时间::d类型的挥发性对象
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- 如何声明一个标准::提升直方图的向量?提升直方图的类型是什么?
- 标准::p空气<自动,自动>返回类型
- 标准::map键类型的严格弱排序概念
- MacOS 上的 LLVM - 标准文件 iosfwd 中未知类型名称'template'
- 标准::enable_if类型检查
- 使用C++11标准的哪些规则来确定({..})中表达式的类型
- 如何为缺少预定义运算符而不扩展命名空间"std"的标准类型定义运算符>> (istream &, ...)?
- POD类型是否完全等同于琐碎的标准布局类型
- 聚合类型是否意味着它也是标准布局
- 将积分类型的数组作为另一个不相关的积分类型的阵列进行访问的安全且符合标准的方法