Velocity Reviews > use of #define MACRO(A,B)

# use of #define MACRO(A,B)

Tagore
Guest
Posts: n/a

 03-31-2010
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

Thanks,

Peter Nilsson
Guest
Posts: n/a

 03-31-2010
Tagore <(E-Mail Removed)> wrote:
> 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
>

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

DEBUG("Foo()", 42);

--
Peter

Barry Schwarz
Guest
Posts: n/a

 03-31-2010
On Tue, 30 Mar 2010 17:55:32 -0700 (PDT), Tagore
<(E-Mail Removed)> wrote:

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

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.

--
Remove del for email

Seebs
Guest
Posts: n/a

 03-31-2010
On 2010-03-31, Tagore <(E-Mail Removed)> wrote:
> 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
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Guest
Posts: n/a

 03-31-2010
Barry Schwarz wrote:
> On Tue, 30 Mar 2010 17:55:32 -0700 (PDT), Tagore
> <(E-Mail Removed)> wrote:

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

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.

--

Barry Schwarz
Guest
Posts: n/a

 04-01-2010
On Wed, 31 Mar 2010 06:35:25 -0700, Thad Smith <(E-Mail Removed)>
wrote:

>Barry Schwarz wrote:
>> On Tue, 30 Mar 2010 17:55:32 -0700 (PDT), Tagore
>> <(E-Mail Removed)> wrote:

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

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

--
Remove del for email

Ben Bacarisse
Guest
Posts: n/a

 04-01-2010
Barry Schwarz <(E-Mail Removed)> writes:

> On Wed, 31 Mar 2010 06:35:25 -0700, Thad Smith <(E-Mail Removed)>
> wrote:
>
>>Barry Schwarz wrote:
>>> On Tue, 30 Mar 2010 17:55:32 -0700 (PDT), Tagore
>>> <(E-Mail Removed)> wrote:

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

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

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.

--
Ben.