ugmisc 0.2-76
Miscellaneous C++ header library
Loading...
Searching...
No Matches
sfinae_helpers.hpp
Go to the documentation of this file.
1/*
2 * SPDX-Licence-Identifier: MIT
3 *
4 * Copyright 2025 Larry Chips
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the “Software”), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#pragma once
25
36
37#include <limits>
38#include <type_traits>
39#include <utility>
40
41
42
43
44namespace ugmisc {
45
46
47
48
49template<class T>
50using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
51
52
53
54
55template<class T>
56using plain_ref_t = std::remove_cv_t<T>&;
57
58
59
60
61template<class T> plain_ref_t<T> decl_plain_ref();
62
63
64
65
66template<class T, class=void>
67struct get_is_bitwise : std::false_type {};
68
69
70
71
72template<class T>
73struct get_is_bitwise<
74 T,
75 std::void_t<
76 decltype(decl_plain_ref<T>() = std::declval<T>() | std::declval<T>()),
77 decltype(decl_plain_ref<T>() = std::declval<T>() & std::declval<T>()),
78 decltype(decl_plain_ref<T>() = std::declval<T>() ^ std::declval<T>()),
79 decltype(decl_plain_ref<T>() = ~ std::declval<T>())
80 >
81 >
82 : public std::true_type {};
83
84
85
86
95template<class T>
96static constexpr bool is_bitwise = get_is_bitwise<T>::value;
97
98
99
100
104template<class T, class R=T> using uint_only = std::enable_if_t<
105 std::numeric_limits<T>::is_integer
106 && ( ! std::numeric_limits<T>::is_signed ),
107
108 R
109 >;
110
111
112
113
117template<class T, class R=T> using modulo_uint_only = std::enable_if_t<
118 std::numeric_limits<T>::is_integer
119 && ( ! std::numeric_limits<T>::is_signed )
120 && std::numeric_limits<T>::is_modulo
121 && std::is_arithmetic_v<T>,
122
123 R
124 >;
125
126
127
128
140template<class T, class R=T> using bitwise_uint_only = std::enable_if_t<
141 std::numeric_limits<T>::is_integer
142 && ( ! std::numeric_limits<T>::is_signed )
143 && ( std::numeric_limits<T>::radix == 2 )
144 && std::numeric_limits<T>::is_modulo
145 && std::is_arithmetic_v<T>
146 && is_bitwise<T>,
147
148 R
149 >;
150
151
152
153
157template<class T, class R=T> using int_only = std::enable_if_t<
158 std::numeric_limits<T>::is_integer,
159
160 R
161 >;
162
163
164
165
166} /* ugmisc */
std::enable_if_t< std::numeric_limits< T >::is_integer &&(! std::numeric_limits< T >::is_signed), R > uint_only
Resolves to R if T is an unsigned integer.
Definition sfinae_helpers.hpp:104
std::enable_if_t< std::numeric_limits< T >::is_integer &&(! std::numeric_limits< T >::is_signed) &&(std::numeric_limits< T >::radix==2) &&std::numeric_limits< T >::is_modulo &&std::is_arithmetic_v< T > &&is_bitwise< T >, R > bitwise_uint_only
Definition sfinae_helpers.hpp:140
std::enable_if_t< std::numeric_limits< T >::is_integer, R > int_only
Definition sfinae_helpers.hpp:157
std::enable_if_t< std::numeric_limits< T >::is_integer &&(! std::numeric_limits< T >::is_signed) &&std::numeric_limits< T >::is_modulo &&std::is_arithmetic_v< T >, R > modulo_uint_only
Resolves to R if T is an unsigned integer with modulo arithmetic.
Definition sfinae_helpers.hpp:117