Should I use long long or int64_t for portable code?

后端 未结 3 2277
臣服心动
臣服心动 2020-12-15 03:03

I have an open-source codebase that is written in both C and C++. I\'m looking for an integer type that is guaranteed to be at least 64 bits wide, which can be reli

相关标签:
3条回答
  • 2020-12-15 03:23

    Use int64_t. int64_t means 64 bits and you'll get 64 bits wherever you go. long long is actually as implementation dependent as long is. That is, a long long has to be bigger than or equal to a long, but that could be different depending on the compiler and platform.

    0 讨论(0)
  • 2020-12-15 03:30

    Is the general suggestion to use int64_t in place of long long, when portability is an important goal?

    I'd be very surprised if a compiler offered int64_t but not long long.

    If long long is present, it must have at least 64 bits, so casting from (u)int64_t to (unsigned) long long is value-preserving.

    If you need a type with exactly 64 bits, use (u)int64_t, if you need at least 64 bits, (unsigned) long long is perfectly fine, as would be (u)int_least64_t.

    0 讨论(0)
  • 2020-12-15 03:37

    The types long long and unsigned long long are standard C and standard C++ types each with at least 64 bits. All compilers I'm aware of provide these types, except possibly when in a -pedantic mode but in this case int64_t or uint64_t won't be available with pre-C++ 2011 compilers, either. On all of the systems <stdint.h> is available, too. That is, as far as I can tell it doesn't matter much how you spell the type. The main goal of <stdint.h> is to provide the best match for a specific number of bits. If you need at least 64 bit but you also want to take advantage of the fasted implementation of such a type, you'd use int_least64_t or uint_least64_t from <stdint.h> or <cstdint> (in case of the latter, the names are defined in namespace std).

    0 讨论(0)
提交回复
热议问题