为什么在结构定义之后使用 typedef *after*? [英] Why using a typedef *after* struct definition?

查看:44
本文介绍了为什么在结构定义之后使用 typedef *after*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两种风格:

struct _something
{
   ...
};
typedef struct _something someting;

那种风格:

typedef struct _something
{
  ...
} something;

在 C 中是正确的 typedef 声明.
请注意,头文件中结构声明的存在是故意的:我需要在其他地方访问结构的内部组件.

are correct typedef declarations in C.
Note that the presence of the structure declaration in the header file is made on purpose: I need to have access to the inner components of the structure somewhere else.

第一个声明的一个缺点是,当您使用任何 IDE 时,自动跳转到声明"通常会将您定向到 typedef struct _something someing; 而不是直接给您真正的结构定义.

One drawback of the first declaration is that when you use any IDE, the automatic "jump to declaration" often directs you to the typedef struct _something someting; instead of giving you directly the real structure definition.

在第二种方法中,您可以直接进入结构定义.

In the second method, you get directly to the structure definition.

是否有理由使用第一种方法?
我正在处理的代码充满了这些...
这仅仅是维护者的坏/好习惯吗?

Is there a reason why one would use the first method?
The code I'm working on is full with these...
Is it simply a bad/good habit from the maintainers?

推荐答案

此讨论主题很好地概述了该主题,并强调了使用第一种样式的重要原因:

This discussion thread gives a good overview of the topic, and highlights an important reason to use the first style:

这种风格将类型定义分开(这是不是 typedef 所做的)从 typename 同义词创建(is typedef 做什么),并保留类型名称和类型同义词之间的强对应关系,没有对两者使用相同名称的缺点(这可能会混淆一些调试器,在任何情况下都是 grep 的痛苦).

This style separates type definition (which is not what typedef does) from typename synonym creation (which is what typedef does), and retains a strong correspondence between type name and type synonym without the disadvantage of using the same name for both (which can confuse some debuggers, and in any case is a pain for grep).

这篇关于为什么在结构定义之后使用 typedef *after*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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