C ++ cross-platform way to define a 64-bit unsigned integer

I am currently working on a project that makes extensive use of 64-bit unsigned integers in many parts of the code. So far we are compiling only gcc 4.6, but now we are transferring some code to windows. It is very important that these unsigned ints have a width of 64 bits. It has been suggested that we could use a long time, but this is not very good, if for a long time there will be more than 64 bits, we really want to get a guarantee that it will be 64 bits, and something like static_assert(sizeof(long long) == 8) code smells a bit.

What is the best way to define something like uint64 that will be compiled for both gcc and msvc, without having to use a different code syntax everywhere?

+4
source share
4 answers

How about turning on cstdint and using std::uint64_t ?

+14
source

You can use boost:

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

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

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

+3
source

This is what I do:

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

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

Look this

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

0
source

Source: https://habr.com/ru/post/1396410/


All Articles