macro chain

Discussion in 'C Programming' started by luser- -droog, Mar 14, 2012.

  1. I remember reading something from Knuth to the effect that you don't
    really need multiple argument macros because you can yield a macro
    name from the macro expansion and call that with a new argument.

    So I tried a little experiment:

    #define A(x) x + B
    #define B(y) y y y
    A(2)(3)

    And I'll be darned if gcc -E doesn't produce:

    2 + 3 3 3

    But I'm not really doing this right, am I? Does anyone have
    suggestions for where I could learn more? (more Knuth?)

    --
    The mystery of luserdroog
    luser- -droog, Mar 14, 2012
    #1
    1. Advertising

  2. luser- -droog

    Ian Collins Guest

    On 03/14/12 06:00 PM, io_x wrote:
    > "luser- -droog"<> ha scritto nel messaggio
    > news:...
    >> I remember reading something from Knuth to the effect that you don't
    >> really need multiple argument macros because you can yield a macro
    >> name from the macro expansion and call that with a new argument.
    >>
    >> So I tried a little experiment:
    >>
    >> #define A(x) x + B
    >> #define B(y) y y y
    >> A(2)(3)
    >>
    >> And I'll be darned if gcc -E doesn't produce:
    >>
    >> 2 + 3 3 3
    >>
    >> But I'm not really doing this right, am I? Does anyone have
    >> suggestions for where I could learn more? (more Knuth?)

    >
    > better not use C macro if not in these case:
    > #define whatIsThisNumber 89
    > #define B 73
    > #define u32 unsigned int
    > #define R return


    No!


    --
    Ian Collins
    Ian Collins, Mar 14, 2012
    #2
    1. Advertising

  3. luser- -droog <> writes:

    > I remember reading something from Knuth to the effect that you don't
    > really need multiple argument macros because you can yield a macro
    > name from the macro expansion and call that with a new argument.
    >
    > So I tried a little experiment:
    >
    > #define A(x) x + B
    > #define B(y) y y y
    > A(2)(3)
    >
    > And I'll be darned if gcc -E doesn't produce:
    >
    > 2 + 3 3 3
    >
    > But I'm not really doing this right, am I? Does anyone have
    > suggestions for where I could learn more? (more Knuth?)


    Not all macro processors are created equal. The big difference between
    C's macro processor and many others (TeX, m4, Lisp, TRAC, ...) is that a
    macro expansion can't define new macros (in fact they can't generate
    *any* macro processing directives).

    Knuth's remark may well be about macro systems that can mirror what
    functional languages often do for multiple-argument functions (looking
    up Currying and partial application may give you more about this -- it's
    off topic enough that I don't want to go into it here). Though I don't
    know the Knuth document in question, I suspect he is referring to macros
    that work in a similar way.

    In C+ it might look a bit like this:

    #define ADD(x) #define ADD_(y) x + y \
    ADD_

    An invocation of ADD(3) produces a new definition of a macro called
    ADD_ which generates code to add 3 to its argument along with a call to
    this macro left "hanging". ADD(3)(4) would then yield 3 + 4.

    To do this properly, you'd want ADD_ to remove its own definition and
    you'd want definitions to "stack" so that the new (temporary) definition
    does not interfere with any existing one, and some macro systems allow
    new, unique, names to be generated thus avoiding the need to pick
    a name for the temporary macro.

    --
    Ben.
    Ben Bacarisse, Mar 14, 2012
    #3
  4. luser- -droog

    Eric Sosman Guest

    On 3/14/2012 12:47 AM, luser- -droog wrote:
    > I remember reading something from Knuth to the effect that you don't
    > really need multiple argument macros because you can yield a macro
    > name from the macro expansion and call that with a new argument.
    >
    > So I tried a little experiment:
    >
    > #define A(x) x + B
    > #define B(y) y y y
    > A(2)(3)
    >
    > And I'll be darned if gcc -E doesn't produce:
    >
    > 2 + 3 3 3
    >
    > But I'm not really doing this right, am I? Does anyone have
    > suggestions for where I could learn more? (more Knuth?)


    This should work, but only for "tail-chaining" expansions,
    that is, where the very last thing in the first macro's replacement
    list is the name of the second macro.

    For example, to put parentheses around the entire expansion
    you could not just change A to

    #define A(x) ( x + B )

    .... but you'd have to change both macros, something like

    #define A(x) ( x + B
    #define B(y) y y y )

    If the first macro's argument must expand after the second
    macro, I think even this won't suffice. For example, consider
    a simple two-argument macro like

    #define MAX(x,y) ( (x) > (y) ? (x) : (y) )
    int max = MAX(42,17);

    .... and try to build an equivalent "chain" of one-argument macros:

    #define AMAX(x) // your answer here
    #define BMAX(y) // your answer here
    int max = AMAX(42)(17);

    I think the fact that the 42 must appear both before and after
    the 17 means you're sunk.

    --
    Eric Sosman
    d
    Eric Sosman, Mar 14, 2012
    #4
  5. Thanks, Eric and Ben.
    Just what I needed!

    io_x, thanks for trying!
    luser- -droog, Mar 14, 2012
    #5
    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. Dead RAM
    Replies:
    20
    Views:
    1,112
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    580
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    502
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    471
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    459
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page