#redefine preprocessor macro statement.

Discussion in 'C++' started by Ernie.Pasveer@cggveritas.com, Apr 14, 2008.

  1. Guest

    Hi All,

    Is there a way to create a macro called #redefine that will allow
    redefinition without having to use #ifdef/#undef

    For example, I'd like to do something like this :

    #define THING "apple"
    ... code

    #redefine THING "banana"
    ... code

    #redefine THING "orange"
    ... code


    Instead of :

    #define THING "apple"
    ... code

    #undef THING
    #define THING "banana"
    ... code

    #undef THING
    #define THING "orange"
    ... code
     
    , Apr 14, 2008
    #1
    1. Advertising

  2. user923005 Guest

    Re: #redefine preprocessor macro statement.

    On Apr 14, 1:00 pm, wrote:
    > Hi All,
    >
    > Is there a way to create a macro called  #redefine that will allow
    > redefinition without having to use #ifdef/#undef
    >
    > For example,  I'd like to do something like this :
    >
    >     #define THING  "apple"
    >     ... code
    >
    >     #redefine THING "banana"
    >     ... code
    >
    >     #redefine THING "orange"
    >     ... code
    >
    > Instead of :
    >
    >     #define THING  "apple"
    >     ... code
    >
    >    #undef THING
    >    #define THING "banana"
    >     ... code
    >
    >    #undef THING
    >    #define THING "orange"
    >    ... code


    void isolating_function(char *thing)
    {
    /* Do stuff here */
    }


    isolating_function("orange");
    isolating_function("banana");
    isolating_function("apple");

    I think that it is good that you cannot do what you want to do. It is
    trouble waiting to happen.
     
    user923005, Apr 14, 2008
    #2
    1. Advertising

  3. Kenneth Bull Guest

    Re: #redefine preprocessor macro statement.

    On Apr 14, 4:00 pm, wrote:
    > Hi All,
    >
    > Is there a way to create a macro called  #redefine that will allow
    > redefinition without having to use #ifdef/#undef
    >
    > For example,  I'd like to do something like this :
    >
    >     #define THING  "apple"
    >     ... code
    >
    >     #redefine THING "banana"
    >     ... code
    >
    >     #redefine THING "orange"
    >     ... code
    >


    just change the #redefine's to #define's


    >
    > Instead of :
    >
    >     #define THING  "apple"
    >     ... code
    >
    >    #undef THING
    >    #define THING "banana"
    >     ... code
    >
    >    #undef THING
    >    #define THING "orange"
    >    ... code




    You don't need the #undef before reusing the #define.
     
    Kenneth Bull, Apr 14, 2008
    #3
  4. Re: #redefine preprocessor macro statement.

    Kenneth Bull <> writes:
    > On Apr 14, 4:00 pm, wrote:
    >> Is there a way to create a macro called  #redefine that will allow
    >> redefinition without having to use #ifdef/#undef
    >>
    >> For example,  I'd like to do something like this :
    >>
    >>     #define THING  "apple"
    >>     ... code
    >>
    >>     #redefine THING "banana"
    >>     ... code
    >>
    >>     #redefine THING "orange"
    >>     ... code
    >>


    No, there isn't. A macro expansion cannot include preprocessor
    directives.

    > just change the #redefine's to #define's

    [...]
    > You don't need the #undef before reusing the #define.


    Yes, you do. C99 6.10.3p2 says:

    An identifier currently defined as an object-like macro shall not
    be redefined by another #define preprocessing directive unless the
    second definition is an object-like macro definition and the two
    replacement lists are identical.

    followed by similar wording for function-like macros.

    I see this was cross-posted to comp.lang.c and comp.lang.c++. I
    assume the rules for C++ are similar, but I'm not certain. It's
    better to post to the newsgroup that covers the language you're
    actually using.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Apr 14, 2008
    #4
    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. qazmlp
    Replies:
    18
    Views:
    571
    Martin Dickopp
    Aug 16, 2003
  2. Cronus
    Replies:
    1
    Views:
    718
    Paul Mensonides
    Jul 15, 2004
  3. Amy
    Replies:
    13
    Views:
    6,060
    Kurt Stutsman
    Feb 23, 2005
  4. qazmlp
    Replies:
    18
    Views:
    879
    Martin Dickopp
    Aug 16, 2003
  5. Replies:
    3
    Views:
    437
    Keith Thompson
    Apr 14, 2008
Loading...

Share This Page