opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
typelist.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <cstddef>
5#include <utility>
6#include <variant>
7
8namespace osc
9{
10 // inspired by: https://codereview.stackexchange.com/questions/269320/c17-typelist-manipulation
11 //
12 // ... which was inspired by the book: "Modern C++ Design" (A. Alexandrescu, 2002)
13
14 template<typename...>
15 struct Typelist;
16
17 template<>
18 struct Typelist<> {};
19
20 template<typename Head, typename... Tails>
21 struct Typelist<Head, Tails...> {
22 using head = Head;
23 using tails = Typelist<Tails...>;
24
25 template<typename T>
26 static constexpr bool contains() noexcept
27 {
28 const auto f = []<typename... Ts>(Typelist<Ts...>)
29 {
30 return (std::same_as<T, Ts> or ...);
31 };
32 return f(Typelist{});
33 }
34 };
35
36 // Typelist: size (the number of types held within)
37
38 template<typename TList>
40
41 template<typename... Types>
43 static constexpr size_t value = sizeof...(Types);
44 };
45
46 template<typename TList>
47 inline constexpr size_t TypelistSizeV = TypelistSize<TList>::value;
48
49 // Typelist: indexed access
50
51 template<typename TList, size_t Index>
52 struct TypeAt;
53
54 template<typename Head, typename... Tails>
55 struct TypeAt<Typelist<Head, Tails...>, 0> {
56 using type = Head;
57 };
58
59 template<typename Head, typename... Tails, size_t Index>
60 struct TypeAt<Typelist<Head, Tails...>, Index> {
61 static_assert(Index < sizeof...(Tails)+1, "index out of range");
62 using type = typename TypeAt<Typelist<Tails...>, Index-1>::type;
63 };
64
65 template<typename TList, size_t Index>
67
68 // Typelist-to-std::variant conversion
69 namespace detail
70 {
71 template<typename... Types>
72 auto to_variant(Typelist<Types...>) -> std::variant<Types...>;
73 }
74
75 template<typename TTypelist>
76 using VariantOfTypelistElements = decltype(detail::to_variant(std::declval<TTypelist>()));
77}
auto to_variant(Typelist< Types... >) -> std::variant< Types... >
Definition custom_decoration_generator.h:5
constexpr size_t TypelistSizeV
Definition typelist.h:47
typename TypeAt< TList, Index >::type TypeAtT
Definition typelist.h:66
decltype(detail::to_variant(std::declval< TTypelist >())) VariantOfTypelistElements
Definition typelist.h:76
constexpr U to(T &&value)
Definition conversion.h:56
typename TypeAt< Typelist< Tails... >, Index-1 >::type type
Definition typelist.h:62
Definition typelist.h:52
Definition typelist.h:39
static constexpr bool contains() noexcept
Definition typelist.h:26
Head head
Definition typelist.h:22
Definition typelist.h:18
Definition typelist.h:15