Path: utzoo!mnetor!uunet!husc6!sri-unix!quintus!ok From: ok@quintus.UUCP (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: A bad design decision early on in ANSI C. Message-ID: <525@cresswell.quintus.UUCP> Date: 12 Jan 88 22:37:31 GMT References: <1322@sugar.UUCP> <1942@ho95e.ATT.COM> <1373@sugar.UUCP> <4604@ihlpg.ATT.COM> Organization: Quintus Computer Systems, Mountain View, CA Lines: 43 Summary: structure comparison In article <4604@ihlpg.ATT.COM>, tainter@ihlpg.ATT.COM (Tainter) writes: > I think if you are going to have structure assignment you should have > structure comparison for equality (and non equality). The programming language I use most imposes a total order on all data, and it is extremely useful. On the other hand, it is quite wrong if the programmer is trying to think in abstract data-type terms. There may be concretely distinct records which represent the same abstract value. (Consider two stacks with the same active elements, but with different 'junk' elements which abstractly don't exist.) There are two objections which are particularly relevant to C: (1) Two records may be absolutely identical, but that does not mean that they should be regarded as the same. For example, suppose I have void p(FILE *f) { FILE g; /* This is legal */ FILE *h; g = *f; /* so is this */ assert(g == f); /* This isn't */ putc('a', f); assert(g != f); /* Illegal, but would succeed if legal */ } (2) In order to make structure equality work, you need equality for all the standard types *and* type constructors. In particular, you need 'union' equality too, because you can have a 'union' inside a 'struct'. But since C's unions are untagged, how can you tell which comparison to use? Exempli gratia: void q() { struct { union { float f; long i; } pun; } x, y; assert(sizeof x.pun.f == sizeof y.pun.i); x.pun.i = 0; /* 0 and 0.0 are usually identical, but */ y.pun.f = -0.0; /* is not the same bit pattern as 0.0 */ assert(x == y); /* should this succeed? */ } /* yes if float comparison, no if int */ Since we can't do it, it can't be the case that we should do it.