Simple-Utility v2.3.1
Loading...
Searching...
No Matches
stl_extensions.hpp
Go to the documentation of this file.
1// Copyright Dominic Koepke 2019 - 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef SL_UTILITY_CONCEPTS_STL_EXTENSIONS_HPP
7#define SL_UTILITY_CONCEPTS_STL_EXTENSIONS_HPP
8
9#pragma once
10
11#include <compare>
12#include <concepts>
13
14// ReSharper disable CppClangTidyClangDiagnosticDocumentation
15// ReSharper disable CppIdenticalOperandsInBinaryExpression
16// ReSharper disable CppClangTidyMiscRedundantExpression
17
18namespace sl::concepts
19{
34 template <class T>
35 concept pointer = std::is_pointer_v<T>;
36
43 template <class T>
44 concept reference = std::is_reference_v<T>;
45
51 template <class T>
52 concept decayed = std::same_as<std::decay_t<T>, T>;
53
59 template <class T>
60 concept unqualified = std::same_as<std::remove_cvref_t<T>, T>;
61
68 template <class TLhs, class TRhs>
69 concept not_same_as = !std::same_as<TLhs, TRhs>;
70
77 template <class TSource, class TTarget>
78 concept initializes = std::constructible_from<TTarget, TSource>;
79
86 template <class TSource, class TTarget>
87 concept assignable_to = std::assignable_from<TTarget, TSource>;
88
95 template <class T>
96 concept weakly_equality_comparable = requires(const std::remove_cvref_t<T>& t)
97 {
98 { t == t } -> std::convertible_to<bool>;
99 { t != t } -> std::convertible_to<bool>;
100 };
101
108 template <class T>
110 && requires(const std::remove_cvref_t<T>& t)
111 {
112 { t == t } noexcept -> std::convertible_to<bool>;
113 { t != t } noexcept -> std::convertible_to<bool>;
114 };
115
123 template <class T1, class T2>
124 concept weakly_equality_comparable_with = requires(const std::remove_cvref_t<T1>& t1, const std::remove_cvref_t<T2>& t2)
125 {
126 { t1 == t2 } -> std::convertible_to<bool>;
127 { t1 != t2 } -> std::convertible_to<bool>;
128 { t2 == t1 } -> std::convertible_to<bool>;
129 { t2 != t1 } -> std::convertible_to<bool>;
130 };
131
139 template <class T1, class T2>
140 concept nothrow_weakly_equality_comparable_with = requires(const std::remove_cvref_t<T1>& t1, const std::remove_cvref_t<T2>& t2)
141 {
142 { t1 == t2 } noexcept -> std::convertible_to<bool>;
143 { t1 != t2 } noexcept -> std::convertible_to<bool>;
144 { t2 == t1 } noexcept -> std::convertible_to<bool>;
145 { t2 != t1 } noexcept -> std::convertible_to<bool>;
146 };
147
153 template <class T>
154 concept comparison_category = !std::is_void_v<std::common_comparison_category_t<T>>;
155
163 template <class TFrom, class TTo>
164 concept explicitly_convertible_to = requires
165 {
166 static_cast<TTo>(std::declval<TFrom>());
167 };
168
176 template <class TFrom, class TTo>
178 {
179 { static_cast<TTo>(std::declval<TFrom>()) } noexcept;
180 };
181
185}
186
187namespace sl::concepts::detail
188{
189 template <class Category, class MinimumCategory>
190 concept compares_as = comparison_category<Category>
191 && comparison_category<MinimumCategory>
192 && std::same_as<std::common_comparison_category_t<Category, MinimumCategory>, MinimumCategory>;
193}
194
195namespace sl::concepts
196{
209 template <class T, class MinimumCategory = std::partial_ordering>
212 && requires(const std::remove_reference_t<T>& t)
213 {
214 { t <=> t } -> detail::compares_as<MinimumCategory>;
215 };
216
225 template <class T, class MinimumCategory = std::partial_ordering>
228 && requires(const std::remove_reference_t<T>& t)
229 {
230 { t <=> t } noexcept -> detail::compares_as<MinimumCategory>;
231 };
232
241 template <class T1, class T2, class MinimumCategory = std::partial_ordering>
245 && requires(const std::remove_reference_t<T1>& t1, const std::remove_reference_t<T2>& t2)
246 {
247 { t1 <=> t2 } -> detail::compares_as<MinimumCategory>;
248 { t2 <=> t1 } -> detail::compares_as<MinimumCategory>;
249 };
250
259 template <class T1, class T2, class MinimumCategory = std::partial_ordering>
263 && requires(const std::remove_reference_t<T1>& t1, const std::remove_reference_t<T2>& t2)
264 {
265 { t1 <=> t2 } noexcept -> detail::compares_as<MinimumCategory>;
266 { t2 <=> t1 } noexcept -> detail::compares_as<MinimumCategory>;
267 };
268
272}
273
274#endif
Checks whether the target type is assignable from the source type.
Definition: stl_extensions.hpp:87
Checks whether the given type denotes a common stl comparison category.
Definition: stl_extensions.hpp:154
Checks whether the T denotes a decayed type.
Definition: stl_extensions.hpp:52
Checks whether a type is explicit convertible to another.
Definition: stl_extensions.hpp:164
Checks whether the target type is constructible from the source type.
Definition: stl_extensions.hpp:78
Checks whether the left-hand-side type is unequal to the right-hand-side type.
Definition: stl_extensions.hpp:69
Checks whether a type is explicit convertible to another and does not throw.
Definition: stl_extensions.hpp:177
Checks whether a symmetrical set of operators == and != to compare both types with each other exists ...
Definition: stl_extensions.hpp:140
Checks whether the given type is comparable via operator == and != and has noexcept specifier.
Definition: stl_extensions.hpp:109
Checks whether a symmetrical set of operator <=> to compare both types with each other with noexcept ...
Definition: stl_extensions.hpp:260
Checks whether the given type is comparable <=> with the minimal comparison category and noexcept spe...
Definition: stl_extensions.hpp:226
Checks whether the T denotes a (possibly cv-qualified) pointer type.
Definition: stl_extensions.hpp:35
Checks whether the T denotes a reference type.
Definition: stl_extensions.hpp:44
Checks whether the T denotes an unqualified type.
Definition: stl_extensions.hpp:60
Checks whether a symmetrical set of operators == and != to compare both types with each other exists.
Definition: stl_extensions.hpp:124
Checks whether the given type is comparable via operator == and !=.
Definition: stl_extensions.hpp:96
Checks whether a symmetrical set of operator <=> to compare both types with each other exists.
Definition: stl_extensions.hpp:242
Checks whether the given type is comparable <=> with the minimal comparison category....
Definition: stl_extensions.hpp:210
Definition: operators.hpp:15