C++ 中的固定宽度整数

2021-12-22 00:00:00 c types visual-c++ portability c++

有时我需要使用固定宽度的整数来与 PLC 等外部设备进行通信.我还使用它们来定义位掩码并执行图像数据的位操作.AFAIK C99 标准定义了固定宽度的整数,如 int16_t.但是,我使用的编译器 VC++ 2008 不支持 C99 并且 AFAIK Microsoft 不打算支持它.

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.

我的问题是在 C++ 中使用固定宽度整数的最佳实践是什么?

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

我知道 VC++ 定义了像 __int16 这样的非标准固定宽度整数,但我对使用非标准类型犹豫不决.下一个 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?

推荐答案

Boost 具有适用于所有 C99 类型及更多类型的 typedef:"Boost 整数库"

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

相关文章