最佳实践:我应该在创建C或C ++为字节的typedef? [英] Best Practices: Should I create a typedef for byte in C or C++?
问题描述
你preFER看到的东西像 t_byte *
(用的typedef unsigned char型t_byte
)或无符号字符*
在code?
Do you prefer to see something like t_byte*
(with typedef unsigned char t_byte
) or unsigned char*
in code?
我在我自己的库倾向于 t_byte
,但从来没有一个大的项目,其中被采取这个方法的工作,而且我想知道的陷阱。
I'm leaning towards t_byte
in my own libraries, but have never worked on a large project where this approach was taken, and am wondering about pitfalls.
推荐答案
如果您正在使用C99或更新版本,你应该使用 stdint.h
这一点。 uint8_t有
,在这种情况下。
If you're using C99 or newer, you should use stdint.h
for this. uint8_t
, in this case.
C ++没有得到这头,直到C ++ 11,称其为 cstdint
。旧版本的Visual C ++的没让你使用C99的用C stdint.h
++ code,但pretty多隔日C ++编译器98一样,所以你可以使用旧的编译器时有这样的选择偶数。
C++ didn't get this header until C++11, calling it cstdint
. Old versions of Visual C++ didn't let you use C99's stdint.h
in C++ code, but pretty much every other C++98 compiler did, so you may have that option even when using old compilers.
由于有这么多其他的事情,提升在以上的 升压/ integer.hpp
,提供类似的事情 uint8_t有
如果你的编译器的C ++标准库没有。
As with so many other things, Boost papers over this difference in boost/integer.hpp
, providing things like uint8_t
if your compiler's standard C++ library doesn't.
这篇关于最佳实践:我应该在创建C或C ++为字节的typedef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!