C macro to expand struct types?

Discussion in 'C Programming' started by Doug ., Jul 1, 2011.

  1. Doug .

    Doug . Guest

    Howdy,

    I'm using the following hackery to define namespaces for structs:

    #define GENERIC_NS_PREFIX default_
    #define GENERIC_CONCAT(p1, p2) GENERIC__CONCAT(p1, p2)
    #define GENERIC__CONCAT(p1, p2) p1 ## p2
    #define $(name) GENERIC_CONCAT(GENERIC_NS_PREFIX, name)

    #undef GENERIC_NS_PREFIX
    #define GENERIC_NS_PREFIX namespace1_
    struct $(List) *list;

    #undef GENERIC_NS_PREFIX
    #define GENERIC_NS_PREFIX namespace2_
    struct $(List) *list;

    This works perfectly, and yields:

    arc:~ douglasl$ cc -E junk.h
    # 1 "junk.h"
    # 1 "<built-in>"
    # 1 "<command-line>"
    # 1 "junk.h"

    struct namespace1_List *list;
    struct namespace2_List *list;

    So, my question is this:

    Why do I need three macros to do, essentially:

    #define X x
    #define Y(y) X ## __ ## y

    Surely there is some syntax I can use that doesn't require 3 nested macros to achieve this?

    cheers,
    Doug.
    Doug ., Jul 1, 2011
    #1
    1. Advertising

  2. Doug .

    Shao Miller Guest

    On Thu, 30 Jun 2011 18:31:34 -0700 (PDT), "Doug ."
    <> wrote:
    > ...
    > So, my question is this:



    > Why do I need three macros to do, essentially:



    > #define X x
    > #define Y(y) X ## __ ## y



    > Surely there is some syntax I can use that doesn't require 3 nested

    macros to achieve this?

    I can't think of any. Sorry.
    Shao Miller, Jul 3, 2011
    #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. MiniDisc_2k2

    Re: Expand macro's

    MiniDisc_2k2, Jul 22, 2003, in forum: C++
    Replies:
    4
    Views:
    517
    Stewart Gordon
    Jul 22, 2003
  2. Victor Bazarov

    Re: Expand macro's

    Victor Bazarov, Jul 22, 2003, in forum: C++
    Replies:
    0
    Views:
    410
    Victor Bazarov
    Jul 22, 2003
  3. Guy Harrison

    Re: Expand macro's

    Guy Harrison, Jul 22, 2003, in forum: C++
    Replies:
    0
    Views:
    369
    Guy Harrison
    Jul 22, 2003
  4. Chris Fogelklou
    Replies:
    36
    Views:
    1,372
    Chris Fogelklou
    Apr 20, 2004
  5. sathyashrayan

    pls expand this macro

    sathyashrayan, Oct 30, 2005, in forum: C Programming
    Replies:
    8
    Views:
    419
    Simon Biber
    Oct 31, 2005
Loading...

Share This Page