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.

3
  • 1
    [[maybe_unused]] or old school (void)parameter_for_debug; Commented Mar 16, 2023 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 Commented Mar 16, 2023 at 14:41
  • [[maybe_unused]] was added C++17. For the void cast, your understanding is correct. Commented Mar 16, 2023 at 14:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.