From d91fdf38b8ab3a6c5f5851c1cf8fe5fad32b936d Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Wed, 4 Mar 2026 18:21:24 -0700 Subject: [PATCH 1/3] :sparkles: Add destructurable `integer_sequence` Problem: - With clang backporting "structured bindings can introduce a pack", it would be very useful to be able to destructure an `integer_sequence`, to avoid having to do the usual call of an immediately-invoked lambda expression. Solution: - Add `make_integer_sequence` and `make_index_sequence` that can be destructured. Note: - This is first available in clang-21. --- include/stdx/utility.hpp | 20 ++++++++++++++++++++ test/utility.cpp | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/stdx/utility.hpp b/include/stdx/utility.hpp index 20db122..0b9dd43 100644 --- a/include/stdx/utility.hpp +++ b/include/stdx/utility.hpp @@ -287,9 +287,29 @@ template constexpr auto is_ct_v> = true; template constexpr auto is_ct_v = is_ct_v; #endif + +template +struct make_integer_sequence : std::make_integer_sequence {}; +template +using make_index_sequence = make_integer_sequence; +template +using index_sequence_for = make_index_sequence; + +template +constexpr auto get(make_integer_sequence) { + return std::integral_constant{}; +} + } // namespace v1 } // namespace stdx +template +struct std::tuple_size> + : std::integral_constant {}; +template +struct std::tuple_element> + : stdx::type_identity> {}; + // NOLINTBEGIN(cppcoreguidelines-macro-usage) #ifndef FWD diff --git a/test/utility.cpp b/test/utility.cpp index 1fa2c46..1c02365 100644 --- a/test/utility.cpp +++ b/test/utility.cpp @@ -432,3 +432,23 @@ TEST_CASE("CX_WRAP non-constexpr expression", "[utility]") { #endif #endif + +STDX_PRAGMA(diagnostic push) +#ifdef __clang__ +STDX_PRAGMA(diagnostic ignored "-Wunknown-warning-option") +STDX_PRAGMA(diagnostic ignored "-Wc++26-extensions") +#endif +#if __cpp_structured_bindings >= 202411L +namespace { +template constexpr auto sum() { + auto [... Is] = stdx::make_index_sequence{}; + constexpr auto sum = (0 + ... + decltype(Is)::value); + return sum; +} +} // namespace + +TEST_CASE("destructurable integer_sequence", "[utility]") { + STATIC_CHECK(sum<4>() == 6); +} +#endif +STDX_PRAGMA(diagnostic pop) From c2ad984bd3fe87dae81a071f02b5a7d35184eee1 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Wed, 4 Mar 2026 21:56:07 -0700 Subject: [PATCH 2/3] :sparkles: Make `integer_sequence` iterable like `tuple` Problem: - It's useful to be able to pass an `integer_sequence` into tuple algorithms like `unrolled_for_each`. Solution: - Change tuple algorithms to use `get` rather than indexing. - Add specialization of `tuple_size_v` for `integer_sequence`. - Add specialization of `get` for `span`. --- include/stdx/span.hpp | 6 ++++++ include/stdx/tuple.hpp | 3 +++ include/stdx/tuple_algorithms.hpp | 4 ++-- test/tuple_algorithms.cpp | 14 ++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/stdx/span.hpp b/include/stdx/span.hpp index 87c9f18..c89b561 100644 --- a/include/stdx/span.hpp +++ b/include/stdx/span.hpp @@ -236,6 +236,12 @@ class span : public detail::span_base { } }; +template +[[nodiscard]] constexpr auto get(span s) -> decltype(s[I]) { + static_assert(E == dynamic_extent or I < E, "get() out of range on span"); + return s[I]; +} + // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) template auto as_bytes(span s) noexcept { if constexpr (N == dynamic_extent) { diff --git a/include/stdx/tuple.hpp b/include/stdx/tuple.hpp index dd7b792..c5d2edc 100644 --- a/include/stdx/tuple.hpp +++ b/include/stdx/tuple.hpp @@ -3,6 +3,7 @@ #if __cplusplus >= 202002L #include +#include #include #include @@ -408,6 +409,8 @@ tuple_impl(Ts...) template constexpr auto tuple_size_v = T::size(); template constexpr auto tuple_size_v> = N; +template +constexpr auto tuple_size_v> = std::size_t{N}; template using tuple_element_t = typename T::template element_t; diff --git a/include/stdx/tuple_algorithms.hpp b/include/stdx/tuple_algorithms.hpp index a1ba123..f886b18 100644 --- a/include/stdx/tuple_algorithms.hpp +++ b/include/stdx/tuple_algorithms.hpp @@ -140,7 +140,7 @@ template