Simple-Utility v2.3.1
Loading...
Searching...
No Matches
General.hpp
Go to the documentation of this file.
1// Copyright Dominic Koepke 2019 - 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef SL_UTILITY_TUPLE_GENERAL_HPP
7#define SL_UTILITY_TUPLE_GENERAL_HPP
8
9#pragma once
10
11#include <concepts>
12#include <tuple>
13#include <type_traits>
14#include <utility>
15
17
18namespace sl::tuple
19{
23}
24
25namespace sl::tuple::detail
26{
27 // use an explicit type here, because a simple lambda type seems to confuse resharper a lot!
28 template <std::size_t index>
29 struct get_fn
30 {
31 template <class Tuple>
32 [[nodiscard]]
33 constexpr decltype(auto) operator ()(Tuple&& tuple) const noexcept
34 {
35 using std::get;
36 return get<index>(std::forward<decltype(tuple)>(tuple));
37 }
38 };
39
40 template <std::size_t index>
41 inline constexpr get_fn<index> get{};
42
43 template <std::size_t index, class Tuple>
44 struct get_result
45 {
46 using type = decltype(get<index>(std::declval<Tuple>()));
47 };
48
49 template <std::size_t index, class Tuple>
50 using get_result_t = typename get_result<index, Tuple>::type;
51}
52
53namespace sl::concepts::detail
54{
55 template <std::size_t index, class Tuple>
56 concept tuple_index = std::convertible_to<
57 tuple::detail::get_result_t<index, Tuple&>,
58 const std::tuple_element_t<index, Tuple>&>
59 && std::convertible_to<
60 tuple::detail::get_result_t<index, const Tuple&>,
61 const std::tuple_element_t<index, Tuple>&>
62 && std::convertible_to<
63 tuple::detail::get_result_t<index, Tuple&&>,
64 const std::tuple_element_t<index, Tuple>&&>
65 && std::convertible_to<
66 tuple::detail::get_result_t<index, const Tuple&&>,
67 const std::tuple_element_t<index, Tuple>&&>;
68
69 template <class Tuple>
70 inline constexpr bool tuple_indices_v = []<std::size_t... indices>([[maybe_unused]] std::index_sequence<indices...>)
71 {
72 return (... && tuple_index<indices, Tuple>);
73 }(std::make_index_sequence<std::tuple_size_v<Tuple>>{});
74
75 template <class Func, class Tuple>
76 inline constexpr bool applicable_v = []<std::size_t... indices>([[maybe_unused]] std::index_sequence<indices...>)
77 {
78 return std::is_invocable_v<
79 Func,
80 tuple::detail::get_result_t<indices, Tuple>...>;
81 }(std::make_index_sequence<std::tuple_size_v<std::remove_cvref_t<Tuple>>>{});
82
83 template <class Func, class Tuple>
84 inline constexpr bool nothrow_applicable_v = []<std::size_t... indices>([[maybe_unused]] std::index_sequence<indices...>)
85 {
86 return std::is_nothrow_invocable_v<
87 Func,
88 tuple::detail::get_result_t<indices, Tuple>...>;
89 }(std::make_index_sequence<std::tuple_size_v<std::remove_cvref_t<Tuple>>>{});
90}
91
92namespace sl::concepts
93{
104 template <class Tuple>
106 && detail::tuple_indices_v<Tuple>;
107
115 template <class Func, class Tuple>
117 && detail::applicable_v<Func, Tuple>;
118
126 template <class Func, class Tuple>
129 && detail::nothrow_applicable_v<Func, Tuple>;
130}
131
132namespace sl::tuple
133{
151 template <class Func, class Tuple>
154 {
155 using type = decltype(std::apply(std::declval<Func>(), std::declval<Tuple>()));
156 };
157
164 template <class Func, class Tuple>
166
178 template <class Tuple, class... Others>
182 {
183 using type = decltype(std::tuple_cat(
184 std::declval<Tuple>(),
185 std::declval<Others>()...
186 ));
187 };
188
193 template <class... Tuples>
194 using tuple_cat_result_t = typename cat_result<Tuples...>::type;
195
197}
198
199#endif
Determines whether the function is invocable with the elements of the given tuple.
Definition: General.hpp:116
Determines whether the function is invocable with the elements of the given tuple without throwing.
Definition: General.hpp:127
Determines whether a type can be used as a tuple-like.
Definition: General.hpp:105
Determines whether a type satisfies the requirements of a type-list.
Definition: TypeList.hpp:103
constexpr auto get
Functional object which retrieves an object of a specific type from a tuple-like argument.
Definition: Tuple.hpp:33
typename apply_result< Func, Tuple >::type apply_result_t
Alias type determining the result of a std::apply call.
Definition: General.hpp:165
typename cat_result< Tuples... >::type tuple_cat_result_t
Alias type determining the result of a std::tuple_cat call.
Definition: General.hpp:194
Definition: operators.hpp:15
Definition: Algorithm.hpp:18
Trait type determining the result of a std::apply call.
Definition: General.hpp:154
decltype(std::apply(std::declval< Func >(), std::declval< Tuple >())) type
Definition: General.hpp:155
Trait type determining the result of a std::tuple_cat call.
Definition: General.hpp:182
decltype(std::tuple_cat(std::declval< Tuple >(), std::declval< Others >()...)) type
Definition: General.hpp:186