Suppose I have a printf
-like function (used for logging) utilizing perfect forwarding:
template
void awesome_printf
As is usual with variadic templates, you can use recursion:
std::string awesome_printf_helper(boost::format& f){
return boost::str(f);
}
template
std::string awesome_printf_helper(boost::format& f, T&& t, Args&&... args){
return awesome_printf_helper(f % std::forward(t), std::forward(args)...);
}
template
void awesome_printf(std::string const& fmt, Arguments&&... args)
{
boost::format f(fmt);
auto result = awesome_printf_helper(f, std::forward(args)...);
// call BlackBoxLogFunction with result as appropriate, e.g.
std::cout << result;
}
Demo.
In C++17, simply (f % ... % std::forward
will do.