Skip to content

Instantly share code, notes, and snippets.

@Fiona-J-W
Created April 4, 2014 01:17
Show Gist options
  • Save Fiona-J-W/9966168 to your computer and use it in GitHub Desktop.
Save Fiona-J-W/9966168 to your computer and use it in GitHub Desktop.
tuple_reader
#include <iostream>
#include <tuple>
#include <istream>
template<unsigned Index, unsigned Max, typename Tuple>
struct read_tuple_helper {
static void read(std::istream& stream, Tuple& tuple) {
stream >> std::get<Index>(tuple);
read_tuple_helper<Index + 1, Max, Tuple>::read(stream, tuple);
}
};
template<unsigned Max, typename Tuple>
struct read_tuple_helper<Max, Max, Tuple>{
static void read(std::istream& stream, Tuple& tuple) {
stream >> std::get<Max>(tuple);
}
};
template<typename...Types>
std::tuple<Types...> read_tuple(std::istream& stream) {
std::tuple<Types...> tuple;
read_tuple_helper<0, sizeof...(Types) - 1, std::tuple<Types...>>::read(stream, tuple);
return tuple;
}
template<>
std::tuple<> read_tuple(std::istream&) = delete;
int main() {
int i;
double d;
std::tie(i, d) = read_tuple<int, double>(std::cin);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment