C Preprocessor - Cascaded Macros

Discussion in 'C Programming' started by Michael Blackney, Sep 10, 2004.

  1. I've been looking into cascaded macros and I can't seem to find if
    what I'm doing is portable. I want to do something like:

    #define THREE_ONES 1,1,1
    #define ONE_TWO_THREE 1,2,3

    #define JOIN_LISTS(a,b) JOIN_LISTS_PROCESSED(a,b)
    #define JOIN_LISTS_PROCESSED(a1,b1,c1,a2,b2,c2) a1+a2,b1+b2,c1+c2

    so that the statement:

    { JOIN_LISTS(THREE_ONES,ONE_TWO_THREE) },

    will expand (one the first preprocessor pass) to:

    { JOIN_LISTS_PROCESSED(1,1,1,1,2,3) },

    then expand on the second pass to:

    { 1+1, 1+2, 1+3 },

    On my compiler (DJGPP) it works just fine - but is it portable? Do
    all preprocessors use a similar replacement order?

    --
    michaelblackney at hotmail dot com
    http://aburatan.sourceforge.net/
    Latest version 0.95 2-5-4
     
    Michael Blackney, Sep 10, 2004
    #1
    1. Advertising

  2. Michael Blackney

    Chris Barts Guest

    On Thu, 09 Sep 2004 16:08:15 -0700, Michael Blackney wrote:

    > I've been looking into cascaded macros and I can't seem to find if
    > what I'm doing is portable. I want to do something like:
    >
    > #define THREE_ONES 1,1,1
    > #define ONE_TWO_THREE 1,2,3
    >
    > #define JOIN_LISTS(a,b) JOIN_LISTS_PROCESSED(a,b)
    > #define JOIN_LISTS_PROCESSED(a1,b1,c1,a2,b2,c2) a1+a2,b1+b2,c1+c2


    All of these definitions look portable. For maximum utility, you should
    put in a lot more parentheses, like so:

    (a1)+(a2),(b1)+(b2),(c1)+(c2)

    Why? So you can have lists like this:
    JOIN_LISTS_PROCESSED(x+5,2+3/2,5-6,4-23,5&6,7^2)

    >
    > so that the statement:
    >
    > { JOIN_LISTS(THREE_ONES,ONE_TWO_THREE) },
    >
    > will expand (one the first preprocessor pass) to:
    >
    > { JOIN_LISTS_PROCESSED(1,1,1,1,2,3) },
    >
    > then expand on the second pass to:
    >
    > { 1+1, 1+2, 1+3 },
    >
    > On my compiler (DJGPP) it works just fine - but is it portable? Do
    > all preprocessors use a similar replacement order?


    I can't think of any reason a conforming compiler would misparse your
    headers.
     
    Chris Barts, Sep 10, 2004
    #2
    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. Cronus
    Replies:
    1
    Views:
    693
    Paul Mensonides
    Jul 15, 2004
  2. sunny

    cascaded struct members.

    sunny, Oct 16, 2006, in forum: C++
    Replies:
    6
    Views:
    340
  3. Frank

    Cascaded Macros -- Back-Up

    Frank, Nov 22, 2007, in forum: C Programming
    Replies:
    1
    Views:
    328
    Eric Sosman
    Nov 22, 2007
  4. GENERATE - cascaded

    , Apr 28, 2008, in forum: VHDL
    Replies:
    2
    Views:
    440
  5. dbuchanan
    Replies:
    6
    Views:
    2,866
    Steven Cheng [MSFT]
    Jun 11, 2008
Loading...

Share This Page