Skip to content

Instantly share code, notes, and snippets.

@takscape
Last active April 14, 2018 03:19
Show Gist options
  • Save takscape/e219a27e1266bf8dae087b278e76e0d8 to your computer and use it in GitHub Desktop.
Save takscape/e219a27e1266bf8dae087b278e76e0d8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sstream>
#include <tuple>
using namespace std;
template <size_t Pos, typename Tpl, typename Head>
void _format_tuple(ostream& os, const Tpl& tpl) {
os << get<Pos>(tpl);
}
template <size_t Pos, typename Tpl, typename Head, typename Snd, typename... Tail>
void _format_tuple(ostream& os, const Tpl& tpl) {
os << get<Pos>(tpl) << ", ";
_format_tuple<Pos+1, Tpl, Snd, Tail...>(os, tpl);
}
string format_tuple(const tuple<>& tpl) {
return "()";
}
template <typename... Args>
string format_tuple(const tuple<Args...>& tpl) {
ostringstream os;
os << "(";
_format_tuple<0, tuple<Args...>, Args...>(os, tpl);
os << ")";
return os.str();
}
int main(int argc, char* argv[]) {
cout << format_tuple(make_tuple()) << endl;
cout << format_tuple(make_tuple(1)) << endl;
cout << format_tuple(make_tuple(1, "foo")) << endl;
cout << format_tuple(make_tuple(1, "foo", "bar")) << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment