TupleSlice.hpp
Go to the documentation of this file.
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 /// \file
5 /// Defines functions for slicing tuples
6 
7 #pragma once
8 
9 #include <cstddef>
10 #include <tuple>
11 #include <utility>
12 
13 namespace tuple_impl_detail {
14 template <size_t Start, typename Tuple, size_t... Is>
16 slice_impl(Tuple&& tuple, std::index_sequence<Is...> /*meta*/) noexcept {
17  return {std::get<Is + Start>(std::forward<Tuple>(tuple))...};
18 }
19 } // namespace tuple_impl_detail
20 
21 /// \ingroup UtilitiesGroup
22 /// The subset of elements in `tuple` from index `Start` to (excluding) `Stop`
23 template <size_t Start, size_t Stop, typename Tuple>
24 constexpr auto tuple_slice(Tuple&& tuple) noexcept {
25  constexpr size_t tuple_size = std::tuple_size<std::decay_t<Tuple>>::value;
26  static_assert(
27  Start <= Stop and Stop <= tuple_size,
28  "'Start' and 'Stop' must satisfy Start <= Stop <= size of 'Tuple'");
29  return tuple_impl_detail::slice_impl<Start>(
30  std::forward<Tuple>(tuple), std::make_index_sequence<Stop - Start>{});
31 }
32 
33 /// \ingroup UtilitiesGroup
34 /// The first `Size` elements in `tuple`
35 template <size_t Size, typename Tuple>
36 constexpr auto tuple_head(Tuple&& tuple) noexcept {
37  constexpr size_t tuple_size = std::tuple_size<std::decay_t<Tuple>>::value;
38  static_assert(Size <= tuple_size,
39  "'Size' must not exceed the size of 'Tuple'");
40  return tuple_slice<0, Size>(std::forward<Tuple>(tuple));
41 }
42 
43 /// \ingroup UtilitiesGroup
44 /// The last `Size` elements in `tuple`
45 template <size_t Size, typename Tuple>
46 constexpr auto tuple_tail(Tuple&& tuple) noexcept {
47  constexpr size_t tuple_size = std::tuple_size<std::decay_t<Tuple>>::value;
48  static_assert(Size <= tuple_size,
49  "'Size' must not exceed the size of 'Tuple'");
50  return tuple_slice<tuple_size - Size, tuple_size>(std::forward<Tuple>(tuple));
51 }
utility
std::index_sequence
tuple
cstddef
tuple_tail
constexpr auto tuple_tail(Tuple &&tuple) noexcept
Definition: TupleSlice.hpp:46
tuple_slice
constexpr auto tuple_slice(Tuple &&tuple) noexcept
Definition: TupleSlice.hpp:24
tuple_head
constexpr auto tuple_head(Tuple &&tuple) noexcept
Definition: TupleSlice.hpp:36