From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6486 invoked by alias); 22 Nov 2002 18:11:37 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 6478 invoked from network); 22 Nov 2002 18:11:36 -0000 Received: from unknown (HELO hotmail.com) (64.4.8.123) by sources.redhat.com with SMTP; 22 Nov 2002 18:11:36 -0000 Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC; Fri, 22 Nov 2002 10:11:36 -0800 X-Originating-IP: [66.170.45.178] From: "Buddy Lott" To: Subject: RE: CPP (preprocessor) quandry Date: Fri, 22 Nov 2002 10:11:00 -0000 Message-ID: <583E84A77A2FCB4F967E6CD9B0CA4AF74E4906@nt-server.kreuter> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal In-Reply-To: <4.3.2.7.2.20021122114945.00b5eb00@iplan-mn.corp.adobe.com> Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 X-OriginalArrivalTime: 22 Nov 2002 18:11:36.0013 (UTC) FILETIME=[984FB7D0:01C29252] X-SW-Source: 2002-11/txt/msg00166.txt.bz2 Does it have to work in C or just C++? It sounds like you are trying to simulate namespaces in c or c++. Have you you tried something like this #define NAMESPACE struct y { #define ENDNAMESPACE } Then in the code you would do: NAMESPACE(buddy) Typedef ... ENDNAMESPACE This would also allow you to "customize" name spaces to the whatever the compiler supports. > -----Original Message----- > From: Eljay Love-Jensen [mailto:eljay@adobe.com] > Sent: Friday, November 22, 2002 1:06 PM > To: Buddy Lott; gcc-help@gcc.gnu.org > Subject: RE: CPP (preprocessor) quandry > > Hi Buddy, > > The macro wraps the data type in a struct, so as to reduce namespace > pollution. > > In particular, it avoids... > typedef enum {a,b,c} EnumWhatever; > ...such that a, b, c and EnumWhatever now reside in the global (or > current) > namespace. > > I have found a GCC-centric solution. But (unfortunately) the code needs > to > run on other compilers. > > #define $MkEnum(name$, enum$...) \ > struct name$ {\ > typedef enum { enum$ } Type;\ > Type m;\ > explicit name$ (Type in) : m(in) { }\ > operator Type () const { return m; };\ > } > > NOTE: local convention dictates that $ is used to prefix macros (function > macros or simple substitution), and $ suffix for the macro's > parameters. The $ is digestable by all the preprocessors / compilers we > care about. Less chance of spurious macro vs. local identifier collision. > > Something similar to this trick was proposed / introduced by Stroustrup in > his C++ Programming Language (3rd and Special editions). Not the macro > part, but the wrapping of a POD in a struct to have somewhat stronger type > safety. Getting that stronger type safety is the ultimate purpose of the > macros; the macros serve to reduce repetitive typing -- and thus reduce > mistakes and simplify maintenance. > > A better solution would be to use a language that has this facility > native. Stroupstrup told me (paraphrased), "No one is stopping you from > writing your own language." (I think he was a little tired of hearing a > bazillion requests and suggestions for how to "improve" C++.) > > Sincerely, > --Eljay > (I hate macros) > > > At 12:43 PM 11/22/2002 -0500, Buddy Lott wrote: > >I can't think of a way to get around this, but maybe if you explain what > >purpose this solves (in other wordes, why use the macro) I could think > >of a way to accomplish the same thing. > > > >One what that comes to mind: > > > >Typedef enum { a,b,c} EnumWhatever; > >MkFoo(Whatever)