名称等效问题
假设我有:
int a;
int b;
变量 a
和 b
名称是否等效(更具体地说,由于基本类型没有类型名称,因此它们可以被视为名称等效)吗?
谢谢。
Suppose I have:
int a;
int b;
Are the variables a
and b
name equivalent (more specifically, since primitive types don't have type names, can they be considered name equivalent)?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
名称(更准确地说,名义上)等效意味着值具有由其类型的(完全限定)名称确定的相同类型 - 例如,
a
和b
名义上类型等效,因为它们都有“int”类型。结构等效意味着这些值被认为具有相同的类型,因为它们的类型在结构上是等效的,无论名称如何。名义类型等效意味着结构类型等效,因为命名类型在结构上与其自身等效。您的a
和b
名义上是类型等效的,因为它们具有相同的名称类型(“int”)。 “原始类型没有类型名称”的说法完全是错误的——int
是类型名称。和 int a; 没有区别。 int b; 和int a, b;
-- 都定义了相同的a
和b
(结构上和名称上)类型。C 的类型系统通常按名称...例如,
int *
和short*
是不同的类型,即使int
和short< /code> 具有相同的表示形式,并且
struct foo { int x; }
和struct bar { int x; }
是不同的类型,尽管它们总是具有相同的表示形式。Name (more properly, nominal) equivalence means that the values have the same type as determined by the (fully qualified) name of their type -- for instance,
a
andb
are nominally type equivalent because they both have "int" type. Structural equivalence means that the values are considered to have the same type because their types are structurally equivalent, regardless of the name. Nominal type equivalence implies structural type equivalence since a named type is structurally equivalent to itself. Youra
andb
, are nominally type equivalent because they have the same type by name ("int"). The claim that "primitive types don't have type names" is simply false --int
is a type name. And there is no difference betweenint a; int b;
andint a, b;
-- both definea
andb
with the same (structurally and by name) type.C's type system is generally by name ... e.g.,
int *
andshort*
are different types even ifint
andshort
have the same representation, andstruct foo { int x; }
andstruct bar { int x; }
are different types even though they always have the same representation.我发现的一些课堂笔记似乎表明:
变量
a
和b
不是名称等效。然而,对于:
变量
a
和b
名称是等价的。Some class notes I found seem to indicate that with:
Variables
a
andb
are not name equivalent.However, with:
Variables
a
andb
are name equivalent.C 采用除结构和联合外的结构等效;对于这些类型,使用名称等效。确实,在名称等价中,这些值具有由其类型名称确定的相同类型,并且在结构等价中,这些值被认为具有由结构等价确定的相同类型。但即使在结构等价上,也不是无论名称如何都可以确定的。
基本上,编程语言中的原子类型都有名称,并且它们用于名称等效和结构等效。差异来自派生类型,例如
int *
或char[10]
。确实,“原始类型具有类型名称”。 C 的类型系统将派生类型的结构与用户定义的名称进行比较,不包括类型同义词,即使它们是由用户使用 typedef 定义的。C的类型系统一般采用结构等价。例如,
int *
和short *
是不同的类型,但int *
和int *
甚至是相同的类型尽管没有为这些指针命名。原因是struct foo { int x; }
和struct bar { int x; }
是不同的类型,因为它们具有不同的结构,直至用户定义的类型名称。类型struct foo *
和struct foo *
是相同的类型,因为它们具有相同的结构。请注意,struct
不仅定义了结构,还定义了类型的名称,更准确地说是结构标记名称。简单地说,在结构等价中,类型被展开为原子类型名称并进行比较。在 C 中,类型通过
struct
或union
展开为名称,包括用户定义的名称。 (展开请参考http:// www.csd.uwo.ca/~moreno//CS447/Lectures/TypeChecking.html/node3.html)这就是区别。当然,名称等效类型在结构上是等效的,但反之则不然。对于以下情况:
x
和y
以及名称等效和结构等效。但是,在以下情况下:x
和y
结构等效,但名称不同。即使对于以下情况:在严格的名称等效方案中,
x
和y
也不被视为等效,因为此声明仅被视为前一个声明的简写。 Ada 是一种采用严格名称对等的著名语言。C adopts structural equivalence except for structures and unions; for these types, name equivalence is used. It is true that the values have the same type as determined by the name of their type in name equivalence and that the values are considered to have the same type as determined by structurally equivalent in structural equivalence. But even in structural equivalence, it is not determined regardless of the name.
Basically, atomic types in a programming language have names and they are used both in name equivalence and structural equivalence. The difference comes from derived types such as
int *
orchar[10]
. It is true that "primitive types have type names." And C's type system is comparing the structure of derived types up to user-defined names excluding type synonyms even they are defined by the user usingtypedef
.C's type system is generally adopting structural equivalence. For instance,
int *
andshort *
are different types butint *
andint *
are the same types even though no names are attributed to the pointers. The reason for thatstruct foo { int x; }
andstruct bar { int x; }
are different types is because they have different structure up to user-defined type names. Typesstruct foo *
andstruct foo *
are the same types since they have the same structure. Note thatstruct
defines not only the structure but also name, more precisely structure tag name, of the type.Simply, in structural equivalence, the types are unfolded up to atomic type names and they are compared. In C, the types are unfolded up to names including user-defined names by
struct
orunion
. (For unfolding, please refer http://www.csd.uwo.ca/~moreno//CS447/Lectures/TypeChecking.html/node3.html) This is the difference. Naturally, name equivalent types are structurally equivalent, but not the converse.For the following case:
x
andy
and both name equivalent and structural equivalent. But, in the following case:x
andy
are structural equivalent but not name equivalent. Even for the following case:x
andy
are not considered equivalent in a strict name equivalence scheme, for this declaration is considered as just a shorthand of the previous declaration. Ada is a well-known language adopting the strict name equivalence.