Created
May 2, 2014 21:38
-
-
Save dgodfrey206/bc442ee343ceaf55e8a0 to your computer and use it in GitHub Desktop.
Turning a tuple to a string
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
#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