Skip to content

Commit bc45b57

Browse files
committed
📚 Add documentation for make_integer_sequence
Problem: - There is no documentation explaining `stdx::make_integer_sequence` and friends. Solution: - Add the docs.
1 parent c2ad984 commit bc45b57

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

docs/utility.adoc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,49 @@ std::uint32_t i;
116116
assert(stdx::is_aligned_with<std::uint32_t>(&i));
117117
----
118118

119+
=== `make_integer_sequence`
120+
121+
`make_integer_sequence` is a class template that derives from
122+
https://en.cppreference.com/w/cpp/utility/integer_sequence.html[`std::make_integer_sequence`].
123+
Likewise, `make_index_sequence` and `index_sequence_for` are analogous with
124+
their standard counterparts. In fact each `stdx` version derives from the `std`
125+
version, so can be used in exactly the same way, for instance:
126+
127+
[source,cpp]
128+
----
129+
auto sum = []<std::size_t... Is>(std::index_sequence<Is...>) {
130+
return (std::size_t{} + ... + Is);
131+
}(stdx::make_index_sequence<3>{});
132+
133+
// sum is 6
134+
----
135+
The difference is that `stdx::make_integer_sequence` implements the tuple
136+
protocol. This means that it is usable with
137+
xref:tuple_algorithms.adoc#_tuple_algorithms_hpp[tuple algorithms]. And when
138+
using the tuple protocol, `stdx::make_index_sequence` produces
139+
`std::integral_constant<std::size_t, I>` rather than `std::size_t{I}`.
140+
141+
[source,cpp]
142+
----
143+
template <std::size_t N>
144+
auto f() {
145+
// use make_index_sequence with tuple algorithms
146+
stdx::unrolled_for_each(
147+
[](auto x) {
148+
// x is std::integral_constant<std::size_t, I> for each I
149+
},
150+
stdx::make_index_sequence<N>{});
151+
152+
// or with C++26, destructure into a pack (optionally constexpr here)
153+
auto [...Is] = stdx::make_index_sequence<N>{};
154+
155+
// std::integral_constant implicitly converts to its contained type...
156+
auto rt_sum = (std::size_t{} + ... + Is);
157+
// ...or can be used at compile time
158+
constexpr auto ct_sum = (std::size_t{} + ... + decltype(Is)::value);
159+
}
160+
----
161+
119162
=== `overload`
120163

121164
`overload` is a struct designed to encapsulate an overload set. It inherits from

0 commit comments

Comments
 (0)