cequivalencetype-equivalence

Name equivalence question


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.


Solution

  • 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.