Fixed-width integers in C++

Dani van der Meer picture Dani van der Meer · Apr 9, 2009 · Viewed 19.9k times · Source

Occasionally I need to use fixed-width integers for communication with external devices like PLCs. I also use them to define bitmasks and perform bit manipulation of image data. AFAIK the C99 standard defines fixed-width integers like int16_t. However the compiler I use, VC++ 2008 doesn't support C99 and AFAIK Microsoft is not planning to support it.

My question is what is the best practice for using fixed-width integers in C++?

I know that VC++ defines non-standard fixed-width integers like __int16, but I am hesitant to use a non-standard type. Will the next C++ standard define fixed-width integers?

Answer

mmx picture mmx · Apr 9, 2009

You can workaround the problem with some #ifdef directives.

#ifdef _MSC_VER
   typedef __int16 int16_t
#else
   #include <stdint.h>
#endif