Skip to content

Instantly share code, notes, and snippets.

@jdeng
Created July 9, 2014 01:06
Show Gist options
  • Save jdeng/7fa56b7f9f26e6959577 to your computer and use it in GitHub Desktop.
Save jdeng/7fa56b7f9f26e6959577 to your computer and use it in GitHub Desktop.
to_string & println with variadic templates
template< class S> void print_with(S& s, char delim) {}
template< class S, typename T> void print_with(S& s, char delim, const T& t) { s<<t; }
template <class S, typename T, typename... Args> void print_with(S& s, char delim, const T& t, const Args& ...args) { s << t; if (delim) s << delim; print_with(s, delim, args...); }
template <typename... Args>
std::string to_string_with(char delim, const Args& ...args) { std::stringstream ss; print_with(ss, delim, args...); return ss.str(); }
template <typename... Args> std::string to_string(const Args& ...args) { return to_string_with('\x00', args...); }
template <typename... Args> std::ostream& print(std::ostream& out, const Args& ...args) { print_with(out, '\x00', args...); return out;}
template <typename... Args> std::ostream& println(std::ostream& out, const Args& ...args) { print_with(out, '\x00', args...); out << std::endl; return out;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment