开发者

long long in C++

I'm creating a game engine and It's going to have support for Microsoft Windows X86 and X64 editions. When I'm creating the custom typedefs, do I have to specify "typedef unsigned lo开发者_如何学编程ng long" if the version is X86 and "typedef unsigned long" when the version is X64?


#include <cstdint>

and use uint32_t, uint64_t, etc.


The long long type is 64 bits on all compilers that support it. (The C/C++ standards say it has to be at least 64 bits; I don't think there are any systems yet that support larger types.) If you just want a 64 bit integer typedef, you can use long long without any worries.

The C99 and C++11 standards provide a new header, <stdint.h> or <cstdint>, that already has a set of fixed size typedefs, so you can just use uint64_t. Visual Studio 2010 supports this but I don't think VS2008 does.

The C and C++ standards only specify minimum sizes for the standard integer types:

  • char is specified as at least 8 bits
  • short is specified as at least 16 bits
  • int is specified as at least 16 bits
  • long is specified as at least 32 bits
  • long long is specified as at least 64 bits


Since 64-bit Windows uses the LLP64 data model and the Visual C++ compiler too, of course, you should be safe using typedef unsigned long long for both versions, since in LLP64 long remains 32 bits.


long long has been working for decades in most C++ compilers (due to the C heritage), althoug it wasn't part of the standard.

There is one little snag: If you compile C++ code with long long with GNU C++ at the -pedantic warning level, it will complain that long long is not standard C++. This is True. Actually it is also true that long long works. On GNU C++. And every other compiler. This does not have to be a problem except you sometimes run into people that force you to blindly enable all compiler warning options, and fix all the trouble that surfaces.

Funny thing is that at the same time GNU C++ will let you use uint64_t without complaining. This is also C, not C++, except that it is less portable because compiler vendors are slow to implement C99.


Here is the code snippet from the Qt source code:

#if defined(Q_OS_WIN) && !defined(Q_CC_GNU) && !defined(Q_CC_MWERKS)
#  define Q_INT64_C(c) c ## i64    /* signed 64 bit constant */
#  define Q_UINT64_C(c) c ## ui64   /* unsigned 64 bit constant */
typedef __int64 qint64;            /* 64 bit signed */
typedef unsigned __int64 quint64;  /* 64 bit unsigned */
#else
#  define Q_INT64_C(c) static_cast<long long>(c ## LL)     /* signed 64 bit constant */
#  define Q_UINT64_C(c) static_cast<unsigned long long>(c ## ULL) /* unsigned 64 bit constant */
typedef long long qint64;           /* 64 bit signed */
typedef unsigned long long quint64; /* 64 bit unsigned */
#endif

typedef qint64 qlonglong;
typedef quint64 qulonglong;

We can see that unsigned __int64 and __int64 are used on windows.


Try boost integer.hpp. It has uint64_t with is guranteed to be unsigned 64 bits on all operating systems.


Concerning Visual Studio (different versions), this page is helpful

I never knew long was only 32 bit on 64bit Windows... until I ran into weird problems with msSinceEpoch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜