Set undefined macros

Discussion in 'C++' started by Immortal Nephi, Apr 21, 2010.

  1. I am curious to find out why C++ Compiler is able to compile without
    error when I set undefined macros. C++ Compiler should report error
    C2065: 'var1' : undeclared identifier.

    void foo( int i, int j ) {}

    //#define var1 1
    //#define var2 2

    #undef var1
    #undef var2

    #define Mfoo( i, j ) do {} while (0) // foo( i, j )

    int main()
    {
    Mfoo( var1, var2 );

    Return 0;
    }
     
    Immortal Nephi, Apr 21, 2010
    #1
    1. Advertising

  2. Immortal Nephi

    Ian Collins Guest

    On 04/22/10 10:13 AM, Immortal Nephi wrote:
    > I am curious to find out why C++ Compiler is able to compile without
    > error when I set undefined macros. C++ Compiler should report error
    > C2065: 'var1' : undeclared identifier.
    >
    > void foo( int i, int j ) {}
    >
    > //#define var1 1
    > //#define var2 2
    >
    > #undef var1
    > #undef var2
    >
    > #define Mfoo( i, j ) do {} while (0) // foo( i, j )
    >
    > int main()
    > {
    > Mfoo( var1, var2 );
    >
    > Return 0;


    It will complain about this!

    > }


    The tokens aren't used, so the compiler does not see them (they are
    handled by the pre-processor). It helps to look at the pre-processor
    output the compiler sees:

    CC z.cc -E
    #1 "z.cc"
    void foo ( int i , int j ) { }
    #11
    int main ( )
    {
    do { } while ( 0 ) ;

    return 0 ;
    }

    --
    Ian Collins
     
    Ian Collins, Apr 21, 2010
    #2
    1. Advertising

  3. Immortal Nephi

    John H. Guest

    On Apr 21, 5:13 pm, Immortal Nephi <> wrote:
    >         I am curious to find out why C++ Compiler is able to compile without
    > error when I set undefined macros.  C++ Compiler should report error
    > C2065: 'var1' : undeclared identifier.
    >
    > void foo( int i, int j ) {}
    >
    > #define Mfoo( i, j ) do {} while (0) // foo( i, j )
    >
    > int main()
    > {
    >         Mfoo( var1, var2 );
    >
    >         Return 0;
    >
    > }


    I am guessing what are you seeing is the pre-processor resolving the
    macro by replacing the place where the macro was called in the
    original source with what the macro evaluates to. In this case it
    evaluates to your while loop:
    After pre-processor but before processor, your code looks like:
    int main()
    {
    do {} while (0);
    return 0;
    }
    The compiler see this code and doesn't complain.
     
    John H., Apr 21, 2010
    #3
  4. On Apr 21, 8:10 pm, Victor Bazarov <> wrote:
    > Pete Becker wrote:
    > > Immortal Nephi wrote:
    > >>     I am curious to find out why C++ Compiler is able to compile without
    > >> error when I set undefined macros.  C++ Compiler should report error
    > >> C2065: 'var1' : undeclared identifier.

    >
    > > Why? (Aside from the Microsoft-specific error message)

    >
    > >> void foo( int i, int j ) {}

    >
    > >> //#define var1 1
    > >> //#define var2 2

    >
    > >> #undef var1
    > >> #undef var2

    >
    > >> #define Mfoo( i, j ) do {} while (0) // foo( i, j )

    >
    > >> int main()
    > >> {
    > >>     Mfoo( var1, var2 );

    >
    > >>     Return 0;
    > >> }

    >
    > > The invocation of the function-like macro Mfoo(var1, var2) replaces
    > > occurrences of "i" in the definition of Mfoo with "var1" and occurrences
    > > of "j" in the definition of Mfoo with "var2". So the replacement text is

    >
    > > do {} while (0) // foo( var1, var2 )

    >
    > > There's nothing wrong with that.

    >
    > I keep confusing the phases of translation.  Aren't comments replaced
    > with single spaces before macro processing?  If so, wouldn't it result in
    >
    >      do {} while (0)
    >
    > ?  I believe unused macro argument are explicitly ignored.


    Why are you confused the phases of translation?

    You should always use do {} while (0) if you want to wrap your code
    into it because do {} while (0) treats like a real function as like
    function macro.
     
    Immortal Nephi, Apr 22, 2010
    #4
  5. Immortal Nephi

    Ian Collins Guest

    On 04/22/10 01:10 PM, Victor Bazarov wrote:
    > Pete Becker wrote:
    >>
    >> The invocation of the function-like macro Mfoo(var1, var2) replaces
    >> occurrences of "i" in the definition of Mfoo with "var1" and
    >> occurrences of "j" in the definition of Mfoo with "var2". So the
    >> replacement text is
    >>
    >> do {} while (0) // foo( var1, var2 )
    >>
    >> There's nothing wrong with that.

    >
    > I keep confusing the phases of translation. Aren't comments replaced
    > with single spaces before macro processing? If so, wouldn't it result in
    >
    > do {} while (0)
    >
    > ? I believe unused macro argument are explicitly ignored.


    I believe you are correct on both counts, the preprocessor output I
    posted backs them up.

    --
    Ian Collins
     
    Ian Collins, Apr 22, 2010
    #5
  6. Immortal Nephi

    Ian Collins Guest

    On 04/22/10 01:36 PM, Immortal Nephi wrote:
    > On Apr 21, 8:10 pm, Victor Bazarov<> wrote:
    >>
    >> I keep confusing the phases of translation. Aren't comments replaced
    >> with single spaces before macro processing? If so, wouldn't it result in
    >>
    >> do {} while (0)
    >>
    >> ? I believe unused macro argument are explicitly ignored.

    >
    > Why are you confused the phases of translation?
    >
    > You should always use do {} while (0) if you want to wrap your code
    > into it because do {} while (0) treats like a real function as like
    > function macro.


    You have misunderstood the nature of Victor's confusion! The question
    was at which phase do various textual substitutions occur. It makes
    sense to remove/replace comments before macro processing. Otherwise
    macro processing would either be more complex (aware of comments) or
    slower (applied to comments).

    --
    Ian Collins
     
    Ian Collins, Apr 22, 2010
    #6
    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,449
    Stephen J. Bevan
    Nov 7, 2003
  2. Replies:
    1
    Views:
    463
    Marco Antoniotti
    Oct 7, 2003
  3. Replies:
    5
    Views:
    505
  4. Michael T. Babcock

    Re: Explanation of macros; Haskell macros

    Michael T. Babcock, Nov 3, 2003, in forum: Python
    Replies:
    0
    Views:
    530
    Michael T. Babcock
    Nov 3, 2003
  5. Andrew Arro

    macros-loop? calling macros X times?

    Andrew Arro, Jul 23, 2004, in forum: C Programming
    Replies:
    2
    Views:
    509
    S.Tobias
    Jul 24, 2004
Loading...

Share This Page