Cascaded Macros -- Back-Up

Discussion in 'C Programming' started by Frank, Nov 22, 2007.

  1. Frank

    Frank Guest

    It would be nice to have short macro names in a header file,
    whilest the probability of name clashes increases. Is there a
    way how to circumvent the 'normal' C-pre-processor handling
    of cascaded macros, such that something like the following
    would make sense?

    #ifdef SHORTY
    # TMP_BACKUP_OF_SHORTY (SHORTY)
    #endif

    #define SHORT my_nice_expression

    ....
    using SHORTY
    ....

    #ifdef TMP_BACKUP_OF_SHORTY
    # define SHORTY TMP_BACKUP_OF_SHORTY
    #endif

    Best Regards

    Frank
    Frank, Nov 22, 2007
    #1
    1. Advertising

  2. Frank

    Eric Sosman Guest

    Frank wrote:
    > It would be nice to have short macro names in a header file,


    Why?

    > whilest the probability of name clashes increases. Is there a
    > way how to circumvent the 'normal' C-pre-processor handling
    > of cascaded macros, such that something like the following
    > would make sense?
    >
    > #ifdef SHORTY
    > # TMP_BACKUP_OF_SHORTY (SHORTY)
    > #endif
    >
    > #define SHORT my_nice_expression
    >
    > ...
    > using SHORTY
    > ...
    >
    > #ifdef TMP_BACKUP_OF_SHORTY
    > # define SHORTY TMP_BACKUP_OF_SHORTY
    > #endif


    No. Choose good names and stick with them; don't try
    to introduce aliases. (Besides, they wouldn't solve the
    problem of clashes: What if TMP_BACKUP_OF_SHORTY is already
    in use? Don't imagine that nobody would invent such a name;
    you're *already* supposing at least two independent inventions
    of SHORTY, so why not two inventions of TMP_BACKUP_OF_x as a
    "standard pattern" for backup names?)

    I've used compilers that could do something like this
    using compiler-specific extensions like

    #define SHORTY xxx
    #pragma push SHORTY
    /* now SHORTY is undefined */
    #define SHORTY yyy
    /* now SHORTY expands to yyy */
    #pragma pop SHORTY
    /* now SHORTY expands to xxx again */

    .... but as soon as you decide to use such a gizmo, your code
    is tied to that specific compiler forever and will not work
    with any other -- which is why I never used that feature even
    on compilers that offered it.

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 22, 2007
    #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. Replies:
    80
    Views:
    2,421
    Stephen J. Bevan
    Nov 7, 2003
  2. Michael Blackney

    C Preprocessor - Cascaded Macros

    Michael Blackney, Sep 10, 2004, in forum: C Programming
    Replies:
    1
    Views:
    711
    Chris Barts
    Sep 10, 2004
  3. sunny

    cascaded struct members.

    sunny, Oct 16, 2006, in forum: C++
    Replies:
    6
    Views:
    331
  4. GENERATE - cascaded

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

Share This Page