Raildex
Raildex

Reputation: 4766

How to ignore parameter pack arguments

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
    }
}

Upvotes: 2

Views: 520

Answers (1)

Ted Lyngmo
Ted Lyngmo

Reputation: 117422

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);
}

Upvotes: 4

Related Questions