Templa 0.0.1-alpha
C++ Metaprogramming Utilities
Loading...
Searching...
No Matches
pack.hpp
Go to the documentation of this file.
1#pragma once
2#include "concepts.hpp"
3#include <iostream>
7
12namespace templa::internal
13{
17
21 namespace hidden
22 {
27 template <typename... Ts>
28 struct pack;
29
35 template <typename T, T... elems>
37 }
38
44 template <typename... Ts>
45 struct type_list
46 {
48 using type = hidden::pack<Ts...>;
49
51 constexpr static size_t size = sizeof...(Ts);
52 };
53
60 template <template <typename...> class T, typename... Ts>
61 struct type_list<T<Ts...>>
62 {
64 using type = type_list<Ts...>;
65 };
66
74 template <typename T, std::size_t N, T... elems>
76 {
78 constexpr static std::array<T, N> array = {elems...};
79
81 constexpr static std::size_t size = N;
82
84 using type = T;
85 };
86
92 template <auto a>
93 requires(concepts::Container<std::remove_cv_t<decltype(a)>>)
95 {
97 using type = decltype(a)::value_type;
98
103 constexpr static auto value = []<std::size_t... I>(std::index_sequence<I...>) consteval
104 {
105 return decltype(a){a[I]...};
106 }(std::make_index_sequence<a.size()>{});
107
109 constexpr static std::size_t size = value.size();
110 };
111
119 template <auto... elems>
121 {
122 public:
124 constexpr static std::size_t size = sizeof...(elems);
125
127 constexpr static bool valid = (std::is_same_v<std::decay_t<decltype(elems)>, std::decay_t<decltype(std::get<0>(std::tuple{elems...}))>> && ...) &&
128 (concepts::Comparable<decltype(elems)> && ...);
129
130 static_assert(valid,
131 "elements must be of uniform type and comparable");
132
133 private:
135 constexpr static auto lambda =
136 []<std::size_t... I>(std::index_sequence<I...>)
137 {
138 constexpr std::tuple<decltype(elems)...> tup{elems...};
139 return std::get<0>(tup);
140 };
141
142 public:
144 using uniform_type = typename std::array<decltype(lambda(std::make_index_sequence<size>{})), size>;
145
147 using value_type = typename uniform_type::value_type;
148
150 constexpr static uniform_type identity_value{elems...};
151 };
152
153} // namespace templa::internal
Concepts.
Concept to check if a type supports common comparison operators.
Definition concepts.hpp:57
Concept that models an STL-like container.
Definition concepts.hpp:245
Internal utilities and implementation details.
Definition pack.hpp:22
Extract elements from a container-like template parameter as a new container.
Definition pack.hpp:95
decltype(a)::value_type type
Value type of the container.
Definition pack.hpp:97
static constexpr std::size_t size
Number of elements in the generated container.
Definition pack.hpp:109
static constexpr auto value
Compile-time generated container with the elements of a. Uses index_sequence to access each element.
Definition pack.hpp:103
Forward a list of elements as a std::array.
Definition pack.hpp:76
T type
The type of elements.
Definition pack.hpp:84
static constexpr std::array< T, N > array
Static array holding the elements.
Definition pack.hpp:78
static constexpr std::size_t size
Number of elements.
Definition pack.hpp:81
A template pack holding a list of values of type T.
Definition pack.hpp:36
A template pack to hold a variadic list of types.
Definition pack.hpp:28
type_list< Ts... > type
Type alias for the type_list containing the unpacked types.
Definition pack.hpp:64
A compile-time list of types.
Definition pack.hpp:46
static constexpr size_t size
Definition pack.hpp:51
hidden::pack< Ts... > type
Type alias for the internal pack of types.
Definition pack.hpp:48
Uniform element identity helper for a list of values.
Definition pack.hpp:121
static constexpr uniform_type identity_value
Static array holding the elements.
Definition pack.hpp:150
typename uniform_type::value_type value_type
Value type of the uniform_type.
Definition pack.hpp:147
typename std::array< decltype(lambda(std::make_index_sequence< size >{})), size > uniform_type
Type alias for the uniform std::array of elements.
Definition pack.hpp:144
static constexpr std::size_t size
Number of elements.
Definition pack.hpp:124
static constexpr bool valid
Check if all elements have the same decayed type and are comparable.
Definition pack.hpp:127