duplicate typedefs and system headers ?

Christoph Mallon christoph.mallon at gmx.de
Wed Jan 28 03:47:48 PST 2009


Luigi Rizzo schrieb:
> On Tue, Jan 27, 2009 at 11:59:15PM +0100, Christoph Mallon wrote:
>> Luigi Rizzo schrieb:
>>> In a recent commit tp sbin/ipfw, in an attempt to avoid
>>> a large set of header deependencies, i put a 'forward typedef'
>>> declaration in a common header, duplicating something that is in a
>>> system header:
>>>
>>> ipfw2.h:	typedef struct _ipfw_insn ipfw_insn;
>>>
>>> netiinet/ip_fw2.h
>>> 	typedef struct  _ipfw_insn {    /* template for instructions */
>>>        	...
>>> 	} ipfw_insn;
>> This just begs to break, if somebody decides to change the name of the 
>> underlying type of the typedef.
> 
> Fortunately this is almost impossible -- the system header is
> basically frozend and if I could change it I would remove the
> typedef in the first place.
> 
> I am more worried of compatibility with different compiler.
> 
> A workaround that seems to work is putting forward declarations
> just for "struct _ipfw_insn" and use it (instead of the typedef
> name) in the place that does not include netiinet/ip_fw2.h
> 
> Would this be better/more compliant with standards -- i.e.,
> is the following code correct ? gcc seems to have no objections.
> 
> 	struct foo;
> 	int a(struct foo *);
> 	typedef struct foo bar;
> 	int a(bar *x) { ... }

What is the problem with just including the correct header? This would 
be The Right Thing(tm). Everything else is just an ugly hack.


More information about the freebsd-current mailing list