Thread: Macros inside of macros

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    Macros inside of macros

    Hey, I am trying to #define something inside of one of my macros. Here is an example of my code:

    Code:
    int unused;
    
    #define REGISTER(a) a ## _REGISTER
    
    #define REGISTER_OUT(a)           \
       #ifndef (REGISTER(a))          \
          #define REGISTER(a) unused  \
       #endif // REGISTER_OUT
    
    REGISTER_OUT(A1)
    So I want to check if A1_REGISTER is defined, and if it is not, define it as the int unused. How can I do this, as the above code does not work.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Unfortunately -- you can't do that, per se. That is, you can't use preprocessor directives like #define inside macros. I've often wanted to, but you can't.

    However, you might be able to solve your problem, with something like this.
    Code:
    #ifndef A1_REGISTER
       #define REGISTER(a) unused
    #else
       #define REGISTER(a) a ## _REGISTER
    #endif // A1_REGISTER
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Wait some people at the http://www.ioccc.org have used shamelesly defines within macros. Maybe you should check out their source even though it's a bit obfuscated...
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. call to realloc() inside a function: memory problem
    By simone.marras in forum C Programming
    Replies: 15
    Last Post: 11-30-2008, 10:01 AM
  2. declaring variables inside loops
    By samus250 in forum C Programming
    Replies: 21
    Last Post: 04-30-2008, 01:51 PM
  3. variables when declared inside or outside a function
    By jas_atwal in forum C Programming
    Replies: 6
    Last Post: 12-14-2007, 02:42 PM
  4. Still battling with Copy Control
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-23-2006, 08:04 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM