2

With the expression (void)var; we can effectively disable unused variable warnings.

However, how does that work with arguments in a parameter pack?

template<bool Debug = DebugMode, typename... Arg>
void log_debug(const Arg&... args) {
    if constexpr(Debug) {
        (std::clog << ... << args) << std::endl;
    } else {
        //ignore the arguments willingly when not in debug
        (void)args; // does not compile
    }
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Raildex
  • 3,406
  • 1
  • 18
  • 42
  • 1
    OT, but please note that parameter pack functions are *not* the same as variadic functions. Variadic functions are the old C functions like `printf` or similar. – Some programmer dude Oct 02 '22 at 09:59
  • Which compiler gives you a warning for this code (with `(void)args;` removed)? They shouldn't do that in a `if constexpr` branch like this. You are using `args` in the other branch. – user17732522 Oct 02 '22 at 10:01
  • What nuisance are you experiencing when you simply delete the entire `else` block in this specific code when actually called? Your question should include that as part of a proper [mcve]. – WhozCraig Oct 02 '22 at 10:01

1 Answers1

3

I suggest using maybe_unused:

template<bool Debug = DebugMode, typename... Arg>
void log_debug([[maybe_unused]] Arg&&... args) {

If you for some reason don't want that, you could make a fold expression over the comma operator in your else:

} else {
    (..., (void)args);
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108