C++ U32类型头文件

3

我想使用U32类型,但是我找不到定义它的头文件。有人知道吗?

3个回答

15

没有名为U32的标准类型,但是如果您#include <cstdint>(对于C语言是stdint.h),则可以使用std::uint32_t1,一个32位无符号整数,这很可能是您想要的。

这里是cstdint头文件中类型的列表

namespace std {
    int8_t
    int16_t
    int32_t
    int64_t
    int_fast8_t
    int_fast16_t
    int_fast32_t
    int_fast64_t
    int_least8_t
    int_least16_t
    int_least32_t
    int_least64_t
    intmax_t
    intptr_t
    uint8_t
    uint16_t
    uint32_t
    uint64_t
    uint_fast8_t
    uint_fast16_t
    uint_fast32_t
    uint_fast64_t
    uint_least8_t
    uint_least16_t
    uint_least32_t
    uint_least64_t
    uintmax_t
    uintptr_t
}

1 感谢 Martinho Fernandes 指出这些类型在命名空间 std 中。


在《游戏引擎架构》中有一个使用U32的代码片段,这就是我问的原因... - Tiago Costa
@Tiago:你知道C语言有typedef关键字可以让你创建类型别名吗? - Kerrek SB
1
请注意,<cstdint>中的类型位于命名空间std中。 - R. Martinho Fernandes
我喜欢u32/u64的风格... 有没有现成的头文件定义这些类型?非常感谢!!! - yushang

2

U32是unsigned int的惯用缩写。它通常的定义是:

typedef unsigned int U32;

但使用它的主要原因是能够手动控制实际定义。希望这有所帮助。


5
unsigned int 不能保证是32位,因此这样的typedef定义非常糟糕。 - ChrisWue
在那里抛出一个 static_assert :) - paulm

2

虽然cstdint可能是一个解决方案,但它并不通用 - 2010年之前的MSVC版本没有提供这个库。如果要为多个平台编写代码,则需要自己提供标准类型,如果检测到MSVC < 2010则需要提供。或者,如果使用boost,则可以使用boost库


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接