std::ranges::to_tuple — convert a range to a tuple.
1#include <ranges>2#include <tuple>34std::vector<int> v = {1, 2, 3};56// Convert to tuple7auto t = std::views::all(v) | std::ranges::to_tuple();8// t = std::tuple<int, int, int>(1, 2, 3)910// Structured binding11auto [a, b, c] = t;12std::cout << a << " " << b << " " << c; // 1 2 31314// With transform15std::string str = "hello";16auto chars = str | std::views::all17 | std::views::transform([](char c) { return std::toupper(c); })18 | std::ranges::to_tuple();1920// From iota21auto nums = std::views::iota(1, 4)22 | std::ranges::to_tuple();23// nums = (1, 2, 3)2425// Compile-time size26static_assert(std::tuple_size_v<decltype(nums)> == 3);2728// Use with std::apply29auto result = std::apply([](auto... args) {30 return (args + ...);31}, nums);32// result = 6
Benefits: