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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:06:38  来源:igfitidea点击:

unsigned long long conflict with uint64_t?

c++

提问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 longis the same as uint64_tin the 32-bit compilation but not in 64-bit compliation?

那么在32位编译中unsigned long long是否相同uint64_t但在64位编译中不同?

The compilation difference is the CXXflag -m32and -m64

编译区别在于CXX标志-m32-m64

回答by Keith Thompson

So unsigned long longis the same as uint64_tin the 32-bit compilation but not in 64-bit compilation?

那么unsigned long long是否与uint64_t32 位编译中相同但在 64 位编译中不同?

Yes.

是的。

In 32-bit mode, most likely longis 32 bits and long longis 64 bits. In 64-bit mode, both are probably 64 bits.

在 32 位模式下,最有可能long是 32 位和long long64 位。在 64 位模式下,两者都可能是 64 位。

In 32-bit mode, the compiler (more precisely the <stdint.h>header) defines uint64_tas unsigned long long, because unsigned longisn't wide enough.

在 32 位模式下,编译器(更准确地说是<stdint.h>头文件)定义uint64_tunsigned long long,因为unsigned long不够宽。

In 64-bit mode, it defines uint64_tas unsigned long.

在 64 位模式下,它定义uint64_tunsigned long.

It couldhave defined it as unsigned long longin 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.hfor GCC 4.8:

这是来自stdint.hGCC 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.

是的。