TuringSim
C++ framework to simulate abstract computing models
tuple_transform.h
1 #pragma once
2 
3 #include <tuple>
4 
5 namespace TuringSim::Utils {
6  namespace {
7  template<size_t N, typename Src, typename Destination, typename Func>
8  [[maybe_unused]]constexpr void tuple_call_assign(Src const& source, Destination& destination, Func f) {
9  std::get<N>(destination) = f(std::get<N>(source));
10  }
11 
12  template<typename Src, typename Destination, typename Func, size_t... Is>
13  [[maybe_unused]]constexpr void tuple_transform_index(Src const& source, Destination& destination, Func f, std::index_sequence<Is...>) {
14  using expander = int[]; /* Useful to ensure sequence point in evaluation of the expansion below */
15  (void) expander{0, (tuple_call_assign<Is>(source, destination, f), 0)...};
16  }
17  }
18 
27  template<typename... Src, typename... Destination, typename Func>
28  constexpr void tuple_transform(std::tuple<Destination...>& destination, Func f, std::tuple<Src...> const& source) {
29  static_assert(sizeof...(Src) == sizeof...(Destination), "Tuples must have the same length");
30  tuple_transform_index<>(source, destination, f, std::index_sequence_for<Src...>());
31  }
32 }
TuringSim::Utils
The namespace for basic function, not specific to TuringSim.
TuringSim::Utils::tuple_transform
constexpr void tuple_transform(std::tuple< Destination... > &destination, Func f, std::tuple< Src... > const &source)
Apply f to each element of source and store the result into destination.
Definition: tuple_transform.h:28