Add CountValue

This commit is contained in:
sha512sum 2024-06-28 14:01:49 +00:00
parent d8063bb39c
commit 475d310f67

View file

@ -10,24 +10,49 @@ namespace impl {
template <typename Tag, std::size_t Value> template <typename Tag, std::size_t Value>
struct TagWithValue {}; struct TagWithValue {};
template <typename Tag, std::size_t I, typename... Ts> template <bool Add, typename Tag, std::size_t I, typename... Ts>
consteval auto Counter() -> std::size_t { consteval auto Counter() -> std::size_t {
if constexpr(requires{Magic(Getter<TagWithValue<Tag, I>{}>{});}) { if constexpr(requires{Magic(Getter<TagWithValue<Tag, I>{}>{});}) {
return Counter<Tag, I + 1, Ts...>(); return Counter<Add, Tag, I + 1, Ts...>();
}; };
if constexpr(Add) {
return (std::ignore = Injector<TagWithValue<Tag, I>{}, 0>{}, I); return (std::ignore = Injector<TagWithValue<Tag, I>{}, 0>{}, I);
} else {
return I;
}; };
};
} // namespace impl; } // namespace impl;
// For incerement counter need a unique Ts... // For incerement counter need a unique Ts...
template < template <
typename Tag, typename Tag,
typename... Ts, typename... Ts,
std::size_t R = impl::Counter<Tag, 0, Ts...>() std::size_t R = impl::Counter<true, Tag, 0, Ts...>()
> >
consteval auto Counter(auto...) -> std::size_t { consteval auto Counter(auto...) -> std::size_t {
return R; return R;
}; };
// Without increment
template <
typename Tag,
typename... Ts,
std::size_t R = impl::Counter<false, Tag, 0, Ts...>()
>
consteval auto CountValue(auto...) -> std::size_t {
return R;
};
/*
static_assert(Counter<void>() == 0);
static_assert(Counter<void, void>() == 1);
static_assert(CountValue<void>() == 2);
static_assert(CountValue<void, void>() == 2);
*/
} // namespace utempl::loopholes } // namespace utempl::loopholes