简体   繁体   English

为什么没有将typedef声明称为typedef定义?

[英]Why is a typedef declaration not called a typedef definition?

I see occasional questions such as "what's the difference between a declaration and a definition": 我偶尔会看到诸如“声明和定义之间的区别”之类的问题:

What is the difference between a definition and a declaration? 定义和声明之间有什么区别? The distinction is important and intellectually it achieves two important things: 这种区别很重要,从思想上讲,它可以实现两件重要的事情:

  1. It brings to the fore the difference between reference and referent 它突显了参照物与参照物之间的区别
  2. It's how C enables separation in time of the attachment between reference and referent. 这就是C如何使参照和参照之间的依附时间分开。

So why is a C typedef declaration not called a typedef definition? 那么,为什么C typedef声明不称为typedef定义呢?

Firstly, it's obviously a definition. 首先,这显然是一个定义。 It defines an alias. 它定义一个别名。 The new name is to be taken as referring to the existing thing. 新名称将被视为指现有事物。 But it certainly binds the reference to a specific referent and is without doubt a defining statement. 但是,它肯定会将引用绑定到特定的引用对象,并且无疑是定义性声明。

Secondly, wouldn't it be called a typedec if it were a declaration? 其次,如果它是一个声明,它不会被称为typedec吗?

Thirdly, wouldn't it avoid all those confusing questions people ask when they try and make a forward declaration using a typedef? 第三,它是否可以避免人们在尝试使用typedef进行前向声明时提出的所有令人困惑的问题?

A typedef declaration is a definition. typedef声明一个定义。

N1570 6.7p5: N1570 6.7p5:

A declaration specifies the interpretation and attributes of a set of identifiers. 声明指定一组标识符的解释和属性。 A definition of an identifier is a declaration for that identifier that: 标识符的定义是对该标识符的声明:

  • for an object, causes storage to be reserved for that object; 对于一个对象,导致为该对象保留存储;
  • for a function, includes the function body; 对于功能,包括功能主体;
  • for an enumeration constant, is the (only) declaration of the identifier; 对于枚举常量,是标识符的(唯一)声明;
  • for a typedef name, is the first (or only) declaration of the identifier. 对于typedef名称,是标识符的第一个(或唯一)声明。

In C99, the last two bullet points were combined; 在C99中,最后两个项目符号要点相结合; C11 introduced the ability to declare the same typedef twice. C11引入了两次声明相同typedef

Note that only objects, functions, enumeration constants, and typedef names can have definitions . 请注意, 只有对象,函数,枚举常量和typedef名称可以具有定义 One might argue that given: 可能有人认为,鉴于:

enum foo { zero, one};

it doesn't make much sense to consider this to be a definition of zero and one , but not of foo or enum foo . 认为这是zeroone定义是没有道理的,但不能定义 fooenum foo On the other hand, an enum , struct , or union declaration, though it creates a type that didn't previously exist, doesn't define an identifier that is that type's name -- and for struct s and union , the tag name can be used (as an incomplete type) even before the type has been defined. 另一方面, enumstructunion声明尽管创建了以前不存在的类型,但并未定义该类型名称的标识符-对于struct s和union ,标记名称可以在定义类型之前就使用(作为不完整的类型)。 Definitions define identifiers, not (necessarily) the entities to which they refer. 定义定义标识符,而不是(不一定)定义它们所引用的实体。

As for why it's not called a "definition" in the subsection that defines it, it's part of section 6.7 "Declarations", which covers all kinds of declarations (some of which are also definitions). 至于为什么在定义它的小节中未将其称为“定义”,则它是第6.7节“声明”的一部分,其中涵盖了所有类型的声明(其中有些也是定义)。 The term definition is defined in the introductory part of 6.7. 术语定义在6.7的简介部分中定义。

As for the name typedef , it's caused a fair amount of confusion over the years since it doesn't really define a type. 至于typedef这个名字,多年来并没有真正定义一个类型,因此引起了很多混乱。 Perhaps typename would have been a better choice, or even typealias . 也许typename会是一个更好的选择,甚至是typealias But since it does define the identifier, typedef isn't entirely misleading. 但是由于typedef确实定义了标识符,因此不会完全误导。

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

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