473,467 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Storing macro param?

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

Mar 20 '06 #1
7 1917
Imre wrote:
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?


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!!!
Mar 20 '06 #2

Phlip wrote:
Imre wrote:
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?


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


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

Mar 20 '06 #3
Imre wrote:
I'm trying to build a sort of reflection system...


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

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 20 '06 #4

Phlip wrote:
Imre wrote:
I'm trying to build a sort of reflection system...
And how much www.boost.org have you studied?


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

Imre

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


Mar 20 '06 #5
Imre wrote:
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


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
Mar 21 '06 #6
Luke H wrote:
#define MACRO_A(param) ( storedParam = (param) )
#define MACRO_B() ( do something with storedParam )
The OP meant a copy of param's source expression (its name). Not a copy of
its value.
As someone else said, using classes is probably a better solution.


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!!!
Mar 21 '06 #7

Hi

Thanks for the replies.

Phlip wrote:
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.
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.
#define MACRO_A(param) ( typedef param klass; )
#define MACRO_B() ( do something with klass )


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.

Mar 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
14
by: David M. Wilson | last post by:
Further to my last post here, I was playing some more with building a regex object and storing it somewhere for use internally by a function. I'm not happy with any of my solutions: # I don't...
20
by: Dead RAM | last post by:
Hey people, i'll try to keep this short ;) Here is what I want to type (or at least close too)... #define VER_BUILD 1 #define STR_VER_BUILD "VER_BUILD" But what happends is...
4
by: Daniel | last post by:
I need to build the maze board(like 2d array) using a tree. But I find that my buildTree function doesn't work. Could anyone give me some hints on debugging it? Thanks bool BuildTree(TreeNodePtr...
8
by: rgparkins | last post by:
Hi I am creating a sign-up process on a web site much like that of a wizard form. I have browsed many sites to look for examples of how to store the entry data, so that the user can go back and...
3
by: param | last post by:
Hi, I need to write a MACRO which helps to call different functions depending upon it's value say On or OFF. If its' value is ON the function1 should get called and if value is OFF function2...
1
by: Rich | last post by:
Hello, I can update a dataset from my client app using a dataAdapter.Updatecommand when I add parameter values outside of the param declaration. But If I add the param values inline with the...
2
by: QTR | last post by:
Hello, I discovered a strange XSLT behaviour on Weblo 8.1. Let me explain it (see at the end my example XSLT): I call a template (1) with one parameter (A) which calls another template (2) with...
5
by: per9000 | last post by:
Hi all, I want to create an encryption program and started thinking about not storing sensitive information in the memory since I guess someone might steal my computer an scan my memory. So I...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.