Set undefined macros

I

Immortal Nephi

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;
}
 
I

Ian Collins

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 ;
}
 
J

John H.

        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.
 
I

Immortal Nephi

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.
 
I

Ian Collins

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.
 
I

Ian Collins

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top