Reputation: 19143
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.
Upvotes: 0
Views: 215
Reputation:
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).
Upvotes: 0
Reputation: 15566
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.
Upvotes: 10
Reputation: 7076
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.
Upvotes: 1
Reputation: 11998
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 ).
Upvotes: 2