Suppose I have a printf
-like function (used for logging) utilizing perfect forwarding:
template<typename... Arguments>
void awesome_printf(std::string const& fmt, Arguments&&... args)
{
boost::format f(fmt);
f % /* How to specify `args` here? */;
BlackBoxLogFunction(boost::str(f).c_str());
}
(I didn't compile this but my real function follows this guideline)
How can I "unroll" the variadic argument into the boost::format variable f
?
I did some googling and found an interesting solution:
I don't know if this is a recommended solution but it seems to work. I don't like the hacky
static_cast
usage, which seems necessary to silence the unused variable warnings on GCC.Just to summarize the void.pointer's solution and the hints proposed by Praetorian, T.C. and Jarod42, let me provide the final version (online demo)
Also, as it was noted by T.C., using the fold expression syntax, available since C++17, the FormatArgs function can be rewritten in the more succinct way
As is usual with variadic templates, you can use recursion:
Demo.
In C++17, simply
(f % ... % std::forward<Arguments>(args));
will do.