-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_tuple.hpp
78 lines (60 loc) · 1.5 KB
/
lambda_tuple.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef LAMBDA_TUPLE_HPP
#define LAMBDA_TUPLE_HPP
#include <type_traits>
#include <utility>
namespace util
{
namespace type
{
// clang-format off
constexpr auto tuple = [] [[nodiscard]] (auto... args) {
return [=] [[nodiscard]] (auto fn) { return fn(args...); };
};
// clang-format on
template<::std::size_t N, class... Args>
struct nth_type;
template<class T, class... Args>
struct nth_type<0, T, Args...> : ::std::type_identity<T>
{
};
template<::std::size_t N, class T, class... Args>
struct nth_type<N, T, Args...> : nth_type<N - 1, Args...>
{
};
template<auto N>
[[nodiscard]] constexpr auto get(auto t)
{
auto func = []<class... Args>(Args... args) {
typename nth_type<N, Args...>::type result;
auto impl = [Count = 0](auto input, auto& result) mutable {
if (Count == N)
{
result = input;
}
++Count;
};
(impl(args, result), ...);
return result;
};
return t(func);
}
template<class T>
[[nodiscard]] constexpr auto get(auto t)
{
auto func = []<class... Args>(Args... args) {
T result;
auto impl = [has_val = false]<class Arg>(Arg input, auto& result) mutable {
if (::std::is_same_v<T, Arg> && not has_val)
{
result = input;
has_val = true;
}
};
(impl(args, result), ...);
return result;
};
return t(func);
}
} // namespace type
} // namespace util
#endif // !LAMBDA_TUPLE_HPP