Created
November 4, 2025 19:15
-
-
Save vladiant/9c6b45796fa86e09ed7007f650202770 to your computer and use it in GitHub Desktop.
C++ tuple iteration 1
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 <tuple> | |
| template <typename T> | |
| void printElem(const T& x) { | |
| std::cout << x << ','; | |
| }; | |
| template <typename TupleT, std::size_t... Is> | |
| void printTupleManual(const TupleT& tp) { | |
| (printElem(std::get<Is>(tp)), ...); | |
| } | |
| int main() { | |
| std::tuple tp { 10, 20, "hello"}; | |
| printTupleManual<decltype(tp), 0, 1, 2>(tp); | |
| } |
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 <tuple> | |
| template <typename T> | |
| void printElem(const T& x) { | |
| std::cout << x << ','; | |
| }; | |
| template <typename TupleT, std::size_t... Is> | |
| void printTupleManual(const TupleT& tp, std::index_sequence<Is...>) { | |
| (printElem(std::get<Is>(tp)), ...); | |
| } | |
| template <typename TupleT, std::size_t TupSize = std::tuple_size_v<TupleT>> | |
| void printTupleGetSize(const TupleT& tp) { | |
| printTupleManual(tp, std::make_index_sequence<TupSize>{}); | |
| } | |
| int main() { | |
| std::tuple tp { 10, 20, "hello"}; | |
| printTupleGetSize(tp); | |
| } |
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
| template <typename TupleT, std::size_t... Is> | |
| void printTupleImp(const TupleT& tp, std::index_sequence<Is...>) { | |
| size_t index = 0; | |
| auto printElem = [&index](const auto& x) { | |
| if (index++ > 0) | |
| std::cout << ", "; | |
| std::cout << x; | |
| }; | |
| std::cout << "("; | |
| (printElem(std::get<Is>(tp)), ...); | |
| std::cout << ")"; | |
| } | |
| template <typename TupleT, std::size_t TupSize = std::tuple_size_v<TupleT>> | |
| void printTuple(const TupleT& tp) { | |
| printTupleImp(tp, std::make_index_sequence<TupSize>{}); | |
| } | |
| int main() { | |
| std::tuple tp { 10, 20, "hello"}; | |
| printTuple(tp); | |
| } |
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 <ostream> | |
| #include <tuple> | |
| template <typename TupleT, std::size_t... Is> | |
| std::ostream& printTupleImp(std::ostream& os, const TupleT& tp, std::index_sequence<Is...>) { | |
| size_t index = 0; | |
| auto printElem = [&index, &os](const auto& x) { | |
| if (index++ > 0) | |
| os << ", "; | |
| os << x; | |
| }; | |
| os << "("; | |
| (printElem(std::get<Is>(tp)), ...); | |
| os << ")"; | |
| return os; | |
| } | |
| template <typename TupleT, std::size_t TupSize = std::tuple_size_v<TupleT>> | |
| std::ostream& printTuple(std::ostream& os, const TupleT& tp) { | |
| return printTupleImp(os, tp, std::make_index_sequence<TupSize>{}); | |
| } | |
| template <typename TupleT, std::size_t TupSize = std::tuple_size<TupleT>::value> | |
| std::ostream& operator <<(std::ostream& os, const TupleT& tp) { | |
| return printTupleImp(os, tp, std::make_index_sequence<TupSize>{}); | |
| } | |
| int main() { | |
| std::tuple tp { 10, 20, "hello"}; | |
| std::cout << tp << '\n'; | |
| } |
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 <ostream> | |
| #include <tuple> | |
| template <typename TupleT, std::size_t... Is> | |
| std::ostream& printTupleImp(std::ostream& os, const TupleT& tp, std::index_sequence<Is...>) { | |
| auto printElem = [&os](const auto& x, size_t id) { | |
| if (id > 0) | |
| os << ", "; | |
| os << id << ": " << x; | |
| }; | |
| os << "("; | |
| (printElem(std::get<Is>(tp), Is), ...); | |
| os << ")"; | |
| return os; | |
| } | |
| template <typename TupleT, std::size_t TupSize = std::tuple_size<TupleT>::value> | |
| std::ostream& operator <<(std::ostream& os, const TupleT& tp) { | |
| return printTupleImp(os, tp, std::make_index_sequence<TupSize>{}); | |
| } | |
| int main() { | |
| std::tuple tp { 10, 20, "hello"}; | |
| std::cout << tp << '\n'; | |
| std::tuple vals { 1.1, 2.2, "hello", "world"}; | |
| std::cout << vals << '\n'; | |
| } |
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 <tuple> | |
| int sum(int a, int b, int c) { | |
| return a + b + c; | |
| } | |
| void print(std::string_view a, std::string_view b) { | |
| std::cout << "(" << a << ", " << b << ")\n"; | |
| } | |
| int main() { | |
| std::tuple numbers {1, 2, 3}; | |
| std::cout << std::apply(sum, numbers) << '\n'; | |
| std::tuple strs {"Hello", "World"}; | |
| std::apply(print, strs); | |
| } |
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 <tuple> | |
| template <typename... Args> | |
| void printImpl(const Args&... tupleArgs) { | |
| size_t index = 0; | |
| auto printElem = [&index](const auto& x) { | |
| if (index++ > 0) | |
| std::cout << ", "; | |
| std::cout << x; | |
| }; | |
| (printElem(tupleArgs), ...); | |
| } | |
| template <typename... Args> | |
| void printTupleApplyFn(const std::tuple<Args...>& tp) { | |
| std::cout << "("; | |
| std::apply(printImpl<Args...>, tp); // << | |
| std::cout << ")"; | |
| } | |
| int main() { | |
| std::tuple tp { 10, 20, 3.14, 42, "hello"}; | |
| printTupleApplyFn(tp); | |
| } |
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 <tuple> | |
| template <typename TupleT> | |
| void printTupleApply(const TupleT& tp) { | |
| std::cout << "("; | |
| std::apply | |
| ( | |
| [](const auto&... tupleArgs) | |
| { | |
| size_t index = 0; | |
| auto printElem = [&index](const auto& x) { | |
| if (index++ > 0) | |
| std::cout << ", "; | |
| std::cout << x; | |
| }; | |
| (printElem(tupleArgs), ...); | |
| }, tp | |
| ); | |
| std::cout << ")"; | |
| } | |
| int main() { | |
| std::tuple tp { 10, 20, 3.14, 42, "hello"}; | |
| printTupleApply(tp); | |
| } |
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 <tuple> | |
| template <typename TupleT> | |
| void printTupleApply(const TupleT& tp) { | |
| std::apply | |
| ( | |
| [](const auto& first, const auto&... restArgs) | |
| { | |
| auto printElem = [](const auto& x) { | |
| std::cout << ", " << x; | |
| }; | |
| std::cout << "(" << first; | |
| (printElem(restArgs), ...); | |
| }, tp | |
| ); | |
| std::cout << ")"; | |
| } | |
| int main() { | |
| std::tuple tp { 10, 20, 3.14, 42, "hello"}; | |
| printTupleApply(tp); | |
| } |
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
| https://www.cppstories.com/2022/tuple-iteration-basics/ | |
| https://www.cppstories.com/2022/tuple-iteration-apply/ | |
| https://www.cppstories.com/2025/tuple-iteration-cpp26/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment