Macro substitutions

J

James S. Singleton

I have the following macro:

#define ABC(x) f((x) + 1)

Is it possible to get the preprocessor to replace lines like

x = g(y) + ABC(z/3) ;

with

x = g(y) + f((z/3) + 1) ;

but leave lines like

x = h->ABC(w) ;

alone?
 
A

Artie Gold

James said:
I have the following macro:

#define ABC(x) f((x) + 1)

Is it possible to get the preprocessor to replace lines like

x = g(y) + ABC(z/3) ;

with

x = g(y) + f((z/3) + 1) ;

but leave lines like

x = h->ABC(w) ;

alone?
Nope.
The preprocessor is dumb*. It works purely by textual substitution.

HTH,
--ag

* - By `dumb' I refer to its level of sophistication. It knows about
tokens, but not anything else about how C works.
 
E

Eric Sosman

James S. Singleton wrote On 12/22/05 14:48,:
I have the following macro:

#define ABC(x) f((x) + 1)

Is it possible to get the preprocessor to replace lines like

x = g(y) + ABC(z/3) ;

with

x = g(y) + f((z/3) + 1) ;

but leave lines like

x = h->ABC(w) ;

No. You could, if you wish, write

x = h->(ABC)(w);

.... but it would probably prevent confusion if you
made a practice of using different names for different
things.
 
P

pete

James said:
I have the following macro:

#define ABC(x) f((x) + 1)

Is it possible to get the preprocessor to replace lines like

x = g(y) + ABC(z/3) ;

with

x = g(y) + f((z/3) + 1) ;

but leave lines like

x = h->ABC(w) ;

alone?

Yes.
Precede the lines that you want left alone with

#undef ABC(x)

and then redefine ABC(x), after.
 
G

gooch

pete said:
Yes.
Precede the lines that you want left alone with

#undef ABC(x)

and then redefine ABC(x), after.

Bit of a kludge isn't it? It also means that anyone using ABC had
better know about this quirk.
 
K

kleuske

James S. Singleton schreef:
I have the following macro:

#define ABC(x) f((x) + 1)

Is it possible to get the preprocessor to replace lines like

x = g(y) + ABC(z/3) ;

with

x = g(y) + f((z/3) + 1) ;

but leave lines like

x = h->ABC(w) ;

alone?

Are you sure your problem is with the preprocessor? I do not know you
application, of course, but when you need this kind of "exceptions", it
makes me think you are suffering from a design-flaw.
It's much better to adress that than introduce some of the kludges
suggested elsewhere in this thread, which will surely end up in
unmaitainable code.

1. Why do you need such an exception?
2. Is employing the same macro in both cases neccesary? If so, why?
3. Can't it be solved differently? You (almost *) ) _always_ have
multiple alternatives to code an algorithm. Choose one that suits you
_and_ the language.

*) I can't think of an example in which there is only one way, but that
does not say there isn't one.
 
R

Richard Bos

James S. Singleton said:
Is it possible to get the preprocessor to replace lines like

x = g(y) + ABC(z/3) ;

with

x = g(y) + f((z/3) + 1) ;

but leave lines like

x = h->ABC(w) ;

alone?

It probably isn't, in any sane way, but if you discover a workaround,
let me beg of you, for the sake of your maintenance programmers, your
debuggers, and your documentation writers, please, _PLEASE_ don't.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top