Thoth
A functional, expressive, asynchronous C++26 webdev library
Loading...
Searching...
No Matches
LastMatchVariant.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <variant>
4
5namespace Thoth::Utils {
6 template<class T, class Variant, template<class, class> class Relation, std::size_t Index = 0>
7 consteval size_t FindMatchIndexImpl() {
8 constexpr size_t size{ std::variant_size_v<Variant> };
9 static_assert(Index != size, "No type in the variant satisfies the specified relation.");
10
11 if constexpr (Relation<T, std::variant_alternative_t<Index, Variant>>::value)
12 return Index;
13
14
15 return FindMatchIndexImpl<T, Variant, Relation, Index + 1>();
16 }
17
18 template<class T, class Variant, template<class, class> class Relation>
19 consteval std::size_t FindMatchIndex() {
20 return FindMatchIndexImpl<T, Variant, Relation>();
21 }
22
23
24 template<class T, class Variant>
25 using FirstConvertibleVariant = std::variant_alternative_t<FindMatchIndex<T, Variant, std::is_convertible>(), Variant>;
26
27 template<class T, class Variant>
28 using FirstEqualVariant = std::variant_alternative_t<FindMatchIndex<T, Variant, std::is_same>(), Variant>;
29}
Definition Env.hpp:5
std::variant_alternative_t< FindMatchIndex< T, Variant, std::is_convertible >(), Variant > FirstConvertibleVariant
Definition LastMatchVariant.hpp:25
consteval size_t FindMatchIndexImpl()
Definition LastMatchVariant.hpp:7
consteval std::size_t FindMatchIndex()
Definition LastMatchVariant.hpp:19
std::variant_alternative_t< FindMatchIndex< T, Variant, std::is_same >(), Variant > FirstEqualVariant
Definition LastMatchVariant.hpp:28