CXXIter 0.2
Loading...
Searching...
No Matches
Constraints.h
1#pragma once
2
3#include <type_traits>
4#include <functional>
5
7namespace CXXIter::util {
9 namespace {
10
11 template <class T, class... Ts>
12 constexpr bool are_same_v = (std::is_same_v<T, Ts> && ...);
13
18 template <typename T, template <typename> typename U>
19 constexpr bool is_template_instance_v = false;
20 template <typename T, template <typename> typename U>
21 constexpr bool is_template_instance_v<U<T>, U> = true;
22
23
28 template<typename T>
29 concept is_hashable = requires(T a) {
30 { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;
31 };
32
39 template<typename TFn, typename... TArgs>
40 concept invocable_byvalue = requires(TFn fn, TArgs... args) {
41 { fn(args...) }; // disallows TArgs&&
42 { fn(std::forward<TArgs>(args)...) }; // disallows TArgs&
43 };
44
45 template<typename T>
46 inline constexpr bool is_const_reference_v = std::is_const_v<std::remove_reference_t<T>>;
47
48 template<typename T> concept is_pair = requires(T pair) {
49 {std::get<0>(pair)} -> std::convertible_to< std::tuple_element_t<0, T> >;
50 {std::get<1>(pair)} -> std::convertible_to< std::tuple_element_t<1, T> >;
51 } && std::tuple_size_v<T> == 2;
52
53 template<typename T> concept is_optional = requires(T optional, typename T::value_type value) {
54 typename T::value_type;
55 {optional.value()} -> std::convertible_to<typename T::value_type>;
56 {optional.value_or(value)} -> std::convertible_to<typename T::value_type>;
57 {optional = value};
58 };
59
60
61 // ################################################################################################
62 // CONTAINER CONSTRAINTS
63 // ################################################################################################
64
65 template<typename TContainer>
66 concept ReservableContainer = requires(TContainer container, size_t newSize) {
67 container.reserve(newSize);
68 };
69
73 template<typename TContainer, typename TItem>
74 concept BackInsertableContainer = requires(TContainer& container, TItem item) {
75 typename TContainer::value_type;
76 container.push_back(item);
77 };
78
82 template<template<typename...> typename TContainer, typename TItem, typename... TContainerArgs>
83 concept BackInsertableContainerTemplate = BackInsertableContainer<TContainer<TItem, TContainerArgs...>, TItem>;
84
85
89 template<typename TContainer, typename TItem>
90 concept InsertableContainer = requires(TContainer& container, TItem item) {
91 typename TContainer::value_type;
92 container.insert(item);
93 };
97 template<template<typename...> typename TContainer, typename TItem, typename... TContainerArgs>
98 concept InsertableContainerTemplate = InsertableContainer<TContainer<TItem, TContainerArgs...>, TItem>;
99
103 template<typename TContainer, typename TItemKey, typename TItemValue>
104 concept AssocContainer = requires(TContainer& container, std::pair<TItemKey, TItemValue> item) {
105 typename TContainer::value_type;
106 typename TContainer::key_type;
107 typename TContainer::mapped_type;
108 container.insert(item);
109 };
113 template<template<typename...> typename TContainer, typename TItemKey, typename TItemValue, typename... TContainerArgs>
114 concept AssocContainerTemplate = AssocContainer<TContainer<TItemKey, TItemValue, TContainerArgs...>, TItemKey, TItemValue>;
115
119 template<typename TContainer, typename TItem>
120 concept StdArrayContainer = requires(TContainer& container, size_t idx, TItem item) {
121 typename TContainer::value_type;
122 typename TContainer::size_type;
123 {container[idx] = item};
124 {container.max_size()} -> std::same_as<typename TContainer::size_type>;
125 {container.fill(item)};
127
132 template<typename TContainer>
133 concept ContiguousMemoryContainer = requires(TContainer& container) {
134 typename TContainer::value_type;
135 typename TContainer::size_type;
136 {container.data()} -> std::same_as<typename TContainer::value_type*>;
137 };
138
139 }
140}
Concept enforcing an associative container like std::map.
Definition: Constraints.h:114
Concept enforcing an associative container like std::map.
Definition: Constraints.h:104
Concept enforcing a back-insertible container like std::vector.
Definition: Constraints.h:83
Concept enforcing a back-insertible container like std::vector.
Definition: Constraints.h:74
Concept enforcing TContainer to be a container based on a contiguous chunk of memory containing all e...
Definition: Constraints.h:133
Concept enforcing an insertible container like std::set.
Definition: Constraints.h:98
Concept enforcing an insertible container like std::set.
Definition: Constraints.h:90
Concept enforcing a std::array<,> template instantiation.
Definition: Constraints.h:120
Concept that checks whether the given invocable type TFn is a function-type that accepts the paramete...
Definition: Constraints.h:40
Concept that checks whether the given type T has an implementation of std::hash<T>::operator()
Definition: Constraints.h:29