Typeof operator in C (Re: An Interesting View of "Strong" Vs. "Weak

Lynn Lively Lynn.Lively at p4694.f506.n106.z1.fidonet.org
Wed Jan 17 05:47:53 AEST 1990


In an article of <15 Jan 90 03:21:53 GMT>, peter at ficc.uu.net (Peter da Silva)   
writes:

 Pd>From: peter at ficc.uu.net (Peter da Silva)
 Pd>Date: 15 Jan 90 03:21:53 GMT
 Pd>Organization: Xenix Support, FICC
 Pd>Message-ID: <JH216U4ggpc2 at ficc.uu.net>
 Pd>Newsgroups: comp.lang.c
 Pd>
 Pd>> C has no run-time "type" type.  The type value of a typeof construct
 Pd>> exists only in the mind of the compiler.
 Pd>
 Pd>Yes, I know that. This discussion originated with a suggestion that 
 Pd>such
 Pd>a type be added to the language. It's kind of hard to see how the 
 Pd>semantics
 Pd>of such a thing could be standardised. What could one do with it at 
 Pd>runtime?
 Pd>
 Pd>The discussion about a typeof operator similar to the one in GCC 
 Pd>developed
 Pd>from that.

Peter,
     Excuse me if I'm answering out of turn, but wouldn't the above require 
a change in the basic philosophy of memory mapping in C? Seems to me in order
to implement such you would need a preamble in front of every field declaring
its 'type' and probably its 'size'. You would also need a dynamic symbol table   
 for user defined types via 'typedef'. This would cause a very fundemental    
change in the way you approached a problem in C and would IMHO cause more harm   
 than good as it would be alow more difficult to do such things as read in an
array of 'int's and then turn around and step thru the array with a 'char'    
pointer. Fields would no longer be layed out in contigious memory, passed
parameters to functions would need the preamble also causing alot of overhead    
for very little return. Seems to me the 'runtime type' is a good job for    
'union', don't you think?
Something like this.
typedef struct
  {
    int type;                  /* Could also use an 'enum' here */
    union
      {
        [various possible types]
      } u;
  } RT_TYPE;
Although a compile time 'typeof' would need less overhead (the compiler keeps   
this information anyway) and would have most of the advantages mentioned   
previously. I would very much like to see it, if it didn't take away from the   
power of C in other areas.
 Your Servant,
      Lynn



More information about the Comp.lang.c mailing list