Created
May 19, 2016 20:36
-
-
Save r-lyeh-archived/7c367fa16f14abd368ccc8c285ac92fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [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