C++ 对于可移植代码,我应该使用 long long 还是 int64_t?

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

Should I use long long or int64_t for portable code?

c++ctypesint64long-long

提问by Alex Reynolds

I have an open-source codebase that is written in both C and C++. I'm looking for an integer type that is guaranteed to be at least64 bits wide, which can be reliably compiled on most OS X (Intel, 64-bit) and Linux boxes with open-source C and C++ compilers, without too much extra work on the end user's part. Windows and 32-bit client support are not important at this time.

我有一个用 C 和 C++ 编写的开源代码库。我正在寻找一种保证至少64 位宽的整数类型,它可以在大多数 OS X(英特尔,64 位)和带有开源 C 和 C++ 编译器的 Linux 机器上可靠地编译,而无需太多最终用户方面的额外工作。Windows 和 32 位客户端支持此时并不重要。

I did some testing on OS X, and the latest GCC that ships with the developer tools does not support C+11 mode (and therefore does not seem to guarantee availability of long long). Clang does not support this, either, though it supports long longif C99 mode is enabled, after a certain version.

我在 OS X 上做了一些测试,开发者工具附带的最新 GCC 不支持 C+11 模式(因此似乎不能保证 的可用性long long)。Clang 也不支持这一点,尽管它支持long long在某个版本之后启用 C99 模式。

Is the general suggestion to use int64_tin place of long long, when portability is an important goal? Using the format specifiers seems painful.

当便携性是一个重要目标时,一般建议使用int64_t代替long long吗?使用格式说明符似乎很痛苦。

Can I reliably cast an int64_tto long long(and likewise to the unsignedequivalent with uint64_t) to use it with existing functions and libraries that take long longas parameters? (And back again, of course.)

我是否可以可靠地强制转换int64_tto long long(以及unsigned等效的 with uint64_t)以将其与long long作为参数的现有函数和库一起使用?(当然,然后再回来。)

In that frame of mind, if I ship code that requires Clang functionality not in GCC, is Clang going to replace GCC as the compiler of choice on Linux? Is that compiler something I can expect, for the most part, when offering source code to end users?

在这种情况下,如果我发布的代码不需要 GCC 中的 Clang 功能,Clang 会取代 GCC 作为 Linux 上的首选编译器吗?在向最终用户提供源代码时,在大多数情况下,我可以期待那个编译器吗?

Basically, I'd like to ask for some advice from other developers who have used both types for portable C and C++ code, who might have some suggestions on what might be the better long-term way to go, given the above goal in mind.

基本上,我想向其他使用这两种类型的可移植 C 和 C++ 代码的开发人员寻求一些建议,考虑到上述目标,他们可能对什么可能是更好的长期方法有一些建议.

采纳答案by Dietmar Kühl

The types long longand unsigned long longare standard C and standard C++ types each with at least 64 bits. All compilers I'm aware of provide these types, except possibly when in a -pedanticmode but in this case int64_tor uint64_twon't be available with pre-C++ 2011 compilers, either. On all of the systems <stdint.h>is available, too. That is, as far as I can tell it doesn't matter much how you spell the type. The main goal of <stdint.h>is to provide the best match for a specific number of bits. If you need at least 64 bit but you also want to take advantage of the fasted implementation of such a type, you'd use int_least64_tor uint_least64_tfrom <stdint.h>or <cstdint>(in case of the latter, the names are defined in namespace std).

类型long longunsigned long long是标准 C 和标准 C++ 类型,每个类型至少有 64 位。我知道的所有编译器都提供这些类型,除非可能在-pedantic模式下但在这种情况下int64_t或在uint64_tC++ 2011 之前的编译器中不可用。在所有系统上<stdint.h>也可用。也就是说,据我所知,您如何拼写类型并不重要。的主要目标<stdint.h>是为特定数量的位提供最佳匹配。如果您需要至少 64 位,但您还想利用此类类型的快速实现,则可以使用int_least64_tor uint_least64_tfrom <stdint.h>or <cstdint>(在后者的情况下,名称在 namespace 中定义std)。

回答by Daniel Fischer

Is the general suggestion to use int64_tin place of long long, when portability is an important goal?

当便携性是一个重要目标时,一般建议使用int64_t代替long long吗?

I'd be very surprised if a compiler offered int64_tbut not long long.

如果编译器提供int64_t但不提供long long.

If long longis present, it must have at least 64 bits, so casting from (u)int64_tto (unsigned) long longis value-preserving.

如果long long存在,它必须至少有 64 位,所以从(u)int64_tto 转换(unsigned) long long是保值的。

If you need a type with exactly64 bits, use (u)int64_t, if you need at least64 bits, (unsigned) long longis perfectly fine, as would be (u)int_least64_t.

如果您需要一个恰好为64 位的类型,则使用(u)int64_t,如果您需要至少64 位,(unsigned) long long则完全可以,就像(u)int_least64_t.

回答by TJ Bandrowsky

Use int64_t. int64_t means 64 bits and you'll get 64 bits wherever you go. long long is actually as implementation dependent as long is. That is, a long long has to be bigger than or equal to a long, but that could be different depending on the compiler and platform.

使用 int64_t。int64_t 表示 64 位,无论您走到哪里,都会得到 64 位。long long 实际上与 long 一样依赖于实现。也就是说,long long 必须大于或等于 long,但这可能因编译器和平台而异。