简体   繁体   English

这些 `typedef` 的目的是什么?

[英]What's the purpose of these `typedef`?

typedef intptr_t        ngx_int_t; 
typedef uintptr_t       ngx_uint_t; 
typedef intptr_t        ngx_flag_t;

What can we benifit from this?I can't think of one to be honest...我们能从中受益什么?老实说,我想不出一个……

The above code are from the famous nginx project,check it if interested.以上代码来自著名的 nginx 项目,有兴趣可以查看。

One of the typedef purposes is portability. typedef 的目的之一是可移植性。 Eg different compilers and platforms have various type sizes, eg sizeof(int) on x86, Linux, gcc is not the same as on Texas Instrument's processors:) But it's still int.例如,不同的编译器和平台具有不同的类型大小,例如 x86、Linux、gcc 上的 sizeof(int) 与德州仪器的处理器不同:

So,所以,

typedef int INT32 

saves one when porting the code.移植代码时节省一个。

Another purpose of typedef, is to declare types in order to make shorter declarations. typedef 的另一个目的是声明类型以进行更短的声明。

typedef sharted_ptr<MyClass> MyClassPtr;

And now, you can use MyClassPtr as a type, instead of writing the whole shared_ptr... string.现在,您可以使用MyClassPtr作为类型,而不是编写整个 shared_ptr... 字符串。

And the very common usage of typedef with structures: typedef 与结构的非常常见的用法:

typedef struct {
   int x;
   int y;
} Point;

or或者

struct point {
   int x;
   int y;
}

typedef struct point Point;

Both typedefs let you avoid typing struct keyword every time.两种 typedef 都可以让您避免每次都输入struct关键字。

It's often done for code portability, and is particularly relevant for embedded systems.它通常用于代码可移植性,并且与嵌入式系统特别相关。

Suppose you have some integer values that absolutlely MUST be 32-bits long.假设您有一些 integer 值绝对必须是 32 位长。 Maybe they need to map to network/disk structures, maybe they need to hold values of that magnitude, or whatever.也许他们需要 map 到网络/磁盘结构,也许他们需要保持那个数量级的值,或者其他什么。

Now, suppose you develop your code on a compiler where 'int' is 32 bits.现在,假设您在“int”为 32 位的编译器上开发代码。 You write...你写...

struct s {
 int a,b,c,d;
}

...and it works fine. ......它工作正常。 But, if you need to switch to a compiler where int is only 16-bits, but long is 32, you would need to change all those declarations to但是,如果您需要切换到 int 仅为 16 位但 long 为 32 位的编译器,则需要将所有这些声明更改为

struct s {
 long a,b,c,d;
}

Worse yet, you can't do just search/replace, because some of the 'ints' you probably don't care about the size.更糟糕的是,您不能只进行搜索/替换,因为您可能不关心某些“整数”的大小。 So, the best approach is to to this:因此,最好的方法是:

typedef long INT32; // change this typedef according to compiler

struct s {
 INT32 a,b,c,d;
}

Then, all you need to is change the typedefs.然后,您只需要更改 typedef。

I know two reasons:我知道两个原因:

  • Aliasing, turning complex declaration something simpler别名,将复杂的声明变得更简单
  • Portability, at different architecture, a type could be differently just, as very simple example: u32 , where at some places could be defined as unsigned int , other unsigned long type.可移植性,在不同的架构下,一个类型可能会有所不同,就像一个非常简单的例子: u32 ,在某些地方可以定义为unsigned int ,其他unsigned long类型。

The reason could be that they wish to change the pointer type when porting the code.原因可能是他们希望在移植代码时更改指针类型。 On another system, there might be different addressing modes ("banking" etc), and then they might need to use non-standard syntax, like在另一个系统上,可能有不同的寻址模式(“银行”等),然后它们可能需要使用非标准语法,例如

typedef far intptr_t        ngx_int_t; 

If they never port the code to any system with more than one addressing mode on the same system, portability of pointers would never be an issue and the typedef would be redundant.如果他们从不将代码移植到同一系统上具有多个寻址模式的任何系统,那么指针的可移植性将永远不会成为问题,并且 typedef 将是多余的。

One of the reason I have seen people do this is that they think the if there is a need to change the actual type they just have to change the typedef .我看到人们这样做的原因之一是他们认为如果需要更改实际类型,他们只需要更改typedef I am not too convined about that argument though.不过,我不太相信这个论点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM