c ++跨平台方式定义64位无符号整数

时间:2021-12-08 03:07:12

Right now I'm working on a project that extensively uses 64bit unsigned integers in many parts of the code. So far we have only been compiling with gcc 4.6 but we are now porting some code to windows. It's crucial that these unsigned ints are 64bits wide. It has been suggested that we could use long long but it's not good if long long happens to be bigger than 64bits, we actually want to have a guarantee that it will be 64 bits and writing something like static_assert(sizeof(long long) == 8) seems to be a bit of a code smell.

现在我正在开发一个在代码的许多部分中广泛使用64位无符号整数的项目。到目前为止,我们只使用gcc 4.6编译,但我们现在将一些代码移植到Windows。这些无符号整数是64位宽是至关重要的。有人建议我们可以使用long long但是如果long long大于64位并不好,我们实际上想要保证它是64位并写一些类似static_assert(sizeof(long long)== 8)似乎有点代码味道。

What is the best way to define something like uint64 that will compile across both gcc and msvc without needing to have different code syntax used everywhere?

定义类似uint64的最佳方法是什么,它将在gcc和msvc之间进行编译,而无需在任何地方使用不同的代码语法?

4 个解决方案

#1


14  

What about including cstdint and using std::uint64_t?

包括cstdint和使用std :: uint64_t怎么样?

#2


3  

You can use boost:

你可以使用boost:

The typedef int#_t, with # replaced by the width, designates a signed integer type of exactly # bits; for example int8_t denotes an 8-bit signed integer type. Similarly, the typedef uint#_t designates an unsigned integer type of exactly # bits.

typedef int#_t,其中#替换为宽度,指定一个完全#位的有符号整数类型;例如,int8_t表示8位有符号整数类型。类似地,typedef uint#_t指定一个完全#位的无符号整数类型。

See: http://www.boost.org/doc/libs/1_48_0/libs/integer/doc/html/boost_integer/cstdint.html

Especially this header: http://www.boost.org/doc/libs/1_48_0/boost/cstdint.hpp

特别是这个标题:http://www.boost.org/doc/libs/1_48_0/boost/cstdint.hpp

#3


1  

This is what I do:

这就是我做的:

#ifndef u64
#ifdef WIN32
typedef unsigned __int64   u64;
#else // !WIN32
typedef unsigned long long u64;
#endif
#endif

#4


0  

On Windows you can use __int64, unsigned __int64, or typedefs: UINT64, INT64 etc.

在Windows上,您可以使用__int64,unsigned __int64或typedef:UINT64,INT64等。

Look at this

看这个

But yes, if code portability is concern, use standard typedefs, as suggested by others.

但是,如果关注代码可移植性,请使用标准typedef,如其他人所建议的那样。

#1


14  

What about including cstdint and using std::uint64_t?

包括cstdint和使用std :: uint64_t怎么样?

#2


3  

You can use boost:

你可以使用boost:

The typedef int#_t, with # replaced by the width, designates a signed integer type of exactly # bits; for example int8_t denotes an 8-bit signed integer type. Similarly, the typedef uint#_t designates an unsigned integer type of exactly # bits.

typedef int#_t,其中#替换为宽度,指定一个完全#位的有符号整数类型;例如,int8_t表示8位有符号整数类型。类似地,typedef uint#_t指定一个完全#位的无符号整数类型。

See: http://www.boost.org/doc/libs/1_48_0/libs/integer/doc/html/boost_integer/cstdint.html

Especially this header: http://www.boost.org/doc/libs/1_48_0/boost/cstdint.hpp

特别是这个标题:http://www.boost.org/doc/libs/1_48_0/boost/cstdint.hpp

#3


1  

This is what I do:

这就是我做的:

#ifndef u64
#ifdef WIN32
typedef unsigned __int64   u64;
#else // !WIN32
typedef unsigned long long u64;
#endif
#endif

#4


0  

On Windows you can use __int64, unsigned __int64, or typedefs: UINT64, INT64 etc.

在Windows上,您可以使用__int64,unsigned __int64或typedef:UINT64,INT64等。

Look at this

看这个

But yes, if code portability is concern, use standard typedefs, as suggested by others.

但是,如果关注代码可移植性,请使用标准typedef,如其他人所建议的那样。