views:

107

answers:

4

Please help a macro beginner... I created a simple macro for loading images and split it up into several lines so that I can log every time code generated from the macro is executed (for debugging). It looks like this:

#define LOAD_PNG(L_I_IMAGE_NAME) ({ \
PngImageClass* __tmp; \
printf("Loading png: %s", L_I_IMAGE_NAME);\
__tmp = [image loading code here];\
__tmp; \
})

My plan was to be able to easily comment out the log line (manually) when needed, but the pre-processor won't tolerate any of the normal ways. How should it be done?!

EDIT: I was completely wrong in saying it doesn't work "any of the normal ways" since I had been lazy enough to only try the single line comment. I'll also heed the advice from several responders to turn this into a function. No, there's no good reason (I guess) to use a macro for this.

+10  A: 

You can do it like this:

#define LOAD_PNG(L_I_IMAGE_NAME) ({ \
PngImageClass* __tmp; \
/*printf("Loading png: %s", L_I_IMAGE_NAME);*/\
__tmp = [image loading code here];\
__tmp; \
})

The Single line comments // simply won't work because you are specifically asking the compiler to continue the lines by providing a backslash(\) at the end of every line.

Aamir
The comment should stop before the end of line backslash; otherwise the lines will not get spliced together, and they won't all be included in the macro definition. (That is, the third line should end "*/\" rather than "\*/")
Matthew Wightman
You are right, Fixed. Thanks for pointing out.
Aamir
+3  A: 

Because a macro is all one line ( via line continuations ) you can't use '#if 0' or '//'-style comments in it. /**/ should work fine.

From the snippet you posted, this doesn't look like it should be a macro anyway. Macros should only be used when you are taking advantage of the textual substitution in some way ( for example, needing both the variable and the name of the variable ).

Chris Arguin
+1  A: 

Why do you have parenthesis around the macro statement? This will not compile once you've got around preprocessor problems and you certainly don't need it.

Key
It does compile, but I can't remember why I used them. Either it comes from some macro I've copied once, or it's a result of a confused testing session. Can't remember. I'll remove the parentheses :)
Felixyz
That's strange. I tried compiling some code with parenthesis like yours and it did not compile. For reference I used with gcc 3.4.4 on cygwin.
Key
A: 

In addition to the /.../ style comments, you can do this:

if (0) printf(...);

A better way is to not use a macro and turn it into a function instead, though, unless there are really important reasons to use a macro (but you didn't give any).

Lars Wirzenius
This wouldn't ever be optimized out though, right?
Felixyz
It entirely depends on the compiler. I would expect any decent compiler to optimize it out, since it is such an obvious case.
Lars Wirzenius
Indeed, gcc on Ubuntu seems to optimize it out even without any -O options used.
Lars Wirzenius