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
|
#ifndef RFL_INTERNAL_NTH_ELEMENT_HPP_
#define RFL_INTERNAL_NTH_ELEMENT_HPP_
#include <tuple>
#include <utility>
namespace rfl::internal {
template <class T, int N, int _i>
struct ElementWrapper {
T t_;
};
template <int N, class T1, int _i1, class T2, int _i2>
constexpr auto operator|(const ElementWrapper<T1, N, _i1>& _e1,
const ElementWrapper<T2, N, _i2>& _e2) {
if constexpr (_i1 == N) {
return _e1;
} else {
return _e2;
}
}
template <class Head, class... Tail>
constexpr auto find_matching_element(const Head& _head, const Tail&... _tail) {
return (_head | ... | _tail);
};
template <int N, class... Ts, int... _is>
constexpr auto wrap_elements(Ts... _ts, std::integer_sequence<int, _is...>) {
return find_matching_element(ElementWrapper<Ts, N, _is>{_ts}...).t_;
}
template <int N, class... Ts>
constexpr auto nth_element(Ts... _ts) {
static_assert(N >= 0, "N out of bounds.");
static_assert(N < sizeof...(Ts), "N out of bounds.");
return wrap_elements<N, Ts...>(
_ts..., std::make_integer_sequence<int, sizeof...(Ts)>());
}
} // namespace rfl::internal
#endif
|