C++ 为什么没有 int128_t?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29638723/
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 21:13:14  来源:igfitidea点击:

Why isn't there int128_t?

c++cc++-standard-libraryc-standard-library

提问by

A number of compilers provide 128-bit integer types, but none of the ones I've used provide the typedefs int128_t. Why?

许多编译器提供 128 位整数类型,但我使用过的编译器都没有提供 typedefs int128_t。为什么?

As far as I recall, the standard

据我所知,标准

  • Reserves int128_tfor this purpose
  • Encourages implementations that provide such a type to provide the typedef
  • Mandates that such implementations provide an intmax_tof at least 128 bits
  • int128_t为此目的准备金
  • 鼓励提供此类类型的实现提供 typedef
  • 要求此类实现提供intmax_t至少 128 位的

(and, I do not believe I've used an implementation that actually conforms to that last point)

(而且,我不相信我使用的实现实际上符合最后一点)

采纳答案by Keith Thompson

I'll refer to the C standard; I think the C++ standard inherits the rules for <stdint.h>/ <cstdint>from C.

我会参考C标准;我认为 C++ 标准从 C继承了<stdint.h>/的规则<cstdint>

I know that gcc implements 128-bit signed and unsigned integers, with the names __int128and unsigned __int128(__int128is an implementation-defined keyword) on some platforms.

我知道 gcc在某些平台上实现了 128 位有符号和无符号整数,名称__int128unsigned __int128(__int128是实现定义的关键字)。

Even for an implementation that provides a standard 128-bit type, the standard does not requireint128_tor uint128_tto be defined. Quoting section 7.20.1.1 of the N1570draft of the C standard:

即使对于提供标准 128 位类型的实现,该标准也不需要int128_t或被uint128_t定义。引用C标准N1570草案第7.20.1.1节:

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.

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

C permits implementations to defined extended integer typeswhose names are implementation-defined keywords. gcc's __int128and unsigned __int128are very similar to extended integer types as defined by the standard -- but gcc doesn't treat them that way. Instead, it treats them as a language extension.

C 允许实现定义的扩展整数类型,其名称是实现定义的关键字。gcc__int128和gcc与unsigned __int128标准定义的扩展整数类型非常相似——但 gcc 不会那样对待它们。相反,它将它们视为语言扩展。

In particular, if __int128and unsigned __int128were extended integer types, then gcc would be required to define intmax_tand uintmax_tas those types (or as some types at least 128 bits wide). It does not do so; instead, intmax_tand uintmax_tare only 64 bits.

特别是,如果__int128unsigned __int128扩展整数类型,则需要 gcc 将intmax_t和定义uintmax_t为这些类型(或定义为至少 128 位宽的某些类型)。它没有这样做;相反,intmax_t并且uintmax_t只有 64 位。

This is, in my opinion, unfortunate, but I don't believe it makes gcc non-conforming. No portable program can depend on the existence of __int128, or on any integer type wider than 64 bits.

在我看来,这是不幸的,但我不认为它会使 gcc 不符合标准。任何可移植程序都不能依赖于 的存在,也不能依赖于__int128任何大于 64 位的整数类型。