Connecting Tech Pros Worldwide Help | Site Map

Storing macro param?

Imre
Guest
 
Posts: n/a
#1: Mar 20 '06
Hi

Is it somehow possible to memorize / store a macro parameter so that we
can reuse it in later macro expansions?

For example, let's suppose we have a MACRO_A(param). I'd like to store
param, so that when the user later invokes MACRO_B, they don't have to
supply the same param again, instead MACRO_B would use the param stored
by MACRO_A.

The obvious solution would be to #define STORED_PARAM param in MACRO_A,
but of course, that doesn't work. Any workarounds?

Thanks,

Imre

Phlip
Guest
 
Posts: n/a
#2: Mar 20 '06

re: Storing macro param?


Imre wrote:
[color=blue]
> Is it somehow possible to memorize / store a macro parameter so that we
> can reuse it in later macro expansions?
>
> For example, let's suppose we have a MACRO_A(param). I'd like to store
> param, so that when the user later invokes MACRO_B, they don't have to
> supply the same param again, instead MACRO_B would use the param stored
> by MACRO_A.
>
> The obvious solution would be to #define STORED_PARAM param in MACRO_A,
> but of course, that doesn't work. Any workarounds?[/color]

C++ also supports classes and objects. They make that technique trivially
easy. Why can't you use them?

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


Imre
Guest
 
Posts: n/a
#3: Mar 20 '06

re: Storing macro param?



Phlip wrote:[color=blue]
> Imre wrote:
>[color=green]
> > Is it somehow possible to memorize / store a macro parameter so that we
> > can reuse it in later macro expansions?
> >
> > For example, let's suppose we have a MACRO_A(param). I'd like to store
> > param, so that when the user later invokes MACRO_B, they don't have to
> > supply the same param again, instead MACRO_B would use the param stored
> > by MACRO_A.
> >
> > The obvious solution would be to #define STORED_PARAM param in MACRO_A,
> > but of course, that doesn't work. Any workarounds?[/color]
>
> C++ also supports classes and objects. They make that technique trivially
> easy. Why can't you use them?[/color]

I'm trying to build a sort of reflection system, that includes having
some property definitions between a BEGIN_CLASS and an END_CLASS macro.
The property definitions also use macros, to save some work (without
macros, several functions would need to be implemented manually). The
problem is, that inside the property macros, I'd need to be able to
reference the host class.
(Actually I have a half-solution, but that's not usable if we're
creating the reflection block for a class template instead of a normal
class.)

Imre

Phlip
Guest
 
Posts: n/a
#4: Mar 20 '06

re: Storing macro param?


Imre wrote:
[color=blue]
> I'm trying to build a sort of reflection system...[/color]

And how much www.boost.org have you studied?

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Imre
Guest
 
Posts: n/a
#5: Mar 20 '06

re: Storing macro param?



Phlip wrote:[color=blue]
> Imre wrote:
>[color=green]
> > I'm trying to build a sort of reflection system...[/color]
>
> And how much www.boost.org have you studied?[/color]

Well, some... Maybe not enough.
Which boost lib do you recommend studying in this case?

Imre
[color=blue]
>
> --
> Phlip
> http://www.greencheese.org/ZeekLand <-- NOT a blog!!![/color]

Luke H
Guest
 
Posts: n/a
#6: Mar 21 '06

re: Storing macro param?


Imre wrote:[color=blue]
> Hi
>
> Is it somehow possible to memorize / store a macro parameter so that we
> can reuse it in later macro expansions?
>
> For example, let's suppose we have a MACRO_A(param). I'd like to store
> param, so that when the user later invokes MACRO_B, they don't have to
> supply the same param again, instead MACRO_B would use the param stored
> by MACRO_A.
>
> The obvious solution would be to #define STORED_PARAM param in MACRO_A,
> but of course, that doesn't work. Any workarounds?
>
> Thanks,
>
> Imre
>[/color]

Well, I wouldn't normally recommend doing this, but it is possible.
The way to do it is:

int storedParam; // a global var.

#define MACRO_A(param) ( storedParam = (param) )
#define MACRO_B() ( do something with storedParam )

of course this means that MACRO_B will only use the last value stored by a call
to MACRO_A, and that you can only use the macros on one type.

As someone else said, using classes is probably a better solution. Templates
can replace macros in many cases.

Good luck

- Luke
Phlip
Guest
 
Posts: n/a
#7: Mar 21 '06

re: Storing macro param?


Luke H wrote:
[color=blue]
> #define MACRO_A(param) ( storedParam = (param) )
> #define MACRO_B() ( do something with storedParam )[/color]

The OP meant a copy of param's source expression (its name). Not a copy of
its value.
[color=blue]
> As someone else said, using classes is probably a better solution.[/color]

The root problem (besides C++'s poor support for reflection) is C++ doesn't
make classes into first-class objects. In languages that do, you generally
don't need extra baggage like templates and macros.

#define MACRO_A(param) ( typedef param klass; )
#define MACRO_B() ( do something with klass )

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


Imre
Guest
 
Posts: n/a
#8: Mar 27 '06

re: Storing macro param?



Hi

Thanks for the replies.

Phlip wrote:[color=blue]
> The root problem (besides C++'s poor support for reflection) is C++ doesn't
> make classes into first-class objects. In languages that do, you generally
> don't need extra baggage like templates and macros.[/color]

It's a bit more general than that. In fact, what I'd really need is the
ability to store any preprocessor token, regardless of what it really
is; the name of a type, or a variable, or whatever.
[color=blue]
> #define MACRO_A(param) ( typedef param klass; )
> #define MACRO_B() ( do something with klass )[/color]

There are two problems with this solutions.
1. It can only be used once in a translation unit. You can't redefine
or undef a typedef. This can be worked around using some template
tricks though.
2. Sometimes I'd need to store things other then a typename, for
example, the argument list of a class template. Now as far as I know,
there's nothing in C++ that you can assign a class template arg list
to. I think this could only be done with some kind of preprocessor
support.

Anyway, it's not really that big a problem. In the worst case, the user
will have to #define some stuff before using these macros. It's a bit
inconvenient and ugly, but well, few things in the world are perfect.

i.

Closed Thread


Similar C / C++ bytes