🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2551

Re: compatibility

#2551
From: Chris Torek
Date: Mon, 08 Apr 1996 16:20
60 lines
3340 bytes
(Anyone have any idea why the `subject' line was:
  Newsgroups: comp.lang.c,comp.lang.c++,comp.lang.c++.moderated,
	comp.lang.c.moderated
 ?  Not that this is a sensible set of newsgroups anyway.)

[Broken news software causes posts to moderated newsgroups to be re-wrapped
 in a normal mail header, which I don't always catch and edit out. -mod]

In article <4jtt2c$3eq@solutions.solon.com> someone whose name was
destroyed by the site's own news software asked about type compatibility
for:

>>>typedef struct { int a; int b; } *TA;
>>>typedef struct { int a; int b; } *TB;

In article <4k1qi3$5ig@solutions.solon.com> fred@genesis.demon.co.uk wrote:
>>Under ANSI C TA and TB have two distinct, incompatible types, they cannot
>>be directly assigned.

In article <4k5vop$9ub@solutions.solon.com> Jos A. Horsmeier <jos@and.nl>
answered:
>I beg to differ ... [see section 6.1.2.6]
>The only thing that (still) bothers me, is the phrase 'two separate
>translation units' ... Compiler checks don't span translation units ...
>Does anyone has any idea what this phrase implies and what is meant
>by it?

The rule in 6.1.2.6 (3.1.2.6 in my ANSI edition) defines what is
usually referred to as `structural equivalence'.  Many languages
define type-compatibility in terms of either `name' or `structural'
equivalence.  Of these, name equivalence is the more restrictive:
it says that two types T1 and T2 are the same if and only if T1
and T2 are the same type *name*.  Structural equivalence is more
generous, saying that T1 and T2 are equivalent if everything `inside'
types T1 and T2 are equivalent (recursively), i.e., T1 and T2 are
the same type *layout*.

C uses `name equivalence', but C also has separate compilation,
and does not require that type-names persist outside a compilation
unit (`source file' after #include processing).  This leads to an
obvious problem: there are no names with which to demand equivalency.
Enter 6.1.2.6: since there are no names, the C standard calls for
structural equivalence.

The result is that TA and TB are compatible only in separate
compilation units.  Within a single unit, they are incompatible.
Any C compiler that fails to emit a diagnostic for the original
sample program (not shown here) is not conformant.

Note that C's definition of a `type name' is a little unusual.  In
most languages that use name equivalence, the language's `typedef'
construct (however it is spelled) makes a new name, and hence a new
type.  (I understand Ada lets you choose whether it is a new type or
a new name for an old type.)

The rules could well be different for C++ (I am no C++ expert),
since C++ demands more from the linker.
-- 
In-Real-Life: Chris Torek, Berkeley Software Design Inc
El Cerrito, CA	Domain:	torek@bsdi.com	+1 510 234 3167
Technical Details
Message-ID: <4kbebh$eid@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4jtt2c$3eq@solutions.solon.com> <4k1qi3$5ig@solutions.solon.com> <4k5vop$9ub@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: Chris Torek <torek@elf.bsdi.com>
Newsgroups: comp.lang.c.moderated
Subject: Re: compatibility
Date: 8 Apr 1996 11:20:01 -0500
Organization: Berkeley Software Design, Inc.
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4kbebh$eid@solutions.solon.com>
References: <4jtt2c$3eq@solutions.solon.com> <4k1qi3$5ig@solutions.solon.com> <4k5vop$9ub@solutions.solon.com>
Reply-To: torek@bsdi.com
NNTP-Posting-Host: solutions.solon.com