关于typedef的问题 [英] question on typedefs

查看:112
本文介绍了关于typedef的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些代码,其中包含以下typedef:

Hi, I encountered someones code which has following typedefs:

typedef unsigned short          u16_t;
typedef int                     s32_t;
typedef unsigned int            u32_t;





为什么需要它?出于可读性目的?



Why is it needed? For readability purposes?

推荐答案

C和C ++原始类型不保证一定的大小。例如,无法保证int的持续时间。这是该语言的父亲明智的设计决定,但也有它的缺点。例如,如果要描述某些项目必须具有特定长度的数据结构。为此,许多开发人员已经开始编写类似于你所展示的typedef。其中一些是有用的(对于大小合规或简单的简写),其他一些破坏原始语言概念并尝试强制执行某些类型大小。在许多情况下,这会适得其反。例如,在16位CPU上强制int为16位是有意义的。但是在32位或64位上,这很可能会产生较慢的代码。
C and C++ original types do not guarantee a certain size. For example, there is no guarantee for how long an int is. That was a wise design decision of the fathers of the language, but has also its draw backs. For example if you want to describe a data structure in which certain items must have certain lengths. For that purpose, many developers have started to write typedefs like the ones you have shown. Some of them are useful (for size compliance or as simple short hand), others undermine the original language concept and try to enforce certain type sizes. In many cases that is counter-productive. For example forcing an int to 16-bits makes sense on a 16-bit CPU. But on 32-bit or 64-bit this will most likely produce slower code.


这篇关于关于typedef的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆