473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Prevent/control macro prescan

Is there a way to prevent macro prescanning?
I have the following problem:

#define APE 4
#define GET_ANIMAL_NAME(a, b) ANIMAL_##b##_##a
#define CREATE_ANIMAL_ENTRY(a, b) GET_ANIMAL_NAME(a, b) = (a + b)

enum animals
{
CREATE_ANIMAL_ENTRY(2, APE)
};

this expands to
ANIMAL_4_2 = (2 + 4)
but I'd like to prevent the prescanning (at least in GET_ANIMAL_NAME)
to expand this to:
ANIMAL_APE_2 = (2 + 4)

I know it works when I move the content of GET_ANIMAL_NAME to
CREATE_ANIMAL_ENTRY - but I need the name macro in other files, too.
Jun 27 '08 #1
4 1587
LCID Fire wrote:
Is there a way to prevent macro prescanning?
I have the following problem:

#define APE 4
#define GET_ANIMAL_NAME(a, b) ANIMAL_##b##_##a
#define CREATE_ANIMAL_ENTRY(a, b) GET_ANIMAL_NAME(a, b) = (a + b)

enum animals
{
CREATE_ANIMAL_ENTRY(2, APE)
};

this expands to
ANIMAL_4_2 = (2 + 4)
but I'd like to prevent the prescanning (at least in GET_ANIMAL_NAME)
to expand this to:
ANIMAL_APE_2 = (2 + 4)
Normally that's a _feature_. In your case, use

#define CREATE_ANIMAL_ENTRY(a, b) ANIMAL_##b##_##a = (a + b)

--
Thad
Jun 27 '08 #2
On May 18, 8:33*pm, LCID Fire <lcid-f...@gmx.netwrote:
Is there a way to prevent macro prescanning?
I have the following problem:

#define APE * * * * * * 4
#define GET_ANIMAL_NAME(a, b) ANIMAL_##b##_##a
#define CREATE_ANIMAL_ENTRY(a, b) GET_ANIMAL_NAME(a, b) = (a + b)

enum animals
{
CREATE_ANIMAL_ENTRY(2, APE)

};

this expands to
ANIMAL_4_2 = (2 + 4)
but I'd like to prevent the prescanning (at least in GET_ANIMAL_NAME)
to expand this to:
ANIMAL_APE_2 = (2 + 4)

I know it works when I move the content of GET_ANIMAL_NAME to
CREATE_ANIMAL_ENTRY - but I need the name macro in other files, too.
You may have to split APE into two identifiers. Here I've used APE and
APE2:

#define APE2 4
#define GET_ANIMAL_NAME(a, b) ANIMAL_##b##_##a
#define CREATE_ANIMAL_ENTRY(a, b) GET_ANIMAL_NAME(a, b) = (a + b##2)

--
Bartc
Jun 27 '08 #3
On May 19, 3:57*am, Thad Smith <ThadSm...@acm.orgwrote:
Normally that's a _feature_. *In your case, use

#define CREATE_ANIMAL_ENTRY(a, b) ANIMAL_##b##_##a = (a + b)
As I stated it makes less sense to use this line because I want a
single point in code to have this logic implemented (and use it in
other files).
To me the "feature" sound rather defunct when there is just one way
and no way around it.
Jun 27 '08 #4
LCID Fire wrote:
Is there a way to prevent macro prescanning?
I have the following problem:

#define APE 4
#define GET_ANIMAL_NAME(a, b) ANIMAL_##b##_##a
#define CREATE_ANIMAL_ENTRY(a, b) GET_ANIMAL_NAME(a, b) = (a + b)

enum animals
{
CREATE_ANIMAL_ENTRY(2, APE)
};

this expands to
ANIMAL_4_2 = (2 + 4)
but I'd like to prevent the prescanning (at least in GET_ANIMAL_NAME)
to expand this to:
ANIMAL_APE_2 = (2 + 4)

I know it works when I move the content of GET_ANIMAL_NAME to
CREATE_ANIMAL_ENTRY - but I need the name macro in other files, too.
I failed to note you last sentence. One way to do this is with compound
macros in which a single macro contains multiple fields. Here you want to
be able to select either the name, APE, or an associated value 4. Here is
one, admittedly ugly, way to do this:

#define APE (APE,4) /* compound definition */

/* ugly macros */
#define ARG2_1(a,b) a
#define ARG2_2(a,b) b
#define ANIMAL_NAME(a) ARG2_1 a
#define ANIMAL_NUM(a) ARG2_2 a
#define GET_ANIMAL_NAME3(a, b) ANIMAL_## b ##_## a
#define GET_ANIMAL_NAME2(a, b) GET_ANIMAL_NAME3(a,b)
#define GET_ANIMAL_NAME(a, b) GET_ANIMAL_NAME2(a, ANIMAL_NAME(b))
#define CREATE_ANIMAL_ENTRY(a, b) GET_ANIMAL_NAME(a, b) = \
(a + ANIMAL_NUM(b))

enum animals
{
CREATE_ANIMAL_ENTRY(2, APE),
GET_ANIMAL_NAME(2,APE),
ANIMAL_NAME(APE),
ANIMAL_NUM(APE)
};

This expands to (with my compiler)

13: enum animals
14: {
15: ANIMAL_APE_2 = (2 + 4),
16: ANIMAL_APE_2,
17: APE,
18: 4
19: };

I have used this technique to assign several values with a single name and
use macros to extract the desired component.

--
Thad
Jun 27 '08 #5

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

Similar topics

1
by: Deano | last post by:
Hi, I need to disable a command button at some point. Easy enough. Initially in design view it is set as enabled. Now once I disable it, how can I ensure that when the app is restarted, the...
3
by: Tony Maresca | last post by:
Hi. I have a class derived from a UserControl, that I want to allow others to derive controls from. I don't want them to design the base class (which is derived from a UserControl). I know that...
24
by: Charles Law | last post by:
When I click a button I don't want the click event to fire. Is this possible? In fact, what I would really like is to be able to intercept the click event, perform some action, and then prevent...
16
by: MLH | last post by:
If I give someone a runtime app, they can open the database window by pressing the F-11 key. How to prevent???
5
by: EqDev | last post by:
I have a class that is a control derived from UserControl. I want to use serialization and deserialization with this calss but I get an exception "Cannot serialize member...
5
by: NEWSGROUPS | last post by:
I have some users that continuously end task on my database when a search takes to long. In turn corrupting the application. Is there any way to prevent this? The Microsoft KB for Methods That Can...
3
by: dave k | last post by:
I often use the Control and Apostrophe keys to enter the value from the same field of the previous record in a form. I would prefer to use a mouse and the double click function. Can anyone help...
5
by: Vincent | last post by:
I have a bound listbox control on one of my forms that has an associated afterupdate event. When this event is invoked, I lock the control to prevent the user from clicking on it more than once....
4
by: eighthman11 | last post by:
this is probably very simple but I don't know. Is there a way to prevent two people from signing on to an Access Application using the some login information???? I can't have two people on the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.