为什么在G ++的std ::还会将intmax_t不是__int128_t? [英] Why in g++ std::intmax_t is not a __int128_t?

查看:469
本文介绍了为什么在G ++的std ::还会将intmax_t不是__int128_t?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是pretty简单:的std ::还会将intmax_t 被定义为最大宽度整数类型 CP preference ,为什么它不符合<$ C> $ C> __ int128_t 在GCC?

My question is pretty simple: as std::intmax_t is defined as the maximum width integer type according to cppreference, why it does not correspond to __int128_t in GCC?

推荐答案

我相信这是违反了C和C ++标准 - 要么,或GCC不考虑 __ int128_t 是一个整数类型。

I believe this is a violation of the C and C++ standards -- either that, or gcc doesn't consider __int128_t to be an integer type.

C标准(1999年和2011版本)不需要还会将intmax_t 是标准类型之一;它必须是有符号整数类型能够重新presenting任何符号整数类型的任何价值。特别是,它可以是一个的扩展整数类型的 - 如果有128位扩展整数类型,那么还会将intmax_t 必须至少128位宽。

The C standard (both the 1999 and 2011 editions) doesn't require intmax_t to be one of the standard types; it's required to be "a signed integer type capable of representing any value of any signed integer type". In particular, it can be an extended integer type -- and if there is a 128-bit extended integer type, then intmax_t must be at least 128 bits wide.

C标准甚至建议用实现定义的关键字,对任何使用保留的标识符的形式作为扩展整型的名字 - 比如 __ int128_t

The C standard even suggests using implementation-defined keywords that "have the form of an identifier reserved for any use" as the names of extended integer types -- such as __int128_t.

2011年的C ++标准采用C99的扩展整型功能,并按照1999年C标准为的还会将intmax_t 的定义和&LT; stdint。 H&GT;

The 2011 C++ standard adopts C99's extended integer types feature, and defers to the 1999 C standard for the definition of intmax_t and <stdint.h>.

所以,如果 __ int128_t 整型的由标准(它一定的定义所指可以的是),以及是,顾名思义,宽128位,然后将intmax_t 必须至少为128位宽。

So if __int128_t is an integer type within the meaning defined by the standard (which it certainly can be), and is, as the name implies, 128 bits wide, then intmax_t must be at least 128 bits wide.

由于斯蒂芬佳能的答案,改变还会将intmax_t 确实需要一些工作。 C和C ++标准不承认,作为一个理由定义还会将intmax_t 不正确。

As Stephen Canon's answer, changing intmax_t does require some work. The C and C++ standards do not recognize that as a justification for defining intmax_t incorrectly.

当然,这一切也同样适用于 uintmax_t型

Of course all of this applies equally to uintmax_t.

#include <stdio.h>
#include <stdint.h>

int main(void) {
    __uint128_t huge = UINTMAX_MAX;
    huge ++;
    if (huge > UINTMAX_MAX) {
        puts("This should not happen");
    }
}

在我的系统(Linux的x86_64的,GCC 4.7.2),上面的程序打印:

On my system (Linux x86_64, gcc 4.7.2), the above program prints:

This should not happen

如果GCC符合标准,那么这应该是可能的,只有 __ int128_t 不是整数类型 - 但引述的gcc 4.8.2手动(强调):

If gcc conforms to the standard, then that should be possible only if __int128_t is not an integer type -- but quoting the gcc 4.8.2 manual (emphasis added):

作为一个扩展在整数标量型 __ int128 的支持
  其中有一个整数模式足够宽的目标,以容纳128位。
  简单的写 __ int128 已签署128位的整数或无符号__int128
  为一个无符号的128位的整数。有一个在海湾合作委员会不​​支持为前pressing整数
  恒类型的 __ int128 与目标
  长长整数宽小于128位。

As an extension the integer scalar type __int128 is supported for targets which have an integer mode wide enough to hold 128 bits. Simply write __int128 for a signed 128-bit integer, or unsigned __int128 for an unsigned 128-bit integer. There is no support in GCC for expressing an integer constant of type __int128 for targets with long long integer less than 128 bits wide.

我想人们可以争辩说,作为一个扩展短语让GCC打爆这里,根据第4标准的第6款证明 __存在int128_t

I suppose one could argue that the "as an extension" phrase lets gcc off the hook here, justifying the existence of __int128_t under section 4 paragraph 6 of the standard:

一个符合标准的实现可以有扩展(含追加
  库函数),只要它们不改变的任何行为
  严格符合程序。

A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any strictly conforming program.

而不是根据第6.2.6条第4款:

rather than under section 6.2.6 paragraph 4:

有也可能是实现定义的扩展符号整数
  类型

There may also be implementation-defined extended signed integer types.

(我个人认为让还会将intmax_t 至少宽 __ int128_t ,如果存在的话,会更标准符合的意图的标准相一致,即使它是(几乎没有)可能争辩说,它没有违反的的。)

(I personally think that making intmax_t at least as wide as __int128_t, if it exists, would be more in keeping with the intent of the standard, even if it's (barely) possible to argue that it doesn't violate the letter of the standard.)

这篇关于为什么在G ++的std ::还会将intmax_t不是__int128_t?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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