|
Prev: -1%N = -1
Next: communication between classes
From: Oncaphillis on 17 Apr 2008 02:39 Hi all, Working with g++ 3.* I have to include 3rd party C-headers which tend to use the same name for typedefs and variables e.g: <snip> /* test.h */ typedef int bar; struct foo { bar bar; }; </snip> ....which seems to be valid C-code. When I include these with: <snip> extern "C" { #include <test.h> }; </snip> g++ chokes on these identical IDs. I help myself with including #ifdef __cplusplus typedef int bar_t; struct foo { bar_t bar; }; #else typedef int bar; struct foo { bar bar; }; #endif ...which is cumbersome and ugly. 1. Is this behavior part of the standard or a defect of GNU C++ ? 2. Is there a more elegant (may be even portable) way to compile these C-headers without having to change them. Thanks you for your attention O. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: red floyd on 17 Apr 2008 21:13 Oncaphillis wrote: > Hi all, > > Working with g++ 3.* I have to include 3rd > party C-headers which tend to use the same > name for typedefs and > variables > > e.g: > > <snip> > /* test.h > */ > > typedef int bar; > > struct foo { > bar bar; > }; > Find the guy who wrote that code, and shoot him. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Marco Manfredini on 18 Apr 2008 08:15 Oncaphillis wrote: [does not work:] > typedef int bar; > > struct foo { > bar bar; > }; > 1. Is this behavior part of the standard or a defect I think this is standard behavior. There is a rule for class-scope declarations in 3.3.6 which basically says, that a declaration is only valid, if it yields the same declaration after seeing all of the class. This prohibits redefining a symbol after using it. > > 2. Is there a more elegant (may be even portable) way > to compile these C-headers without having to change > them. Not really. It might be a bit more elegant not to duplicate everything in the headers, but just to wrap all occurrences of a typename in a macro: #ifdef __cplusplus__ #define TYPESYM(X) X##_t #else #define TYPESYM(X) #endif typedef int TYPESYM(bar); struct TYPESYM(foo) { TYPESYM(bar) bar; }; -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Oncaphillis on 18 Apr 2008 08:20 red floyd wrote: > Oncaphillis wrote: >> Hi all, >> >> Working with g++ 3.* I have to include 3rd >> party C-headers which tend to use the same >> name for typedefs and >> variables >> >> e.g: >> >> <snip> >> /* test.h >> */ >> >> typedef int bar; >> >> struct foo { >> bar bar; >> }; >> > > Find the guy who wrote that code, and shoot him. > Ok.. that was my plan B. But since Mom and Dad always told me that it is something you shouldn't do I tried to find something else. So is this kind of typdef and variable name mix up forbidden by C++ or is it just g++ that barks at me ? Cheers O. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Pages: 1 Prev: -1%N = -1 Next: communication between classes |