-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsteval_filter.hpp
More file actions
37 lines (31 loc) · 854 Bytes
/
consteval_filter.hpp
File metadata and controls
37 lines (31 loc) · 854 Bytes
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
#ifndef CONSTEVAL_FILTER_HPP
#define CONSTEVAL_FILTER_HPP
#include <array>
#include <tuple>
#include <utility>
#include <vector>
namespace util {
namespace range {
constexpr auto filter(auto t, auto fn) {
// get the vector of filtered elements
constexpr auto r = [=] {
return std::apply(
[=](auto... ts) {
return [=] {
auto v = std::vector{ts...};
std::erase_if(v, std::not_fn(fn));
return v;
};
},
t()
);
}();
// convert the vector to a tuple
return [=]<auto... Ns>(std::index_sequence<Ns...>) {
const auto v = r();
return std::tuple{v[Ns]...};
}(std::make_index_sequence<r().size()>{});
}
} // namespace range
} // namespace util
#endif // !CONSTEVAL_FILTER_HPP