Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed-width integers in C++

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?

like image 461
Dani van der Meer Avatar asked Apr 09 '09 15:04

Dani van der Meer


2 Answers

You can workaround the problem with some #ifdef directives.

#ifdef _MSC_VER
   typedef __int16 int16_t
#else
   #include <stdint.h>
#endif
like image 72
mmx Avatar answered Nov 07 '22 08:11

mmx


Boost has the typedefs for all of the C99 types and more: "Boost integer library"

like image 33
Hrvoje Prgeša Avatar answered Nov 07 '22 06:11

Hrvoje Prgeša