“结构命名空间"是否存在技术原因?在 C? [英] Is there a technical reason for the "struct namespace" in C?

查看:49
本文介绍了“结构命名空间"是否存在技术原因?在 C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C 中,大多数声明结构体的代码将遵循以下模式:

In C, most of the code declaring structs will follow this pattern:

/* struct forward-declaration */
typedef struct T T ;

/* struct definition */
typedef struct T
{
   /* etc. */
} T ;

这太普遍了,我与之交谈的大多数开发人员甚至都不知道上面的代码同时做了两件事(结构体声明,然后在正常命名空间中为结构体名称取别名),只是出于习惯而写了它.

This is so prevalent most developers I talked with didn't even know the code above did two things at the same time (struct declaration, then aliasing the struct name in the normal namespace), and just wrote it out of habit.

在 C++ 中,该问题得到缓解,因此您可以省略 typedefing 部分.在 C# 和 Java 中,设计人员甚至都没有打扰.所以这些语言无助于理解为什么 C 会这样做.

In C++, the issue is mitigated so you can omit the typedefing part. In C# and in Java, the designers didn't even bother. So those languages won't help understand why C does this that way.

因此,根据奥利弗查尔斯沃思的建议:

struct T 放在与其他普通标识符不同的命名空间中是否有技术原因?

Is there a technical reason to have struct T in a separate namespace from other normal identifiers?

C89/C90 标准中的相关部分是:

The relevant section in the C89/C90 standard is:

6.1.2.3 标识符的命名空间

在翻译单元中的任何一点都可以看到多个特定标识符的声明.句法上下文消除了指代不同实体的用法的歧义.因此.不同类别的标识符有单独的名称空间,如下所示:

It more than one declaration of a particular identifier is visible at any point in a translation unit. the syntactic context disambiguates uses that refer to different entities. Thus. there are separate name spaces for various categories of identifiers, as follows:

  • [...]

  • [...]

结构、联合和枚举的标签(通过跟随任何关键字structunion来消除歧义>枚举).

the tags of structure, unions and enumerations (disambiguated by following any of the keywords struct, union, or enum).

[...]

所有其他标识符.称为普通标识符(在普通声明符或枚举常量中声明).

all other identifiers. called ordinary identifiers (declared in ordinary declarators or as enumeration constants).

C11(n1570:6.2.3 标准草案)的文本大致相同.

The text for C11 (n1570:6.2.3 standard draft) is more or less the same.

推荐答案

第一行只有在你想使用 T 名称引用 struct T 时才需要第二个声明.

The first line is only needed if you want to reference struct T using the name T inside the second declaration.

对于不包含此类引用的结构,只需要第二种形式.对于这种情况,为了简单起见,我建议删除当时毫无意义的结构标记:

For structs that don't contain such references, only the second form is needed. For such cases, for simplicity and brevity, I would recommend dropping the then pointless struct tag:

typedef struct {
  /* interesting fields go here */
} T;

此外,typedef 不会将结构名称带入正常的命名空间",它会创建(就像 typedef 总是这样)一个 别名 (T) 用于不同的类型名称 struct T.当然,这里的名称拼写之间没有联系,这就是为什么我建议首先删除 struct 标记,它只是添加一个在大多数情况下毫无意义的名称.

Also, the typedef doesn't "bring the struct name into the normal namepace", it creates (like typedef always does) an alias (T) for a different type name struct T. Of course there is no connection between the spelling of names here, which is why I recommend dropping tagging the struct in the first place, it just adds a name that is pointless most of the time.

这篇关于“结构命名空间"是否存在技术原因?在 C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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