名称等效问题

发布于 2024-10-24 00:58:53 字数 150 浏览 2 评论 0原文

假设我有:

int a;
int b;

变量 ab 名称是否等效(更具体地说,由于基本类型没有类型名称,因此它们可以被视为名称等效)吗?

谢谢。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陈甜 2024-10-31 00:58:53

名称(更准确地说,名义上)等效意味着值具有由其类型的(完全限定)名称确定的相同类型 - 例如,ab名义上类型等效,因为它们都有“int”类型。结构等效意味着这些值被认为具有相同的类型,因为它们的类型在结构上是等效的,无论名称如何。名义类型等效意味着结构类型等效,因为命名类型在结构上与其自身等效。您的 ab 名义上是类型等效的,因为它们具有相同的名称类型(“int”)。 “原始类型没有类型名称”的说法完全是错误的—— int 是类型名称。和 int a; 没有区别。 int b; 和 int a, b; -- 都定义了相同的 ab(结构上和名称上)类型。

C 的类型系统通常按名称...例如,int *short* 是不同的类型,即使 intshort< /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 and b 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. Your a and b, 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 between int a; int b; and int a, b; -- both define a and b with the same (structurally and by name) type.

C's type system is generally by name ... e.g., int * and short* are different types even if int and short have the same representation, and struct foo { int x; } and struct bar { int x; } are different types even though they always have the same representation.

自由如风 2024-10-31 00:58:53

我发现的一些课堂笔记似乎表明:

int a;
int b;

变量ab不是名称等效。

然而,对于:

int a, b;

变量ab名称是等价的。

Some class notes I found seem to indicate that with:

int a;
int b;

Variables a and b are not name equivalent.

However, with:

int a, b;

Variables a and b are name equivalent.

纸短情长 2024-10-31 00:58:53

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 中,类型通过 structunion 展开为名称,包括用户定义的名称。 (展开请参考http:// www.csd.uwo.ca/~moreno//CS447/Lectures/TypeChecking.html/node3.html)这就是区别。当然,名称等效类型在结构上是等效的,但反之则不然。

对于以下情况:

int x;
int y;

xy 以及名称等效和结构等效。但是,在以下情况下:

int *x;
int *y;

xy 结构等效,但名称不同。即使对于以下情况:

int *x, *y;

在严格的名称等效方案中,xy 也不被视为等效,因为此声明仅被视为前一个声明的简写。 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 * or char[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 using typedef.

C's type system is generally adopting structural equivalence. For instance, int * and short * are different types but int * and int * are the same types even though no names are attributed to the pointers. The reason for that struct foo { int x; } and struct bar { int x; } are different types is because they have different structure up to user-defined type names. Types struct foo * and struct foo * are the same types since they have the same structure. Note that struct 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 or union. (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:

int x;
int y;

x and y and both name equivalent and structural equivalent. But, in the following case:

int *x;
int *y;

x and y are structural equivalent but not name equivalent. Even for the following case:

int *x, *y;

x and y 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文