C++ uint128_t 未命名类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34588650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:24:06  来源:igfitidea点击:

uint128_t does not name a type

c++ctypesinteger

提问by Zimano

I am porting some code from C to C++. During the conversion I encountered:

我正在将一些代码从 C 移植到 C++。在转换过程中我遇到了:

uint128_tdoes not name a type

uint128_t不命名类型

My compiler: gcc version 5.2.1
My operating system: Ubuntu 15.1

我的编译器:gcc 版本 5.2.1
我的操作系统:Ubuntu 15.1

This compiled fine as C and I thought it would be resolved by including stdint.hbut it has not. So far I have not tried anything else since there doesn't seem to be a lot of information on this error (example). uint128_tis used throughout this entire program and is essential for the build, therefore I can not remove it, and I'm not sure about using a different integer type.

这作为 C 编译得很好,我认为它可以通过包含来解决,stdint.h但它没有。到目前为止,我还没有尝试过其他任何东西,因为似乎没有很多关于此错误的信息(示例)。uint128_t在整个程序中使用并且对于构建至关重要,因此我无法删除它,并且我不确定是否使用不同的整数类型。

Below is an example of where and how it is used.

下面是它的使用地点和方式的示例。

union {
    uint16_t  u16;
    uint32_t  u32;
    uint128_t u128;
} value;

Would it be okay to define a uint128_tor should I look at my compiler?

可以定义一个uint128_t还是我应该看看我的编译器?

回答by Andrea Corbellini

GCC has builtin support for the types __int128, unsigned __int128, __int128_tand __uint128_t(the last two are undocumented). Use them to define your own types:

GCC有类型内置支持__int128unsigned __int128__int128_t__uint128_t(最后两个无证)。使用它们来定义您自己的类型:

typedef __int128 int128_t;
typedef unsigned __int128 uint128_t;


Alternatively, you can use __mode__(TI):

或者,您可以使用__mode__(TI)

typedef int int128_t __attribute__((mode(TI)));
typedef unsigned int uint128_t __attribute__((mode(TI)));

Quoting the documentation:

引用文档

TImode

“Tetra Integer” (?) mode represents a sixteen-byte integer.

TImode

“Tetra Integer” (?) 模式表示一个 16 字节的整数。

Sixteen byte = 16 * CHAR_BIT >= 128.

十六字节 = 16 * CHAR_BIT >= 128。

回答by Sourav Ghosh

I thought this would be resolved by including stdint.hbut it has not.

我认为这可以通过包含来解决,stdint.h但事实并非如此。

Well, it may not.

嗯,可能不是。

First to check the C++ header, cstdint, from C++14, chapter § 18.4.1,

首先检查 C++ 头文件,cstdint来自 C++14,第 18.4.1 章,

namespace std {.....

typedef unsigned integer type uint8_t; // optional
typedef unsigned integer type uint16_t; // optional
typedef unsigned integer type uint32_t; // optional
typedef unsigned integer type uint64_t; // optional
.....
namespace std {.....

typedef unsigned integer type uint8_t; // optional
typedef unsigned integer type uint16_t; // optional
typedef unsigned integer type uint32_t; // optional
typedef unsigned integer type uint64_t; // optional
.....

and,

和,

The header defines all functions, types, and macros the same as 7.18 in the C standard. [..]

头文件定义了与 C 标准中的 7.18 相同的所有函数、类型和宏。[..]

Then quote the C11standard, chapter §7.20.1.1 (emphasis mine)

然后引用C11标准,第 7.20.1.1 章(强调我的

  1. The typedef name uintN_tdesignates an unsigned integer type with width Nand no padding bits. Thus, uint24_tdenotes such an unsigned integer type with a width of exactly 24bits.

  2. These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two's complement representation, it shall define the corresponding typedef names.

  1. typedef 名称uintN_t指定具有宽度N和无填充位的无符号整数类型。因此,uint24_t表示具有精确24位宽度的这种无符号整数类型。

  2. 这些类型是可选的。但是,如果实现提供宽度为 8、16、32 或 64 位、无填充位且(对于有符号类型)具有二进制补码表示的整数类型,则应定义相应的 typedef 名称。

So, here we notice two things.

所以,在这里我们注意到两件事。

  1. An implementation is not mandated to provide support for the fixed-width ints.

  2. Standard limits the width upto 64, as we see it. having a width more than that is once again not mandated in the standard. You need to check the documentation of the environment in use.

  1. 不强制要求实现为固定宽度的ints提供支持。

  2. 64正如我们所见,标准将宽度限制为 。标准中再次没有强制要求宽度大于该宽度。您需要检查所使用环境的文档。

回答by pqnet

As pointed out by other answer, C++ standard does not require 128 bit integer to be available, nor to be typedefed as uint128_teven if present. If your compiler/architecture does not support 128 bit integers and you need them, you could use boost to emulate them:

正如其他答案所指出的那样,C++ 标准不需要 128 位整数可用,也不需要typedefed 为uint128_t即使存在。如果您的编译器/架构不支持 128 位整数而您需要它们,则可以使用 boost 来模拟它们:

http://www.boost.org/doc/libs/1_58_0/libs/multiprecision/doc/html/boost_multiprecision/tut/ints/cpp_int.html

http://www.boost.org/doc/libs/1_58_0/libs/multiprecision/doc/html/boost_multiprecision/tut/ints/cpp_int.html

I think that the boost library will automatically use the native type if available

我认为如果可用,boost 库会自动使用本机类型