2

I defined with -D compiler option a symbol debug: -DDEBUG_VALUE. I would like a function in which the presence of a parameter depends on the definition or less of the symbol debug flag.

Namely if DEBUG_VALUE is defined, I have

my_function(int parameter1, int  my_parameter_dependent)

Otherwise

my_function(int parameter1)

In this way

my_function(int parameter1  #ifdef DEBUG_VALUE , int my_parameter_dependent #endif)

I get

error: stray ‘#’ in program
error: expected ‘,’ or ‘...’ before ‘ifdef’

How can I solve it?

(I'm on a C++ compiler on a Unix system.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nick
  • 1,439
  • 2
  • 15
  • 28

2 Answers2

5

You can declare the function differently...

 #if defined( DEBUG_VALUE )
     void my_function( int parameter1, int my_parameter_dependent );
 #else
     void my_function( int parameter1 );
 #endif

Create an embedded macro

 # if defined( DEBUG_VALUE )
         #define DEPENDENT_PARAM( x )   x
 # else
         #define DEPENDENT_PARAM( x )
 #endif
 void my_function( int parameter1  DEPENDENT_PARAM(, int my_parameter_dependent) );

This means that the text within the macro is munched by the preprocessor, and is hidden

Or you can declare debug data

  #if defined( DEBUG_VALUE )
      #define EXTRA_DEBUG  , int my_parameter_dependent
  #else
      #define EXTRA_DEBUG
  #endif
  void my_function( int parameter1 EXTRA_DEBUG );

They all have their merits, depending on flexibility and how many functions are changed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mksteve
  • 12,614
  • 3
  • 28
  • 50
  • `DEPENDENT_PARAM` strikes me as a good solution to this particular problem (though under the circumstances, I think a different name like `DEBUG_PARAM` would probably be better). – Jerry Coffin Jun 16 '17 at 17:41
  • I'd rather call it `DEBUG_ONLY`, since its use isn't limited to parameters. In fact, having `_PARAM` there in the name would be confusing to me, because I might be tempted to omit the comma delimiter. (Warning: MFC has such a `DEBUG_ONLY` macro, so beware of possible name clashes.) – Cody Gray - on strike Jun 16 '17 at 18:03
4

You can't embed a preprocessor macro within a line. They require a dedicated line of their own. So you have to break this declaration up onto separate lines:

#ifdef DEBUG_VALUE
    void my_function(int parameter1, int my_parameter_dependant);
#else
    void my_function(int parameter1);
#endif

Or, if you want to get clever and DRY, take advantage of C++'s great flexibility with regard to statements and whitespace:

void my_function(int parameter1
#ifdef DEBUG_VALUE
                , int my_parameter_dependant
#endif
                );
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 2
    A 3rd way is with an appropriately defined helper macro: `void my_function(int parameter1 DEBUG_VALUE_ONLY(int my_parameter_dependent))`. – 1201ProgramAlarm Jun 16 '17 at 17:29
  • 1
    Hmm yeah, the only thing better than 1 macro is 2 macros! – Cody Gray - on strike Jun 16 '17 at 17:33
  • @CodyGray Excellent, your second solution. I knew the first solution but it did not matter because I would have to replicate many lines of code. The second way works. Thanks. – Nick Jun 16 '17 at 17:35
  • I strongly recommend against the second version; it tends to reduce understanding even more than the first. Regardless, preprocessor trickery like this can easily get hard to understand, especially once you start wanting yet another configuration – Justin Jun 16 '17 at 17:37
  • @Justin I use it in debug mode alone. When the code will be ready for release this preprocessor directive will disappear. – Nick Jun 16 '17 at 17:40