From: "Buddy Lott" <buddy_lott@hotmail.com>
To: <gcc-help@gcc.gnu.org>
Subject: RE: CPP (preprocessor) quandry
Date: Fri, 22 Nov 2002 10:11:00 -0000 [thread overview]
Message-ID: <583E84A77A2FCB4F967E6CD9B0CA4AF74E4906@nt-server.kreuter> (raw)
In-Reply-To: <4.3.2.7.2.20021122114945.00b5eb00@iplan-mn.corp.adobe.com>
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)
next prev parent reply other threads:[~2002-11-22 18:11 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-10-29 14:51 Problem compiling GCC 3.2 on i686-pc-linux: "not enough room for program headers" Francis Hwang
2002-10-29 17:11 ` Ben Elliston
2002-10-30 9:55 ` Francis Hwang
2002-10-30 17:02 ` Ben Elliston
2002-10-31 11:16 ` Francis Hwang
2002-11-22 9:17 ` CPP (preprocessor) quandry Eljay Love-Jensen
2002-11-22 9:43 ` Buddy Lott
2002-11-22 10:06 ` Eljay Love-Jensen
2002-11-22 10:11 ` Buddy Lott [this message]
2002-11-22 10:32 ` Eljay Love-Jensen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=583E84A77A2FCB4F967E6CD9B0CA4AF74E4906@nt-server.kreuter \
--to=buddy_lott@hotmail.com \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).