Typeof operator in C (Re: An Interesting View of "Strong" Vs. "Weak" Typing)
Blair P. Houghton
bph at buengc.BU.EDU
Tue Jan 16 08:19:51 AEST 1990
In article <JH216U4ggpc2 at ficc.uu.net> peter at ficc.uu.net (Peter da Silva) writes:
FLAMES, AGAIN, SORRY:
[There you go again, Peter, pretending you know better than the rest of the
planet whether we should be able to see if you're flaming someone who
probably knew what he was talking about before you butchered his articles
and whom we can now not contact to ask more about their ideas because we
find yours repugnant...come on, are you that paranoid?]
FLAMES, AGAIN, DONE, I WISH...
>Someone else (whom peter doesn't respect) wrote:
>> C has no run-time "type" type. The type value of a typeof construct
>> exists only in the mind of the compiler.
>
>Yes, I know that. This discussion originated with a suggestion that such
>a type be added to the language. It's kind of hard to see how the semantics
>of such a thing could be standardised. What could one do with it at runtime?
I get it.
Okay, so you want something that turns the type of an object
into data.
A `valoftypeof' operator?
(Of course, it would be called simply 'typeof' by the ANSI committee,
noting that it would have the two distinct meanings -- return a
type-specifier or return a data representation of the type-specifier --
depending on whether it appeared in a value- context or not...)
If it returns some integer, what does that integer mean,
and what would you do with it?
Obviously you're going to compare or display it, at least.
I thought at first that returning a string with at least
the string representation of the type-specifier would be
useful, but then you get into the usual problems of
defining string-constants and comparing them, but there
are mechanisms for that (strcmp(), et al), so it may be
sufficient to do so.
However, what if there's a gaffe and, say, two variables
of the same type are defined with separate declarations
neither of which carries an identifier? e.g.:
struct {
int gobble;
} dee;
struct {
int gobble;
} gook;
Obviously dee and gook are the same type, but it would be a
wise (and damn slow) compiler indeed that could figure that
out and assign the same internal type-specifier to them both.
And it would be wrong for
valoftypeof dee == valoftypeof gook
not to be 1.
So it would have to be either that the compiler would have
to match new types (which it can do anyway, or it wouldn't
be able to assign the value of dee to gook without complaining)
or the valoftypeof operator would have to return the entire
declaration less the variable identifier.
--Blair
"It still says,
'error: dollarsin pocket: concept undefined'"
More information about the Comp.lang.c
mailing list