在 C++11 之前,为了解决不同平台下 unsigned long long 位数不同的问题,程序员通常采用以下两种方式之一:

  1. 使用预编译指令或条件编译,根据当前编译环境的位数进行选择。例如:
#if defined(_MSC_VER) && (_MSC_VER < 1600)
    typedef unsigned __int64 uint64_t;
    typedef __int64 int64_t;
#elif defined(_MSC_VER)
    typedef unsigned long long uint64_t;
    typedef long long int64_t;
#else
    #include <stdint.h>
#endif

在这个例子中,我们根据编译器类型和版本来选择使用 unsigned __int64__int64(用于 Microsoft Visual C++ 版本 2008 及以下),或使用 unsigned long longlong long(用于 Microsoft Visual C++ 版本 2010 及以上),或者使用标准库中的 stdint.h 头文件中的类型和常量。

  1. 定义自己的类型和常量,例如:
#if UINT_MAX == 0xffffffffUL
    typedef unsigned long long uint64_t;
    typedef long long int64_t;
    #define UINT64_MAX    0xffffffffffffffffULL
    #define INT64_MAX     0x7fffffffffffffffLL
    #define INT64_MIN     (-INT64_MAX - 1LL)
#else
    typedef unsigned long int uint64_t;
    typedef long int int64_t;
    #define UINT64_MAX    0xffffffffffffffffUL
    #define INT64_MAX     0x7fffffffffffffffL
    #define INT64_MIN     (-INT64_MAX - 1L)
#endif

在这个例子中,我们根据 UINT_MAX(无符号 int 类型的最大值)来选择使用 unsigned long longlong long,或使用 unsigned longlong,并定义自己的常量。

这些方法虽然解决了不同平台下 unsigned long long 位数不同的问题,但是这些方法需要依赖于编译器和操作系统,不够通用和可移植。因此,从 C++11 开始,标准库提供了 <cstdint> 头文件,以提供一种更方便、通用、可移植的解决方案。