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.

有帮助吗?

解决方案

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.

其他提示

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.

I don't think that C adopts name equivalence. IMHO, C adopts structural equivalence with exceptions for structures and unions; for them names are 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 by structure ... e.g., int * and short * are different types but int * and int * are same types. 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 same types since they are in the same structure. Please, don't confuse that struct is for structure of types. It is just for defining type names, more precisely structure tag names in C.

Simply, in strutural equivalence, the types are unfolded up to atomic type names and they are compared. In C, the types are unfolded up to user-defined names. (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 reverse.

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 well-known language adopting the strict name equivalence.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top