Created
June 26, 2020 03:43
-
-
Save lanesurface/59fbce0ce05f90ff0bfbb0767d05be57 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* FILE: print_tuple.cpp | |
* DESCRIPTION: Print the contents of a std::tuple<>. | |
* DATE: 06/25/2020 | |
*/ | |
#include <iostream> | |
#include <tuple> | |
template <int I=0, typename ...T> | |
void print_tuple( | |
std::ostream& stream, | |
std::tuple<T...>& tup) | |
{ | |
const size_t tup_size=sizeof...(T); | |
if constexpr (I<tup_size) { | |
stream << std::get<I>(tup); | |
print_tuple<I+1>(stream, tup); | |
} | |
} | |
int main() { | |
auto my_tuple=std::make_tuple(0,0,0); | |
print_tuple(std::cout, my_tuple); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another way to generate the calls to
std::get
which avoids recursion is shown below.This avoids the overhead of generating the recursive template function, which grows in proportion to the size of the tuple being printed.