Just to summarize the void.pointer solution and the hints suggested by Praetorian , TC and Jarod42 , let me provide the final version ( online demo )
#include <boost/format.hpp> #include <iostream> template<typename... Arguments> std::string FormatArgs(const std::string& fmt, const Arguments&... args) { boost::format f(fmt); std::initializer_list<char> {(static_cast<void>( f % args ), char{}) ...}; return boost::str(f); } int main() { std::cout << FormatArgs("no args\n"); // "no args" std::cout << FormatArgs("%s; %s; %s;\n", 123, 4.3, "foo"); // 123; 4.3; foo; std::cout << FormatArgs("%2% %1% %2%\n", 1, 12); // 12 1 12 }
In addition, as TC noted , using fold expression syntax available with C ++ 17, the FormatArgs function can be rewritten in a more concise way
template<typename... Arguments> std::string FormatArgs(const std::string& fmt, const Arguments&... args) { return boost::str((boost::format(fmt) % ... % args)); }
source share