本文介绍了什么时候对typedef基本数据类型有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公司的内部c ++编码标准文档声明,即使对于基本数据类型,如int,char等,应该定义自己的typedef如typedef int Int。这是由于代码的可移植性的优点。
然而,有什么一般的考虑/建议什么时候(在哪些类型的项目的类型)是真的有意义吗?
提前感谢..

A company's internal c++ coding standards document states that even for basic data types like int, char, etc. one should define own typedefs like "typedef int Int". This is justified by advantage of portability of the code.However are there general considerations/ advice about when (in means for which types of projects) does it really make sense?Thanks in advance..

推荐答案

Typedefing int Int 几乎没有提供任何优点(它不提供语义上的好处,并导致诸如 typedef long Int 平台保持兼容)。

Typedefing int to Int offers almost no advantage at all (it provides no semantic benefit, and leads to absurdities like typedef long Int on other platforms to remain compatible).

但是,typedefing int 到eg int32_t (以及 long int64_t 等)确实提供了一个优点,因为你现在可以自由选择具有相关宽度的数据类型自我记录的方式,它将是便携式的(只是切换typedefs在不同的平台)。

However, typedefing int to e.g. int32_t (along with long to int64_t, etc.) does offer an advantage, because you are now free to choose the data-type with the relevant width in a self-documenting way, and it will be portable (just switch the typedefs on a different platform).

事实上,大多数编译器提供了一个 stdint.h ,其中包含所有这些定义。

In fact, most compilers offer a stdint.h which contains all of these definitions already.

这篇关于什么时候对typedef基本数据类型有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 19:51