简体   繁体   English

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

[英]Should I use long long or int64_t for portable code?

I have an open-source codebase that is written in both C and C++.我有一个用 C 和 C++ 编写的开源代码库。 I'm looking for an integer type that is guaranteed to be at least 64 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.我正在寻找一种保证至少64 位宽的整数类型,它可以在大多数 OS X(英特尔,64 位)和带有开源 C 和 C++ 编译器的 Linux 机器上可靠地编译,而无需太多最终用户方面的额外工作。 Windows and 32-bit client support are not important at this time. 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 ).我在 OS X 上做了一些测试,开发者工具附带的最新 GCC 不支持 C+11 模式(因此似乎不能保证long long可用性)。 Clang does not support this, either, though it supports long long if C99 mode is enabled, after a certain version. Clang 也不支持这个,尽管它在某个版本之后支持long long如果启用了 C99 模式。

Is the general suggestion to use int64_t in place of long long , when portability is an important goal?当可移植性是一个重要目标时,一般建议使用int64_t代替long long吗? Using the format specifiers seems painful.使用格式说明符似乎很痛苦。

Can I reliably cast an int64_t to long long (and likewise to the unsigned equivalent with uint64_t ) to use it with existing functions and libraries that take long long as parameters?我能否可靠地将int64_tlong long (同样转换为uint64_tunsigned等效项)以将其与需要long long作为参数的现有函数和库一起使用? (And back again, of course.) (当然,然后再回来。)

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?在这种情况下,如果我发布的代码不需要 GCC 中的 Clang 功能,Clang 会取代 GCC 作为 Linux 上的首选编译器吗? Is that compiler something I can expect, for the most part, when offering source code to end users?在向最终用户提供源代码时,在大多数情况下,我可以期待那个编译器吗?

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++ 代码的开发人员征求一些建议,考虑到上述目标,他们可能对什么可能是更好的长期发展方式有一些建议.

The types long long and unsigned long long are standard C and standard C++ types each with at least 64 bits. long longunsigned long long类型是标准 C 和标准 C++ 类型,每个类型至少有 64 位。 All compilers I'm aware of provide these types, except possibly when in a -pedantic mode but in this case int64_t or uint64_t won't be available with pre-C++ 2011 compilers, either.我知道的所有编译器都提供这些类型,除非可能在-pedantic模式下,但在这种情况下int64_tuint64_t也不适用于 pre-C++ 2011 编译器。 On all of the systems <stdint.h> is available, too.在所有系统上<stdint.h>也是可用的。 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. <stdint.h>的主要目标是为特定数量的位提供最佳匹配。 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_t or uint_least64_t from <stdint.h> or <cstdint> (in case of the latter, the names are defined in namespace std ).如果你需要至少64位,但你也想采取禁食实现这种类型的优势,你会使用int_least64_tuint_least64_t<stdint.h><cstdint>在后一种情况下,该名称是在命名空间std定义)。

Is the general suggestion to use int64_t in place of long long , when portability is an important goal?当可移植性是一个重要目标时,一般建议使用int64_t代替long long吗?

I'd be very surprised if a compiler offered int64_t but not long long .如果编译器提供int64_t但不是long long我会感到非常惊讶。

If long long is present, it must have at least 64 bits, so casting from (u)int64_t to (unsigned) long long is value-preserving.如果long long存在,则它必须至少有 64 位,因此从(u)int64_t(unsigned) long long是保值的。

If you need a type with exactly 64 bits, use (u)int64_t , if you need at least 64 bits, (unsigned) long long is perfectly fine, as would be (u)int_least64_t .如果您需要一个正好为64 位的类型,请使用(u)int64_t ,如果您需要至少64 位, (unsigned) long long非常好, (u)int_least64_t

Use int64_t.使用 int64_t。 int64_t means 64 bits and you'll get 64 bits wherever you go. int64_t 表示 64 位,无论您走到哪里,都会得到 64 位。 long long is actually as implementation dependent as long is. long long 实际上与 long 一样依赖于实现。 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.也就是说,long long 必须大于或等于 long,但这可能因编译器和平台而异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM