use of #define MACRO(A,B)

T

Tagore

hi,

While understanding some code, I came across a definition of macro
without expansion
#define MACRO(A,B) // expansion missing

What could be use of such macro?

I think, there is no use of it without expanding e.g.
#define MACRO(A,B) A+B // expansion given

Please clear doubts

Thanks,
 
P

Peter Nilsson

Tagore said:
While understanding some code, I came across a definition
of macro without expansion
#define MACRO(A,B)      // expansion missing

What could be use of such macro?

To produce nothing in certain circumstances.
I think, there is no use of it without expanding e.g.
#define MACRO(A,B) A+B    // expansion given

Please clear doubts

#if NDEBUG
#define DEBUG(a,b)
#else
#define DEBUG(a,b) fprintf(stderr, "%s: %d\n", a, (int) (b))
#endif

DEBUG("Foo()", 42);
 
B

Barry Schwarz

hi,

While understanding some code, I came across a definition of macro
without expansion
#define MACRO(A,B) // expansion missing

I hope the comment is your addition and not present in the source.
What could be use of such macro?

I think, there is no use of it without expanding e.g.
#define MACRO(A,B) A+B // expansion given

Please clear doubts

At one time early in the development, it could have been a debugging
macro of the form
#define MACRO(A,B) \
printf(#A "=%d, " #B "=%d.\n", A, B)

After the developer was satisfied, the new definition reduces any
invocations of MACO to white space which should be ignored during
compilation.
 
S

Seebs

While understanding some code, I came across a definition of macro
without expansion
#define MACRO(A,B) // expansion missing
What could be use of such macro?

To make calls of the form "MACRO(arg1, arg2)" turn into nothing.

For instance, consider

#ifdef VERBOSE
#define DEBUG(x) x
#else
#define DEBUG(x)
#endif

DEBUG(printf("%d\n", foo->bar));

If VERBOSE is defined, this expands to printf, otherwise to an empty
statement which does nothing.

-s
 
T

Thad Smith

Barry said:
On Tue, 30 Mar 2010 17:55:32 -0700 (PDT), Tagore


I hope the comment is your addition and not present in the source.

While I typically don't use // comments, I have otherwise written macros with a
commented whitespace-only body, perhaps
#define MACRO(A,B) /* nothing */

It documented that the macro replacement was intentionally empty.
 
B

Barry Schwarz

While I typically don't use // comments, I have otherwise written macros with a
commented whitespace-only body, perhaps
#define MACRO(A,B) /* nothing */

It documented that the macro replacement was intentionally empty.

But your comment is closed, so a statement of the form
if (x)
MACRO(y,z);
compiles as
if (x)
/* nothing */ ;
while the code in the OP will compile as
if (x)
// expansion missing ;
which will do something unintended with the next statement.
 
B

Ben Bacarisse

Barry Schwarz said:
But your comment is closed, so a statement of the form
if (x)
MACRO(y,z);
compiles as
if (x)
/* nothing */ ;
while the code in the OP will compile as
if (x)
// expansion missing ;
which will do something unintended with the next statement.

This is not right. Comments are removed (well, replaced with a single
space) in translation phase 3 before pre-processing is done. Both
definitions of MACRO are empty.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top