Macro substitutions

Discussion in 'C Programming' started by James S. Singleton, Dec 22, 2005.

  1. 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?
    James S. Singleton, Dec 22, 2005
    #1
    1. Advertising

  2. James S. Singleton

    Artie Gold Guest

    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!"
    Artie Gold, Dec 22, 2005
    #2
    1. Advertising

  3. James S. Singleton

    Eric Sosman Guest

    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.

    --
    Eric Sosman, Dec 22, 2005
    #3
  4. James S. Singleton

    pete Guest

    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
    pete, Dec 22, 2005
    #4
  5. James S. Singleton

    gooch Guest

    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.
    gooch, Dec 23, 2005
    #5
  6. James S. Singleton

    Guest

    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, 2005
    #6
  7. James S. Singleton

    Richard Bos Guest

    "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
    Richard Bos, Dec 27, 2005
    #7
  8. James S. Singleton

    pete Guest

    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
    pete, Jan 5, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RJGraham
    Replies:
    6
    Views:
    472
    RJGraham
    Jun 28, 2004
  2. Chris Goller
    Replies:
    4
    Views:
    366
    Jordan Stewart
    Mar 9, 2005
  3. Tony Eva
    Replies:
    1
    Views:
    473
    Jeff Epler
    Nov 9, 2003
  4. Vivien Mallet

    Backslash substitutions

    Vivien Mallet, Sep 29, 2004, in forum: Python
    Replies:
    1
    Views:
    300
    Peter Otten
    Sep 29, 2004
  5. Yan

    do{..}while(0) macro substitutions

    Yan, Dec 30, 2004, in forum: C Programming
    Replies:
    27
    Views:
    725
    Randy Howard
    Jan 1, 2005
Loading...

Share This Page