CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    What can and can't the preprocessor do?

    I want the code

    Code:
    MY_MACRO(x)
    to be replaced by the code

    Code:
    #ifdef my_class_##x
       #error My error message
    #endif
    class my_class_##x
    {
         ...
    };
    However, this macro does not compile:

    Code:
    #define MY_MACRO(x) \
    #ifdef my_class_##x \
        #error My error message \
    #endif \
    class my_class_##x; \
    { \
       ... \
    };
    Is there a way to accomplish something like this?
    Old Unix programmers never die, they just mv to /dev/null

  2. #2
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: What can and can't the preprocessor do?

    I don't think you can have pre-processor instructions inside macros.

    Even if you can, the statement #ifdef my_class_##x does not check whether the class my_class_##x has already been declared, it only checks whether the macro my_class_##x has already been defined.

    Although I don't think it is a good idea to use macros in that way, I don't see why you can't just use the following:

    Code:
    #define MY_MACRO(x) \
    class my_class_##x \
    { \
       ... \
    };
    If my_class_##x already exists then the compiler will complain anyway.
    Last edited by Zaccheus; August 10th, 2007 at 07:15 AM.
    My hobby projects:
    www.rclsoftware.org.uk

  3. #3
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: What can and can't the preprocessor do?

    Quote Originally Posted by HighCommander4
    However, this macro does not compile:

    Code:
    #define MY_MACRO(x) \
    #ifdef my_class_##x \
        #error My error message \
    #endif \
    class my_class_##x; \
    { \
       ... \
    };
    I just noticed the ; after the x. That's a bug in itself.
    My hobby projects:
    www.rclsoftware.org.uk

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: What can and can't the preprocessor do?

    You need to do it the other way around (typing this off the top of my head...)

    Code:
    #if CONDITION
    #define MYMACRO(X)  RealCode()
    #else
    #define MYMACRO(X)  #error "Contition Not Met"
    #endif
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured