Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #30434

Re: Struct assignment

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.c
Subject Re: Struct assignment
Date 2013-01-20 22:08 -0500
Organization A noiseless patient Spider
Message-ID <kdibfc$d0q$1@dont-email.me> (permalink)
References (2 earlier) <simqs9-ue9.ln1@main.anatron.com.au> <kdehee$3ni$1@dont-email.me> <bturs9-mi4.ln1@main.anatron.com.au> <kdgtp3$7n1$1@dont-email.me> <hmgus9-9b5.ln1@main.anatron.com.au>

Show all headers | View raw


On 1/20/2013 8:02 PM, Russell Shaw wrote:
> On 21/01/13 01:08, Eric Sosman wrote:
>> On 1/19/2013 8:46 PM, Russell Shaw wrote:
>>> [... two "identical" struct types are incompatible ...]
>>>
>>> The problem is that if you #include the declaration of an anonymous
>>> struct sa from a separate translation unit, then the compiler complains
>>> about sb = sa.
>>
>> I don't understand this. A "translation unit" is all of
>> the source the compiler sees at one time: The original source
>> file, all the files and headers it #includes, all the files and
>> headers they in turn #include, all macros defined on compiler
>> command lines or whatever, ... If you #include a declaration,
>> that declaration is part of *the* translation unit; it is not
>> an interloper from "a separate translation unit."
>
> Precisely. Because of that problem, i could not see how declarations
> from separate translation units should be compatible, when including a
> header with the second declaration now becomes a part of the first
> translation unit anyway, thus making the point mute.

     (s/mute/moot/)

     That's what the special dispensation in 6.2.7 is for: It
says that "lexically identical" [my phrase] structs in separate
translation units are compatible.  You #include the header from
a source file that is part of translation unit A, and again from
a source in TU B, and you then know that the types in A and B
are compatible.  (As mentioned earlier, you don't even need to
#include the very same file in both TU's: you can #include a copy,
or you can copy-and-paste the text, or you can even re-type the
declaration from scratch.  As long as the two declarations in A
and B satisfy 6.2.7, the types are compatible.)

     In a sense, 6.2.7's special dispensation can be viewed as a
hack, a way to get around the limitations of tool sets.  Note the
precision of the language: The types in A and B are "compatible,"
not "the same."  They cannot be "the same" because there is no
UUID for types, no anchor that allows a compiler in California
and a compiler in Cameroon to cooperate -- especially if the two
compilers process their source files in different decades.  Yet
another way of looking at the issue is to realize that types have
no linkage (6.2.2): If they did, the special dispensation would
be unnecessary, but a whole lot of additional tool machinery
would be needed.  6.2.7 is a formalization of "We want separate
compilation, so we need a way for different compilers on different
days in different parts of the world to agree on compatible types,
without inventing a central universal type registry."

     But within a single TU the difficulties of cross-compiler
coordination vanish: It's one compiler on one day looking at one
batch of source, and it needs no special dispensation to figure
out that `struct foo' means the same thing every time it pops up
in a particular scope.  That's why 6.2.7's language doesn't cover
"lexically identical" declarations in a single TU: The need for
the hack doesn't exist, and there's some (smallish) benefit to
type safety in avoiding hacks where hacking isn't required.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Struct assignment Russell Shaw <rjshawN_o@s_pam.netspace.net.au> - 2013-01-19 23:09 +1100
  Re: Struct assignment Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-19 08:27 -0500
    Re: Struct assignment Russell Shaw <rjshawN_o@s_pam.netspace.net.au> - 2013-01-20 01:18 +1100
      Re: Struct assignment "BartC" <bc@freeuk.com> - 2013-01-19 14:59 +0000
      Re: Struct assignment Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-19 11:25 -0500
        Re: Struct assignment Russell Shaw <rjshawN_o@s_pam.netspace.net.au> - 2013-01-20 12:46 +1100
          Re: Struct assignment Bart van Ingen Schenau <bart@ingen.ddns.info.invalid> - 2013-01-20 09:20 +0000
          Re: Struct assignment Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-20 09:08 -0500
            Re: Struct assignment Russell Shaw <rjshawN_o@s_pam.netspace.net.au> - 2013-01-21 12:02 +1100
              Re: Struct assignment Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-20 22:08 -0500
                Re: Struct assignment James Kuyper <jameskuyper@verizon.net> - 2013-01-20 23:21 -0500
              Re: Struct assignment James Kuyper <jameskuyper@verizon.net> - 2013-01-21 08:23 -0500
                Re: Struct assignment Russell Shaw <rjshawN_o@s_pam.netspace.net.au> - 2013-01-22 13:33 +1100
                Re: Struct assignment Russell Shaw <rjshawN_o@s_pam.netspace.net.au> - 2013-01-23 19:53 +1100
      Re: Struct assignment Shao Miller <sha0.miller@gmail.com> - 2013-01-19 11:45 -0500
        Re: Struct assignment Russell Shaw <rjshawN_o@s_pam.netspace.net.au> - 2013-01-20 13:21 +1100
          Re: Struct assignment Shao Miller <sha0.miller@gmail.com> - 2013-01-20 01:06 -0500
            Re: Struct assignment Russell Shaw <rjshawN_o@s_pam.netspace.net.au> - 2013-01-20 18:03 +1100
  Re: Struct assignment glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-19 19:18 +0000
    Re: Struct assignment James Kuyper <jameskuyper@verizon.net> - 2013-01-19 15:22 -0500
      Re: Struct assignment Shao Miller <sha0.miller@gmail.com> - 2013-01-19 15:47 -0500
      Re: Struct assignment Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-19 17:56 -0500
        Re: Struct assignment Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-20 12:12 -0800
  Re: Struct assignment falk@rahul.net (Edward A. Falk) - 2013-01-19 23:41 +0000
    Re: Struct assignment glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-20 01:01 +0000
    Re: Struct assignment James Kuyper <jameskuyper@verizon.net> - 2013-01-19 20:53 -0500
      Re: Struct assignment Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-20 12:03 -0800

csiph-web