OpenKalman
tuple_like_to_tuple.hpp
Go to the documentation of this file.
1 /* This file is part of OpenKalman, a header-only C++ library for
2  * Kalman filters and other recursive filters.
3  *
4  * Copyright (c) 2025 Christopher Lee Ogden <ogden@gatech.edu>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
9  */
10 
17 #ifndef OPENKALMAN_TUPLE_LIKE_TO_TUPLE_HPP
18 #define OPENKALMAN_TUPLE_LIKE_TO_TUPLE_HPP
19 
20 #include <type_traits>
21 #include <tuple>
23 
25 {
26  namespace detail
27  {
28 #ifndef __cpp_concepts
29  template<typename T, typename = void>
30  struct is_stl_tuple_like : std::false_type {};
31 
32  template<typename T>
33  struct is_stl_tuple_like<T, std::void_t<decltype(std::tuple_cat(std::declval<T>()))>> : std::true_type {};
34 #endif
35 
36 
37 #ifdef __cpp_concepts
38  template<typename T>
39  concept stl_tuple_like = requires(T t) { std::tuple_cat(t); };
40 #else
41  template<typename T>
42  constexpr bool stl_tuple_like = detail::is_stl_tuple_like<std::decay_t<T>>::value;
43 #endif
44 
45 
46  template<typename Arg, std::size_t...Ix>
47  constexpr auto
48  tuple_like_to_tuple_impl(Arg&& arg, std::index_sequence<Ix...>)
49  {
50  return std::tuple {get(std::forward<Arg>(arg), std::integral_constant<std::size_t, Ix>{})...};
51  }
52  } // namespace detail
53 
54 
61 #ifdef __cpp_concepts
62  template<tuple_like Arg>
63  constexpr detail::stl_tuple_like decltype(auto)
64 #else
65  template<typename Arg, std::enable_if_t<tuple_like<Arg>, int> = 0>
66  constexpr decltype(auto)
67 #endif
69  {
70  if constexpr (detail::stl_tuple_like<Arg>)
71  return std::forward<Arg>(arg);
72  else
73  return detail::tuple_like_to_tuple_impl(std::forward<Arg>(arg), std::make_index_sequence<std::tuple_size_v<std::decay_t<Arg>>>{});
74  }
75 
76 } // namespace OpenKalman::internal
77 
78 #endif //OPENKALMAN_TUPLE_LIKE_TO_TUPLE_HPP
Definition for collections::tuple_like.
Definition: tuple_reverse.hpp:103
constexpr bool value
T is numerical value or is reducible to a numerical value.
Definition: value.hpp:31
decltype(auto) constexpr tuple_like_to_tuple(Arg &&arg)
Convert a tuple_like object to a std::tuple or equivalent.
Definition: tuple_like_to_tuple.hpp:68
Definition: tuple_like_to_tuple.hpp:24