typedefs and structs

linas linas at austin.ibm.com
Wed Nov 9 10:36:58 EST 2005


On Tue, Nov 08, 2005 at 12:18:42PM +1100, Neil Brown was heard to remark:
> 
> Another reason  for not using typedefs is that if you do, and you want
> to refer to the structure in some other include file, you have to
> #include the include file that devices the structure.
> If you don't use typedefs, you can just say:
> 
>    struct foo;
> 
> and the compiler will happily wait for the complete definition later
> (providing it doesn't need the size in the meanwhile). 

Yes, this is the "forward declaration" problem I was refering to. 
Its unavoidable if structs have circular references to each other.

However, I've learned, by experience, several things by trying to
eliminate such forward declarations (and the related #include hell):

-- Its really, really hard, and right in the middle, you think,
   "gosh this is a stupid idea, why am I bothering?"

-- When you get done, you think: "wow this new code structure
   is so insanely better than the old code! The guy who wrote
   the old code should be hung from a yardarm as an example!"

So having a mechanism that prevents coders from declaring 
"struct foo" whenever they feel like it can be a good thing.
Of course, your milage may vary.

--linas



More information about the Linuxppc64-dev mailing list