Skip to content

Instantly share code, notes, and snippets.

@r-lyeh-archived
Created May 19, 2016 20:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-lyeh-archived/7c367fa16f14abd368ccc8c285ac92fd to your computer and use it in GitHub Desktop.
Save r-lyeh-archived/7c367fa16f14abd368ccc8c285ac92fd to your computer and use it in GitHub Desktop.
// [src] https://github.com/apfeltee/cpp11-sprintf
#include <iostream>
#include <sstream>
#include <string>
std::string fmt( const char *format ) {
return format ? format : "";
}
template<typename Type, typename... Args>
std::string fmt( const char *format, const Type& value, Args... args) {
std::stringstream strm;
if( format ) {
do {
if(*format == '%') {
strm << value;
strm << fmt( format+1, args... );
return strm.str();
}
strm << *format++;
} while(*format);
}
//assert(!"too many args");
return strm.str();
}
int main() {
std::cout << fmt("Hello, %! It's %:% o'clock in %.", "doc", 12, 4, "funkytown") << std::endl;
std::cout << fmt("") << std::endl;
std::cout << fmt(0) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment