为什么对结构使用不同的标签和 typedef? [英] Why use different a different tag and typedef for a struct?

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

问题描述

在 C 代码中,我看到了以下内容:

In C code, I've seen the following:

typedef struct SomeStructTag {
    // struct members
} SomeStruct;

我不清楚为什么这与以下内容不同:

I'm not clear on why this is any different from:

typedef struct SomeStruct {
    // struct members
} SomeStruct;

为类型使用特定名称并将其typedef改为不同的类型名称有什么意义?

What's the point of using a particular name for the type and typedefing it to a different type name?

推荐答案

在大多数情况下,可以为两种目的使用相同的名称,没有任何问题.人们在代码中使用不同名称的做法可能源于 ANSI 标准化之前的日子,当时有很多编译器接受类似于 C 的大多数兼容方言.一些构造和功能几乎适用于任何编译器,其他人则适用在大多数上(但在大量失败上失败),而其他人只能在少数上工作.尽管多年来标准明确规定类型、结构标记和每个单独结构的成员有不同的命名空间,但早期的一些编译器并没有将所有这些命名空间识别为不同的、相当数量的代码编写的代码即使使用这样的编译器也应该可以工作,而且人们倾向于编写与他们见过的其他代码类似的代码.

In most cases, one could use the same name for both purposes without any problem. The practice of people using different names in code probably stems from the days before ANSI standardization, when there were a lot of compilers that accepted mostly-compatible dialects of something resembling C. Some constructs and features would work on almost any compiler, others would work on most (but fail on a significant number), and others would only work on a few. Although the standard has for many years clearly mandated that there are different namespaces for types, structure tags, and for the members of each individual structure, some compilers from the early days didn't recognize all those namespaces as distinct, a fair amount of code was written which was supposed to work even with such compilers, and people tend to write code which resembles other code they've seen.

我认为,鉴于今天的规则,对使用相同标识符的最强烈反对是不同标识符应该看起来不同的原则,并且通常应该避免为给定目的使用两个语义等效的标识符.如果它们之间有一些明显的语义区别(例如最快的类型至少 32 位"与正好 32 位"类型),那么出于某种目的使用两个不同的标识符是可以的,但是让代码使用两个不同的标识符是不好的出于相同目的的标识符基本上是随机的.如果声明 typedef struct FOO {...} FOO;,那么 struct FOOFOO 将是不同的标识符,看起来好像它们应该可以互换.如果在某些特定情况下应该使用一种或另一种并且名称不同,那么在不应该使用的地方键入 struct ,反之亦然,将导致编译错误.如果名称匹配,事情就会编译,并且不会有关于名称的用法与其在其他地方的用法不一致的警告.

I think the strongest objection to using the same identifier given today's rules would be the principle that distinct identifiers should look distinct, and one should generally avoid using two semantically-equivalent identifiers for a given purpose. It's fine to use two distinct identifiers for some purpose if there's some clear semantic distinction between them (e.g. "the fastest type that's at least 32 bits" versus an "exactly 32 bits" type), but it's not good to have code use two different identifiers for the same purpose essentially at random. If one declares typedef struct FOO {...} FOO;, then struct FOO and FOO will be distinct identifiers that look as though they should be interchangeable. If there are particular cases where one is supposed to use one or the other and the names are distinct, then typing struct where one shouldn't, or vice versa, will cause a compilation error. If the names match, things would compile and there would be no warning that the usage of the name wasn't consistent with its usage elsewhere.

顺便说一句,尽管结构标记是全局可用的,但通常不需要多次使用任何标记.即使结构应该包含自引用指针,也可以在 ANSI C 中声明它:

Incidentally, although struct tags are globally available, there often isn't any need to use any tag more than once. Even if a struct is supposed to contain self-referential pointers, one may, in ANSI C, declare it:

typedef struct NODE_S NODE;
struct NODE_S {
  NODE *parent,*left,*right;
};

无需在除了声明 typedef 名称的特定行之外的任何地方将类型称为 struct NODE_S.在其他地方,人们可以直接使用 typedef 名称.

without having to refer to the type as struct NODE_S anywhere except the particular line declaring the typedef name. Everywhere else, one may instead simply use the typedef name directly.

这篇关于为什么对结构使用不同的标签和 typedef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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