在 C 中为 typedef 区分类型/标签名称有什么优点? [英] What are the advantages of differentiating type/tag names for a typedef in C?

查看:25
本文介绍了在 C 中为 typedef 区分类型/标签名称有什么优点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些代码库对 tagtype 使用不同的标识符,例如:

Some code-bases use a different identifier for the tag and the type, eg:

typedef struct _foo { int bar; } foo;

代替:

typedef struct foo { int bar; } foo;

此处详细解释了差异:C++ 中struct"和typedef struct"之间的区别?
(注意,这个问题是关于 C 的,链接是一个 C++ 问题,所以我假设该答案的基础知识也适用于 C,尽管情况可能并非如此,或者可能有一些微妙的差异).

我的问题是:

为类型命名空间和全局命名空间使用不同的标识符有哪些实际优势(如果有)?

What are the practical advantages (if any) of using different identifiers for the type name-space and the global name-space?

是否有充分的理由做一个而不是另一个?(在某些条件下)..
或者这只是一个约定?

Are there strong reasons to do one over the other? (under certain conditions)..
or is this just a convention?

推荐答案

在我看来,没有真正的优势.结构标记和 typedef 名称位于不同的命名空间"(不是 C++ 意义上的 命名空间),因此对两者使用相同的名称没有任何实际问题.

In my opinion, there are no real advantages. Struct tags and typedef names are in different "namespaces" (not namespaces in the C++ sense), so there's no real problem using the same name for both.

声明

typedef struct foo {
    int bar;
} foo;

是完全有效的,并且名称 struct foo 和名称 foo 之间不可能存在冲突.struct 标签只能出现在 struct 关键字之后.

is perfectly valid, and there is no possible conflict between the name struct foo and the name foo. The struct tag can only appear immediately after the struct keyword.

在某些 IDE 中可能有一些细微的优势.例如,如果您使用单击标识符并使​​用显示该标识符声明的功能,则可能会有一些歧义;它可以向您显示 typedef 名称或标签.但是因为两者都指的是同一种类型,所以这应该不是什么大问题.

There might be some slight advantage in some IDEs. For example, if you use click on an identifier and use a feature that shows you that identifier's declaration, there could be some ambiguity; it could show you either the typedef name or the tag. But since both refer to the same type, that shouldn't be much of a problem.

如果您想避免使用名称 struct foo,而更喜欢使用 typedef 名称 foo,那么为 struct 标记使用不同的标识符可以提醒您不要使用它.例如:

If you want to avoid using the name struct foo, preferring the typedef name foo, then using a different identifier for the struct tag can be a reminder not to use it. For example:

typedef struct foo_s {
    int bar;
} foo;

可以作为提醒,避免使用名称foo_s.

can serve as a reminder to avoid the name foo_s.

除此之外,在我看来,结构类型的 typedef 是不必要的.我将上述简单定义为:

Going beyond this, in my opinion typedefs for structure types are not necessary. I would define the above simply as:

struct foo {
    int bar;
};

并将类型称为struct foo.我个人认为为已经有一个完美名称 struct foo 的类型定义一个新名称 foo 并没有什么好处.唯一的例外是抽象类型,你想隐藏它是一个结构的事实.在 中输入 FILE 就是一个例子.

and refer to the type as struct foo. I personally don't see much benefit in defining a new name foo, for a type that already has a perfectly good name struct foo. The only exception is for an abstract type, where you want to hide the fact that it's a structure. Type FILE in <stdio.h> is an example of this.

另一方面,很多优秀的 C 程序员在这一点上不同意我的观点,认为定义一个只是标识符的名称是值得的.

On the other hand, plenty of good C programmers disagree with me on this point, and feel that it's worthwhile to define a name that's just an identifier.

这篇关于在 C 中为 typedef 区分类型/标签名称有什么优点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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