我应该使用cstdint吗? [英] Should I use cstdint?
问题描述
我一直在思考是否应该使用< cstdint>
里面的typedef。
I have been pondering on whether or not I should use the typedefs inside <cstdint>
or not.
我个人更喜欢写 uint32_t
over unsigned int
和 int8_t
char
etc ...因为它对我来说更直观。
I personally prefer writing uint32_t
over unsigned int
and int8_t
over char
etc... since it to me is alot more intuitive.
你们怎么想?使用< cstdint>
的typedef是否是个好主意?是否有什么缺点?
What do you guys think? Is it a good idea to use the typedefs from <cstdint>
or not? Are there any disadvantages?
推荐答案
实际上,我建议使用两者。
Actually, I would suggest using both.
如果你想要一个肯定是32位无符号的,使用uint32_t。例如,如果你正在实现一个struct来表示一个外部对象,其规范定义其中一个字段为32位无符号。
If you want something that is definitely 32-bits unsigned, use uint32_t. For example, if you are implementing a "struct" to represent an external object whose specification defines one of its fields as 32 bits unsigned.
如果你想要的东西是机器的自然字大小,使用int或unsigned int。例如:
If you want something that is the "natural word size of the machine", use int or unsigned int. For example:
for (unsigned i = 0 ; i < 200 ; ++i)
// stuff
机器的自然字大小将给你最好的性能,处理器和明天。
The "natural word size of the machine" is going to give you the best performance, both on today's processors and on tomorrow's.
如果您的意思是字符,请使用char; char或unsigned char,如果你的意思是byte。
Use "char" if you mean "character"; "char" or "unsigned char" if you mean "byte". C/C++ lets you access an arbitrary object's bytes via "char *", not anything else, strictly speaking.
使用uint8_t或int8_t,如果你特别想要一个8位的字符串,你可以通过char *访问任意对象的字节。整数,类似于uint32_t。
Use uint8_t or int8_t if you specifically want an 8-bit integer, similar to uint32_t.
这篇关于我应该使用cstdint吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!