-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinlin_if.hpp
72 lines (59 loc) · 2.08 KB
/
inlin_if.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
#pragma once
namespace mlib
{
struct SameAs {};
// this is a bad implementation, becuase it requires operator==
template <typename A, typename B> struct is_same {
static constexpr bool value = false;
};
template <typename A, typename B>
requires(A{} == B{})
struct is_same<A, B> {
static constexpr bool value = true;
};
template<auto T, auto B>
struct is_same_value
{
static constexpr auto value = (T == B);
};
template <typename T, typename condition, typename ToCompare, auto first_,
auto second_>
struct inline_if {
constexpr inline_if() {}
constexpr auto get() {
}
constexpr auto operator()() { return get(); }
static constexpr auto first = first_;
static constexpr auto second = second_;
};
template<typename T, typename ToCompare, auto first_, auto second_>
struct inline_if<T, is_same<T, ToCompare>, ToCompare, first_, second_>
{
constexpr inline_if() {}
constexpr auto get() {
if constexpr (is_same<T, ToCompare>::value) {
return first;
}
else if constexpr (!is_same<T, ToCompare>::value) {
return second;
}
constexpr auto operator()() { return get(); }
static constexpr auto first = first_;
static constexpr auto second = second_;
};
template<auto T, auto ToCompare, auto first_, auto second_>
struct inline_if<T, is_same_value<T, ToCompare>, ToCompare, first_, second_>
{
constexpr inline_if() {}
constexpr auto get() {
if constexpr (is_same<T, ToCompare>::value) {
return first;
}
else if constexpr (!is_same<T, ToCompare>::value) {
return second;
}
constexpr auto operator()() { return get(); }
static constexpr auto first = first_;
static constexpr auto second = second_;
};
} // namespace mlib