Typeof operator in C (Re: An Interesting View of "Strong" Vs. "Weak" Typing)
Blair P. Houghton
bph at buengc.BU.EDU
Tue Jan 16 07:46:34 AEST 1990
In article <-121H63ggpc2 at ficc.uu.net> peter at ficc.uu.net (Peter da Silva) writes:
>[ Note, I frequently leave off attributions where they're not needed,
> on the theory that you're less likely to flame a discussion if you
> don't know the principals. Is this to be a discussion of ideas or
> of personalities? ]
INAPPROPRIATE FLAME ON!
Perhaps then you should instead leave off your .sig, since you
are about the only person on the whole of the net whom I'm likely
to flame on sight.
Credit those who wrote their words, especially when you agree
with them, and stop being so damn supercilious about the
purpose of the net.
INAPPROPRIATE FLAME OFF...
>I said... what would typeof() return for some complex structure. This
>being the typeof that returns a first-class object, not the typeof from
>GCC
>
>> `struct _T_aaa' or something similar.
>
>You mean it would return a string? That's an interesting idea. I would have
>expected it to return a small integer of some sort. You know: _T_INT,
>_T_CHAR, _T_DOUBLE.
Sorry, let me clarify:
You had asked what the meaning of the typeof expression would be
if it were applied to this rather complicated struct you had
devised; my response was that there really was nothing to worry
about, since a struct can be recognized from its struct-identifier
(the optionial name that comes right after the word 'struct'
and right before the optional left-brace in the declaration)
and hence the compiler would only have to take the typeof
expression, evaluate it, and then replace the expression with
its value, which would be the type-identifier. So, in the
case where you don't think up the identifier yourself, the
compiler would make up a unique one and use it.
typeof should return a type identifier, rather than a data value.
It's obviously useful in situations where you need to do a cast
or a declaration but don't know the type of the object you're
casting or creating. Specifically, it fits perfectly in that
swap-macro you devised:
#define swap(a,b) {typeof a tmp; tmp = a; a = b; b = tmp}
where the expansion of
int x,y;
...
swap(x,y);
gives
{int tmp; tmp = x ...
where it wouldn't work if the typeof operator returned anything
that was data, even integer constants or strings.
Returning to the struct example, I was using `_T_aaa' as the
sort of nebulous name a compiler might choose as the identifier
of an otherwise un-identifier'ed struct, and then defining
struct {
...
} x;
would mean that `typeof x' would return this thing from the
symbol table that was the type-identifier for the variable x,
which might come out in a debugger something like
struct _T_aaa
--Blair
"As long as we're making up operators,
if ( dollarsin pocket != 0 )..."
More information about the Comp.lang.c
mailing list