C++ unsigned long long 与 uint64_t 冲突?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32198368/
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
unsigned long long conflict with uint64_t?
提问by thundium
We use template specialization for some type parameter like
我们对某些类型参数使用模板特化,例如
class my_template_class<uint64_t M>: public my_template_class_base<uint64_t> {
....
}
class my_template_class<unsigned long long,M>: public my_template_class_base<unsigned long long> {
....
}
This is working perfectly with 64-bit compilation with gcc. While when we try the 32 bit mode, it reports "previous definition" for above two classes.
这与 gcc 的 64 位编译完美配合。而当我们尝试 32 位模式时,它会报告上述两个类的“先前定义”。
So unsigned long long
is the same as uint64_t
in the 32-bit compilation but not in 64-bit compliation?
那么在32位编译中unsigned long long
是否相同uint64_t
但在64位编译中不同?
The compilation difference is the CXX
flag -m32
and -m64
编译区别在于CXX
标志-m32
和-m64
回答by Keith Thompson
So
unsigned long long
is the same asuint64_t
in the 32-bit compilation but not in 64-bit compilation?
那么
unsigned long long
是否与uint64_t
32 位编译中相同但在 64 位编译中不同?
Yes.
是的。
In 32-bit mode, most likely long
is 32 bits and long long
is 64 bits. In 64-bit mode, both are probably 64 bits.
在 32 位模式下,最有可能long
是 32 位和long long
64 位。在 64 位模式下,两者都可能是 64 位。
In 32-bit mode, the compiler (more precisely the <stdint.h>
header) defines uint64_t
as unsigned long long
, because unsigned long
isn't wide enough.
在 32 位模式下,编译器(更准确地说是<stdint.h>
头文件)定义uint64_t
为unsigned long long
,因为unsigned long
不够宽。
In 64-bit mode, it defines uint64_t
as unsigned long
.
在 64 位模式下,它定义uint64_t
为unsigned long
.
It couldhave defined it as unsigned long long
in both modes. The choice is arbitrary; all that's required is that it has to be a 64-bit type.
它可以将其定义为unsigned long long
两种模式。选择是任意的;所需要的只是它必须是 64 位类型。
In general, each of the integer types defined in <stdint.h>
is a typedef for some predefined type with the appropriate characteristics. You can't assume that any of them are distinct from the predefined types.
通常,定义的每个整数类型<stdint.h>
都是具有适当特征的某些预定义类型的 typedef。您不能假设它们中的任何一个都不同于预定义的类型。
回答by Paolo M
This is from stdint.h
for GCC 4.8:
这是来自stdint.h
GCC 4.8:
#if __WORDSIZE == 64
typedef unsigned long int uint64_t;
#else
__extension__
typedef unsigned long long int uint64_t;
#endif
So:
所以:
So unsigned long long is the same as uint64_t in the 32bit compliation but not in 64 bit compliation?
所以 unsigned long long 和 uint64_t 在 32 位编译中是一样的,但在 64 位编译中不一样?
Yes.
是的。