pistacchio
pistacchio

Reputation: 58963

Variadic function: expression contains unexpanded parameter pack 'args'

I want to write a vey simple log function that accepts any number of arguments and outputs them to console. Example usage:

LOG("hello");
LOG("hello", 1, 0.6);

I started implementing it like this

template<typename... Args>
void LOG(Args... args) {
    va_list vargs;
    va_start(vargs, args);
    // for (auto arg: vargs) {}
}

But the compiler (clang++) gives me the error in the title

expression contains unexpanded parameter pack 'args'

Any help? Thanks

Upvotes: 2

Views: 3548

Answers (1)

Salvatore Avanzo
Salvatore Avanzo

Reputation: 2786

You can try this :

void LOG() {
    cout << endl;
}
template<class T, class... OtherArgs> void LOG(T&& var, OtherArgs&&... args) {
    cout << std::forward<T>(var);
    LOG(std::forward<OtherArgs>(args)...);
}

That is a recursive solution based on this work (explained here as well)

Upvotes: 9

Related Questions