Skip to content

Instantly share code, notes, and snippets.

@eyelash
Last active December 6, 2015 14:14
Show Gist options
  • Select an option

  • Save eyelash/5491d652c54a4de31964 to your computer and use it in GitHub Desktop.

Select an option

Save eyelash/5491d652c54a4de31964 to your computer and use it in GitHub Desktop.
a printf-like printer for arbitrary types
#include <cstdio>
class Printer;
class Printable {
public:
virtual void print (Printer& printer) const = 0;
};
class Printer {
FILE* file;
public:
Printer (FILE* file = stdout): file(file) {}
void print (const char* s) {
fputs (s, file);
}
void print (int n) {
fprintf (file, "%d", n);
}
void print (const Printable& p) {
p.print (*this);
}
template <class T0, class... T> void print (const char* s, const T0& v0, const T&... v) {
while (true) {
if (*s == '\0') return;
if (*s == '%') {
++s;
if (*s != '%') break;
}
fputc (*s, file);
++s;
}
print (v0);
print (s, v...);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment