-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack_find.hpp
49 lines (43 loc) · 1.05 KB
/
pack_find.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
#pragma once
#include<utility>
#include<cstddef>
namespace mlib
{
template<auto A, auto...B>
struct first
{
constexpr auto operator()()
{
return A;
}
};
template<auto Index, auto value, auto value_two>
struct value_is_value
{
constexpr auto operator()() const
{
if constexpr (decltype(value_two)(value) == value_two)
{
return Index;
}
else
{
return 0;
}
}
constexpr value_is_value() {}
};
template<auto value_to_find, auto... pack>
constexpr auto pack_find()
{
constexpr auto x = [&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
return (value_is_value<indexes, pack, value_to_find>{}() + ...);
}(std::make_index_sequence<sizeof...(pack)>{});
if constexpr ((x == 0) && mlib::first<pack...>{}() != value_to_find)
{
return -1;
}
return x;
}
} // namespace mlib