Typeof operator in C (Re: An Interesting View of "Strong" Vs. "Weak" Typing)
Andrew P. Mullhaupt
amull at Morgan.COM
Sun Jan 14 09:44:53 AEST 1990
In article <-K016ODxds13 at ficc.uu.net>, peter at ficc.uu.net (Peter da Silva) writes:
> > This last sentence bothers me. It would be quite simple to implement
> > a C operator such as "(typeof) x" that returns some representation of
> > the type of the variable x.
>
> I think this would be a worthwhile innovation, and one that's as easy
> to implement as sizeof. It wouldn't return a value, but would be used
> anywhere a type could be used.
>
Damn right it's useful. You can write concurrent assignment in
general using ANSI standard C without this trick, but you need to
go through a lot of alignment pain and type-defeating function calls
to get there. On some systems, you can get it all done for about a
two or threefold performance penalty. On the RISC chips, you're
totally nailed for a factor on the order of 30 (THIRTY).
GNU C (gcc) has this little trick, and the performance penalty
evaporates. but you don't even have to use the typeof function (?)
(It seems a little bit different than most C functions). You can
define a temporary variable for the value in say, a variable x by
typedef _type_of_x (x); _type_of_x temp_x = x;
Is there a down side to these facilities? (I.e. why didn't people
put 'em in ANSI? I'd really like to use GNU C, but it ain't likely
to show up in DOS or OS/2 this year, (or is it?)).
Later,
Andrew Mullhaupt
More information about the Comp.lang.c
mailing list