Simple-Utility v2.3.1
Loading...
Searching...
No Matches
Tuple.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_FUNCTIONAL_TUPLE_HPP
7#define SL_UTILITY_FUNCTIONAL_TUPLE_HPP
8
9#pragma once
10
14
15#include <tuple>
16#include <utility>
17
19{
32 template <class T>
33 inline constexpr auto get = envelop<Transform>(
34 []<class Tuple>(Tuple&& v) -> decltype(auto)requires concepts::tuple<std::remove_cvref_t<Tuple>>
35 {
36 using std::get;
37 return get<T>(std::forward<Tuple>(v));
38 });
39
44 template <std::size_t index>
45 inline constexpr auto get_at = envelop<Transform>(
46 []<class Tuple>(Tuple&& v) -> decltype(auto)requires concepts::tuple<std::remove_cvref_t<Tuple>>
47 {
48 using std::get;
49 return get<index>(std::forward<Tuple>(v));
50 });
51}
52
53namespace sl::functional::tuple::detail
54{
55 // workaround
56 // as of MSVC version 19.32.31328.0 the following workaround became necessary.
57 template <class... Args, class Tuple>
59 constexpr std::tuple<Args...> reduce(Tuple&& tuple)
60 {
61 return {tuple::get<Args>(std::forward<Tuple>(tuple))...};
62 }
63
64 // workaround
65 // as of MSVC version 19.32.31328.0 the following workaround became necessary.
66 template <class... Tuples>
67 requires (... && concepts::tuple<std::remove_cvref_t<Tuples>>)
68 constexpr auto concat(Tuples&&... tuples)
69 {
70 return std::tuple_cat(std::forward<Tuples>(tuples)...);
71 }
72}
73
75{
81 template <class... Args>
82 requires concepts::unique_types<Args...>
83 inline constexpr auto reduce = envelop<Transform>(
84 []<class Tuple>(Tuple&& t)
85 requires concepts::tuple<std::remove_cvref_t<Tuple>>
86 {
87 return detail::reduce<Args...>(std::forward<Tuple>(t));
88 });
89
93 inline constexpr auto concat = envelop<Transform>(
94 []<class... Tuples>(Tuples&&... tuples)
95 requires (... && concepts::tuple<std::remove_cvref_t<Tuples>>)
96 {
97 return detail::concat(std::forward<Tuples>(tuples)...);
98 });
99
103 inline constexpr auto tie = envelop<Transform>(
104 [](auto&... args) { return std::tie(args...); });
105
109 template <class To>
110 inline constexpr auto make_from = envelop<Transform>(
111 []<class Tuple>(Tuple&& t) noexcept(noexcept(std::make_from_tuple<To>(std::forward<Tuple>(t))))
112 requires concepts::tuple<std::remove_cvref_t<Tuple>>
113 {
114 // Due to a bug in gcc-10, we must check this requirement in two steps
115 static_assert(
116 requires { { std::make_from_tuple<To>(std::forward<Tuple>(t)) }; },
117 "The object is not constructible with the elements of the given tuple.");
118 static_assert(
119 std::same_as<To, decltype(std::make_from_tuple<To>(std::declval<Tuple>()))>,
120 "The object is not constructible with the elements of the given tuple.");
121
122 return std::make_from_tuple<To>(std::forward<Tuple>(t));
123 });
124
128}
129
130namespace sl::functional
131{
136 template <function Fn>
138}
139
140#endif
The core class, wrapping one functional object and enabling a variety of composing operators for it.
Definition: BasicClosure.hpp:112
Determines whether a type can be used as a tuple-like.
Definition: General.hpp:105
constexpr auto make_from
Constructs an object with elements from the source tuple as constructor arguments.
Definition: Tuple.hpp:110
constexpr auto get
Functional object which retrieves an object of a specific type from a tuple-like argument.
Definition: Tuple.hpp:33
constexpr auto concat
Combines all elements from each given tuple into one tuple.
Definition: Tuple.hpp:93
constexpr auto get_at
Functional object which retrieves an object at a specific index from a tuple-like argument.
Definition: Tuple.hpp:45
constexpr auto reduce
Reduces (or permutes) the components of a tuple and returns a them as new tuple.
Definition: Tuple.hpp:83
constexpr auto tie
Combines all given lvalue references into one tuple.
Definition: Tuple.hpp:103
Definition: Tuple.hpp:19
Definition: Arithmetic.hpp:13