Snis Pilbor wrote On 07/18/06 16:22,:
As long as we're on the topic of typedef, I may as well posit a
question I've been curious about for some time. Do typedefs fall into
the same precompiler realm as things like #define? It seems to me like
they ought to, since essentially they are just a more sophisticated
#define? In otherwords if you went through a program, took out all the
typedefs and manually filled them in everywhere, would it actually
change the final machine code produced?
No. Macro processing operates on the source code,
before types exist at all and before keywords like typedef
have any significance.
On a more concrete level, consider this fragment:
typedef double Matrix[3][3];
Matrix x;
.... and try to rewrite it using a macro
#define Matrix /* what goes here? */
Matrix x;
.... to get the same effect.
Also tangentially related, what is the deal with the struct keyword:
it seems to me like any structure which matters, is going to be
typedefd to avoid typing "struct" every single time such an object is
declared. If I'm going to be using struct widget's extensively
throughout my code, I dont want to type "struct" a hundred million
times, im going to make the original structure a struct WIDGET and use
a typedef so everywhere i need a "struct WIDGET" I can simply type
"widget". I can hardly imagine, even in the most bizarre scenarios,
why it would actually be desirable to have to type that extra keyword
every five lines--- so why have it in the first place? It seems to
me there's no reason C shouldn't be able to detect that the user means
"struct widget *p" when they type "widget *p"...
This is an unimportant matter of style -- and like most
other unimportant matters, elicits more vociferous opinions
than the things that really count! Some people feel that
writing `struct widget' is an insignificant burden, and has
the benefit of reminding the reader that the thing is in
fact a struct. If the struct-ness were hidden behind the
bland veneer of a typedef, mystifications might ensue (for a
recent example, see the "error: invalid operands to binary &"
thread).
You (and I, as it happens) are in the other camp. I find
the code reads more smoothly when a widget is just a Widget
(or a Wadget, or Boff), a single nounish word instead of a
two-word phrase in which the first word carries practically
no information I couldn't guess anyhow.
One thing I detest, though, is the use of typedef to create
aliases for pointer types. I grimace when I see
typedef struct { ... } Widget, *WidgetPtr;
In this I am inconsistent: I prefer to cover up struct-ness but
want pointer-ness to be out in the open. Pointer-ness "feels"
like a more basic quality than struct-ness; I want to know
whether I've got hold of the thing itself or just a link to it.
When I see `Widget *w' in the code I have a much better notion
of what's going on than when I see `WidgetRef w'.
I'll break my "no pointer typedefs" rule to avoid nasty-
looking situations such as commonly arise when you're casting
function pointer types back and forth, but I'll very seldom
create a typedef alias for a data pointer type.