Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
tuple_cat.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TUPLE_TUPLE_CAT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TUPLE_TUPLE_CAT_HPP_
7
8// IWYU pragma: private, include "arene/base/tuple.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/stdlib_choice/cstddef.hpp"
17#include "arene/base/stdlib_choice/declval.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19#include "arene/base/stdlib_choice/forward.hpp"
20#include "arene/base/stdlib_choice/forward_as_tuple.hpp"
21#include "arene/base/stdlib_choice/integer_sequence.hpp"
22#include "arene/base/stdlib_choice/remove_cv.hpp"
23#include "arene/base/stdlib_choice/remove_reference.hpp"
24#include "arene/base/stdlib_choice/tuple.hpp"
25#include "arene/base/stdlib_choice/tuple_size.hpp"
26#include "arene/base/tuple/detail/as_type_list.hpp"
27#include "arene/base/tuple/detail/get.hpp"
33// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
34
35namespace arene {
36namespace base {
37
38namespace tuple_cat_detail {
39
40/// @brief helper binding tuple_index positions to their corresponding tuple sizes
41/// @tparam Is pack of tuple_index values (one per input tuple)
42/// @tparam Sizes... sizes of the input tuples, in positional correspondence with @c Is
43template <class Is, std::size_t... Sizes>
44class tuple_index_seq_impl;
45
46/// @brief tuple_index_seq_impl specialization that concatenates one run per input tuple
47/// @tparam Is pack of tuple_index values (one per input tuple)
48/// @tparam Sizes... sizes of the input tuples, in positional correspondence with @c Is
49///
50/// @note Prepending an empty @c std::index_sequence<> seed makes the empty-pack case
51/// well-formed, as @c integer_sequence_cat rejects an empty pack.
52template <std::size_t... Is, std::size_t... Sizes>
53class tuple_index_seq_impl<std::index_sequence<Is...>, Sizes...> {
54 public:
55 /// @brief The resulting integer sequence
57};
58
59/// @brief Generate the @c tuple_index sequence, used as the first index into the @c bundle
60/// @tparam Sizes... sizes of the input tuples, in order
61///
62/// For each input tuple at position @c k in the bundle (with size @c Sizes[k]), emit the
63/// value @c k repeated @c Sizes[k] times; the resulting runs are concatenated into a single
64/// flat sequence whose length equals the total number of output elements.
65template <std::size_t... Sizes>
66using tuple_index_seq_t = typename tuple_index_seq_impl<std::make_index_sequence<sizeof...(Sizes)>, Sizes...>::type;
67
68/// @brief helper producing the flat element_index sequence
69/// @tparam Sizes... sizes of the input tuples, in order
70///
71/// @note Prepending an empty @c std::index_sequence<> seed makes the empty-pack case
72/// well-formed, as @c integer_sequence_cat rejects an empty pack.
73template <std::size_t... Sizes>
74class element_index_seq_impl {
75 public:
76 /// @brief The resulting integer sequence
78};
79
80/// @brief Generate the @c element_index sequence, used as the second index into the @c bundle
81/// @tparam Sizes... sizes of the input tuples, in order
82///
83/// For each input tuple at position @c k in the bundle (with size @c Sizes[k]), emit the sequence <c>[0, 1, ...,
84/// Sizes[k] - 1]</c>; the resulting runs are concatenated into a single flat sequence whose length equals the total
85/// number of output elements.
86template <std::size_t... Sizes>
87using element_index_seq_t = typename element_index_seq_impl<Sizes...>::type;
88
89/// @brief result of concatenating the element types of tuple-likes @c Ts...
90/// @tparam Ts... tuple-like types
91template <class... Ts>
92using tuple_cat_result_t = type_lists::concat_t<std::tuple<>, tuple_detail::as_type_list_t<Ts>...>;
93
94/// @brief function object implementing tuple concatenation
95///
96/// Rather than a recursive approach, this implementation builds the resulting tuple in a single pack expansion.
97///
98/// Start by creating a single tuple-of-tuples (the @c bundle) from the list of input tuples. This bundle can be treated
99/// as a jagged 2D array (an array where each row can have different length), which can be indexed into with two
100/// coordinates. By generating a list of coordinates which covers each item in the bundle, each element in the bundle
101/// can be fetched and used to build the resulting tuple in a single expansion.
102///
103/// Two parallel <c>std::index_sequence</c>s can be used to represent the 2d coordinates. Both sequences must be of
104/// equal length @c K (where @c K is the total number of output elements). The first @c std::index_sequence holds the
105/// @c tuple_index - the first index into the @c bundle. The second @c std::index_sequence holds the @c element_index -
106/// the index into the particular tuple at @c tuple_index of the bundle.
107///
108/// The @c tuple_index @c std::index_sequence is generated by repeating the tuple's position in the bundle a number of
109/// times equal to that tuple's size. The @c element_index sequence is generated by concatenating the result of @c
110/// std::index_sequence_for for each tuple in the bundle.
111///
112/// For example, for <c>tuple_cat(tuple<A,B>, tuple<C>, tuple<D,E,F>)</c>:
113///
114/// @code
115/// output index k : 0 1 2 3 4 5
116/// tuple_index[k] : 0, 0, 1, 2, 2, 2
117/// element_index[k] : 0, 1, 0, 0, 1, 2
118/// @endcode
119///
120/// Once both index sequences are created, they can be simultaneously expanded to generate the final tuple. To continue
121/// the example:
122/// @code
123/// (0,0) -> A
124/// (0,1) -> B
125/// (1,0) -> C
126/// (2,0) -> D
127/// (2,1) -> E
128/// (2,2) -> F
129/// @endcode
130///
131/// Credit for this algorithm goes to Stephen T. Lavavej in the implementation of @c std::tuple_cat for MSVC.
132/// This particular implementation is inspired by Eric Niebler's implemetation
133/// @see https://ericniebler.com/2014/11/13/tiny-metaprogramming-library/
134// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
135class tuple_cat_fn {
136 private:
137 /// @brief concatenation implementation over a bundle of forwarded tuples
138 /// @tparam Result @c std::tuple type carrying the final element types
139 /// @tparam Bundle @c std::tuple holding forwarded input tuples
140 /// @tparam TupleIndices value pack: for each output position, which input tuple to read from
141 /// @tparam ElementIndices value pack: for each output position, which slot within that tuple
142 /// @param bundle bundle of tuples
143 /// @return concatenated @c std::tuple
144 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
145 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: The parameter bundles is used"
146 template <class Result, class Bundle, std::size_t... TupleIndices, std::size_t... ElementIndices>
147 static constexpr auto
149 noexcept(Result{tuple_detail::get<ElementIndices>(tuple_detail::get<TupleIndices>(std::declval<Bundle>()))...})
150 ) -> Result {
151 return Result{tuple_detail::get<ElementIndices>(tuple_detail::get<TupleIndices>(std::forward<Bundle>(bundle)))...};
152 }
153 // parasoft-end-suppress AUTOSAR-A0_1_4-a
154 // parasoft-end-suppress CERT_C-EXP37-a
155
156 public:
157 /// @brief concatenate zero or more tuple-ish inputs
158 /// @tparam Ts tuple-ish input types
159 /// @param args tuple-ish arguments
160 /// @return @c std::tuple formed by concatenating the elements of each input in order;
161 /// an empty @c std::tuple<> when called with no arguments
162 ///
163 /// @note Constraints: <br>
164 /// * Every input must satisfy @c arene::base::is_tuple_like_v
165 ///
166 /// @note A type implements the tuple protocol if it
167 /// * provides a specialization of @c std::tuple_size
168 /// * provides a specialization of @c std::tuple_element
169 /// * provides a function to get an element by index @c I, which is specified
170 /// as:
171 /// ** <c> std::forward<Tuple>(tuple).get<I>() </c> if valid
172 /// ** otherwise, <c> get<I>(std::forward<Tuple>(tuple)) </c> if valid,
173 /// where @c get is looked up by ADL only
174 /// Note that the tuple-like concept defines types that satisfy the tuple
175 /// protocol but are restricted to a list of types defined in the @c std
176 /// namespace (@c std::array, @c std::pair, @c std::tuple in C++14). The term
177 /// tuple-ish is used to define types that satisfy the tuple protocol without
178 /// the restriction that the type is define in the @c std namespace -- which
179 /// includes user-defined types.
180 template <
181 class... Ts,
183 class Result = tuple_cat_result_t<remove_cvref_t<Ts>...>,
184 class TupleIndices = tuple_index_seq_t<std::tuple_size<remove_cvref_t<Ts>>::value...>,
185 class ElementIndices = element_index_seq_t<std::tuple_size<remove_cvref_t<Ts>>::value...>>
186 constexpr auto operator()(Ts&&... args) const
187 noexcept(noexcept(impl<Result>(std::declval<std::tuple<Ts&&...>>(), TupleIndices{}, ElementIndices{})))
188 -> Result {
189 return impl<Result>(std::forward_as_tuple(std::forward<Ts>(args)...), TupleIndices{}, ElementIndices{});
190 }
191};
192// parasoft-end-suppress AUTOSAR-M2_10_1-a
193
194} // namespace tuple_cat_detail
195
196/// @brief Concatenate tuple-like values into a single tuple.
197// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
198// object used in multiple TUs."
199// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
200// object used in multiple TUs."
201ARENE_CPP14_INLINE_VARIABLE(tuple_cat_detail::tuple_cat_fn, tuple_cat);
202// parasoft-end-suppress AUTOSAR-M7_3_3-a
203// parasoft-end-suppress CERT_CPP-DCL59-a
204
205} // namespace base
206} // namespace arene
207
208#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TUPLE_TUPLE_CAT_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Provides the is_tuple_like_v<T> and is_pair_like_v<T> traits.
typename concat< Ln... >::type concat_t
collapse L0<T...>, .. Ln<U...> into TypeList<T..., .., U...>
Definition concat.hpp:107
Definition accumulate.hpp:37
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
repeat_type_t< Count, make_index_sequence_repeat_n_detail::to_index_sequence, std::integral_constant< std::size_t, Value > > make_index_sequence_repeat_n
An std::index_sequence whose elements are Count copies of Value.
Definition make_index_sequence_repeat_n.hpp:32
typename integer_sequence_ops_detail::integer_sequence_cat_impl< Sequences... >::type integer_sequence_cat
Produces a single std::integer_sequence which is the concatenation of multiple std::integer_sequence.
Definition integer_sequence_ops.hpp:74
Definition accumulate.hpp:36
constexpr auto forward_as_tuple(ArgTs &&... args) noexcept -> tuple< ArgTs &&... >
construct a tuple of references to the provided arguments suitable for forwarding as arguments to a f...
Definition forward_as_tuple.hpp:31
make_integer_sequence< size_t, Size > make_index_sequence
A helper alias template for make_integer_sequence<size_t, Size>
Definition integer_sequence.hpp:224
integer_sequence< size_t, Ints... > index_sequence
A helper alias template for integer_sequence<size_t, Ints>
Definition integer_sequence.hpp:218
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24
Provides the all_of_v<bool...> trait.