Best Practices: Should I create a typedef for byte in C or C++?

前端 未结 6 1683
情歌与酒
情歌与酒 2021-02-07 05:40

Do you prefer to see something like t_byte* (with typedef unsigned char t_byte) or unsigned char* in code?

I\'m leaning towards

6条回答
  •  梦如初夏
    2021-02-07 06:01

    I prefer for types to convey the meaning of the values stored in it. If I need a type describing a byte as it is on my machine, I very much prefer byte_t over unsigned char, which could mean just about anything. (I have been working in a code base that used either signed char or unsigned char to store UTF-8 strings.) The same goes for uint8_t. It could just be used as that: an 8bit unsigned integer.

    With byte_t (as with any other aptly named type), there rarely ever is a need to look up what it is defined to (and if so, a good editor will take 3secs to look it up for you; maybe 10secs, if the code base is huge), and just by looking at it it's clear what's stored in objects of that type.

提交回复
热议问题