0

I only found this related question, which isn't quite what I am looking for.

I used to have macros defined inside an #ifdef statement:

#ifdef DEBUG
#   define PRINT_IF_DEBUGGING(format) printf(format);
#   define PRINTF_IF_DEBUGGING(format, ...) printf(format, __VA_ARGS__);
#else
#   define PRINT_IF_DEBUGGING(...)
#   define PRINTF_IF_DEBUGGING(...)
#endif

Now, I want to do the inverse, to have the #ifdef statements inside the macros. Something like this:

#define PRINT_IF_DEBUGGING(format, ...) \
#if defined(DEBUG) print(format); #endif
#define PRINTF_IF_DEBUGGING(format, ...) \
#if defined(DEBUG) printf(format, __VA_ARGS__); #endif

However, I am having an issue using __VA_ARGS__ inside the #ifdef defined.

error: '#' is not followed by a macro parameter
 #define PRINT_IF_DEBUGGING(format, ...)
error: '#' is not followed by a macro parameter
 #define PRINTF_IF_DEBUGGING(format, ...)
warning: __VA_ARGS__ can only appear in the expansion of a C++11 variadic macro
 #if defined(DEBUG) printf(format, __VA_ARGS__); #endif

Is this possible?

Raymond Chen
  • 43,603
  • 11
  • 89
  • 129
Eduardo Reis
  • 1,347
  • 17
  • 35
  • 1
    The more I look at it the more I wonder _why_ you'd want to abandon something that is readable and works. – Ted Lyngmo May 16 '22 at 23:38
  • Let's say I was exploring new ways for using Macros. If my intent was possible, I would be able to use `#define DEBUG` to enable the `printf` for a segment of code instead of the entire program code. – Eduardo Reis May 17 '22 at 01:56
  • In other words, I would like to the compiler to define the code not when parsing the definition of the macro `PRINTF_IF_DEBUGGING`, but when parsing the use of it. – Eduardo Reis May 17 '22 at 02:02

2 Answers2

4

This should really be a comment, but I can't format that in a way that will allow me to say what I want to say, so I'm answering instead.

Anyway, just change this:

#if defined(DEBUG) print(format); #endif

to this:

#if defined(DEBUG)
    print(format);
#endif

and so on, and that should fix it.

Paul Sanders
  • 21,574
  • 4
  • 24
  • 46
  • 2
    Fwiw, I think this _should_ be an answer :-) – Ted Lyngmo May 16 '22 at 23:11
  • 1
    @TedLyngmo To quote [Kenneth Wolstenhome](https://en.wikipedia.org/wiki/Kenneth_Wolstenholme) RIP, "it is now" :) Hmmm, I see Wikipedia have fluffed the quote. Oh well. – Paul Sanders May 16 '22 at 23:13
  • It did not solve the issue. Notice that the issue is on the second macro, the one that can evaluate to `printf(format, __VA_ARGS__)` – Eduardo Reis May 16 '22 at 23:22
  • 1
    @EduardoReis Your updated code shows that you still try to put multiple preprocessor directives on the same line. That's not what Paul suggested at all. – Ted Lyngmo May 16 '22 at 23:25
  • Oh right, you have two separate issues. I missed that, sorry. I recommend you ask a new question, incorporating the fix described in my answer, so that readers can focus on your problem using `__VA_ARGS__` . When you do that, please focus on just that issue and show both your macro and how you are invoking it, see [mre]. – Paul Sanders May 16 '22 at 23:26
  • PS: I rolled back your edit. Editing questions that have been answered in a way that renders the answer invalid is a no-no. And if you take note of and act on what @ted told you, your second problem might go away in any case. – Paul Sanders May 16 '22 at 23:28
1

You can't use #ifdef inside of #define , so no, this is not possible. The first code you showed is the correct solution.

Remy Lebeau
  • 505,946
  • 29
  • 409
  • 696