4
votes

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.

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?

4
would an ifdef and a typedef together do the trick? - David Feurle
@DavidFeurle, it might be sufficient, the purpose of asking this question was to get a feel for what the best practices are here. - shuttle87

4 Answers

14
votes

What about including cstdint and using std::uint64_t?

3
votes

You can use 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.

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

1
votes

This is what I do:

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

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

Look at this

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