445,918 Members | 2,294 Online
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

# Macro substitutions

 P: n/a 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? Dec 22 '05 #1
7 Replies

 P: n/a James S. Singleton wrote: 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. -- Artie Gold -- Austin, Texas http://goldsays.blogspot.com (new post 8/5) http://www.cafepress.com/goldsays "If you have nothing to hide, you're not trying!" Dec 22 '05 #2

 P: n/a 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. -- Er*********@sun.com Dec 22 '05 #3

 P: n/a James S. Singleton wrote: 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. -- pete Dec 22 '05 #4

 P: n/a pete wrote: James S. Singleton wrote: 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. Bit of a kludge isn't it? It also means that anyone using ABC had better know about this quirk. Dec 23 '05 #5

 P: n/a 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. Dec 23 '05 #6

 P: n/a "James S. Singleton" wrote: 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 Dec 27 '05 #7

 P: n/a gooch wrote: pete wrote: James S. Singleton wrote: 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. Bit of a kludge isn't it? It also means that anyone using ABC had better know about this quirk. Yes. -- pete Jan 5 '06 #8

### This discussion thread is closed

Replies have been disabled for this discussion.