#include #include #include /** * Convert all std::strings to const char* using constexpr if (C++17) */ template auto convert(T&& t) { if constexpr (std::is_same>, std::string>::value) { return std::forward(t).c_str(); } else { return std::forward(t); } } /** * printf like formatting for C++ with std::string * Original source: https://stackoverflow.com/a/26221725/11722 */ template std::string stringFormatInternal(const std::string& format, Args&& ... args) { size_t size = snprintf(nullptr, 0, format.c_str(), std::forward(args) ...) + 1; if( size <= 0 ){ throw std::runtime_error( "Error during formatting." ); } std::unique_ptr buf(new char[size]); snprintf(buf.get(), size, format.c_str(), args ...); return std::string(buf.get(), buf.get() + size - 1); } template std::string stringFormat(std::string fmt, Args&& ... args) { return stringFormatInternal(fmt, convert(std::forward(args))...); } int main() { std::string s = "a"; const std::string cs = "b"; const char* cc = "c"; int i = 1; const int i2 = 2; using namespace std::literals; std::cout << stringFormat("%s %s %s %s %s %d %d %d \n", s, cs, cc, "d", "e"s, i, i2, 3); return 0; }