0

I have a function that takes a parameter but that parameter is only used if a certain macro is turned on. e.g.,

void function(bool parameter_for_debug, ....some other parameters used with or without debug turned on....) {
  // do some stuff with other parameters

#ifdef DEBUG
  // do stuff with parameter_for_debug
#endif

}

The problem with this function is you get the unused parameter warning when compiling with DEBUG off.

The current solution I have is to just instead of writing a single function, write function_without_debug and function_with_debug where the former doesn't contain parameter_for_debug and the debug logic, and the latter does (with the macro flags removed). Then in the calling function, I do

#ifdef DEBUG
  function_with_debug(....)
#else
  function_without_debug(....)
#endif

Is there another way to approach this problem? I feel there should be. The problem with my approach is just having some overlap in the two functions.

roulette01
  • 1,984
  • 2
  • 13
  • 26
  • 1
    `[[maybe_unused]]` or old school `(void)parameter_for_debug;` – Eljay Mar 16 '23 at 14:38
  • @Eljay Could you expand on the former `[[maybe_unused]]`? I'm not sure what you mean by that. For the latter, I think you just mean including `(void)parameter_for_debug;` in the body of the function to silence the warning – roulette01 Mar 16 '23 at 14:41
  • [`[[maybe_unused]]`](https://en.cppreference.com/w/cpp/language/attributes/maybe_unused) was added C++17. For the `void` cast, your understanding is correct. – Eljay Mar 16 '23 at 14:55

0 Answers0