Skip to content

Instantly share code, notes, and snippets.

@jonesinator
Last active October 12, 2017 19:10
Show Gist options
  • Save jonesinator/cb1e4d05cc13fb9f5f97b8c8d6a1171a to your computer and use it in GitHub Desktop.
Save jonesinator/cb1e4d05cc13fb9f5f97b8c8d6a1171a to your computer and use it in GitHub Desktop.
c++17 - Get several inputs at once from an input stream.
#include <iostream>
#include <tuple>
template <typename... input_ts>
auto stream_get(std::istream& stream) {
if constexpr (sizeof...(input_ts) == 1) {
std::tuple_element_t<0, std::tuple<input_ts...>> input;
stream >> input;
return input;
} else {
return std::tuple{stream_get<input_ts>(stream)...};
}
}
int main() {
auto [a, b, c] = stream_get<int, int, float>(std::cin);
std::cout << a << " " << b << " " << c << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment