Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Created May 2, 2014 21:38
Show Gist options
  • Save dgodfrey206/bc442ee343ceaf55e8a0 to your computer and use it in GitHub Desktop.
Save dgodfrey206/bc442ee343ceaf55e8a0 to your computer and use it in GitHub Desktop.
Turning a tuple to a string
#include <iostream>
#include <vector>
#include <string>
namespace detail
{
template <int... Is>
struct index { };
template <int N, int... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> { };
template <int... Is>
struct gen_seq<0, Is...> : index<Is...> { };
}
template<typename... Ts, int... Is>
std::string tuple_to_string(std::tuple<Ts...>& tup, detail::index<Is...>)
{
std::string result;
auto discard = { 0, ((result += std::to_string(std::get<Is>(tup))), 0)... };
(void)discard;
return result;
}
template<typename... Ts>
std::string tuple_to_string(std::tuple<Ts...>& tup)
{
return tuple_to_string(tup, detail::gen_seq<sizeof...(Ts)>());
}
int main()
{
std::tuple<int, char, double> tup{ 1, 'a', 2.2 };
std::string str = tuple_to_string(tup);
std::cout << str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment