libstdc++
utility
Go to the documentation of this file.
1// <utility> -*- C++ -*-
2
3// Copyright (C) 2001-2024 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
51/** @file include/utility
52 * This is a Standard C++ Library header.
53 */
54
55#ifndef _GLIBCXX_UTILITY
56#define _GLIBCXX_UTILITY 1
57
58#ifdef _GLIBCXX_SYSHDR
59#pragma GCC system_header
60#endif
61
62/**
63 * @defgroup utilities Utilities
64 *
65 * Basic function and class templates used with the rest of the library.
66 * Includes pair, swap, forward/move helpers, declval, integer_sequence.
67 */
68
69#include <bits/c++config.h>
70#include <bits/stl_relops.h>
71#include <bits/stl_pair.h>
72
73#if __cplusplus >= 201103L
74
75#include <initializer_list>
76#include <type_traits>
77#include <bits/move.h>
78#include <bits/utility.h>
79
80#if __cplusplus >= 202002L
81#include <ext/numeric_traits.h> // __is_standard_integer, __int_traits
82#endif
83
84#define __glibcxx_want_addressof_constexpr
85#define __glibcxx_want_as_const
86#define __glibcxx_want_constexpr_algorithms
87#define __glibcxx_want_constexpr_utility
88#define __glibcxx_want_exchange_function
89#define __glibcxx_want_forward_like
90#define __glibcxx_want_integer_comparison_functions
91#define __glibcxx_want_integer_sequence
92#define __glibcxx_want_ranges_zip
93#define __glibcxx_want_to_underlying
94#define __glibcxx_want_tuple_element_t
95#define __glibcxx_want_tuples_by_type
96#define __glibcxx_want_unreachable
97#define __glibcxx_want_tuple_like
98#define __glibcxx_want_constrained_equality
99#include <bits/version.h>
100
101namespace std _GLIBCXX_VISIBILITY(default)
102{
103_GLIBCXX_BEGIN_NAMESPACE_VERSION
104
105#ifdef __cpp_lib_exchange_function // C++ >= 14
106 /// Assign @p __new_val to @p __obj and return its previous value.
107 template <typename _Tp, typename _Up = _Tp>
108 _GLIBCXX20_CONSTEXPR
109 inline _Tp
110 exchange(_Tp& __obj, _Up&& __new_val)
111 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
112 is_nothrow_assignable<_Tp&, _Up>>::value)
113 { return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
114#endif
115
116#ifdef __cpp_lib_as_const // C++ >= 17
117 template<typename _Tp>
118 [[nodiscard]]
119 constexpr add_const_t<_Tp>&
120 as_const(_Tp& __t) noexcept
121 { return __t; }
122
123 template<typename _Tp>
124 void as_const(const _Tp&&) = delete;
125#endif
126
127#ifdef __cpp_lib_integer_comparison_functions // C++ >= 20
128 template<typename _Tp, typename _Up>
129 constexpr bool
130 cmp_equal(_Tp __t, _Up __u) noexcept
131 {
132 static_assert(__is_standard_integer<_Tp>::value);
133 static_assert(__is_standard_integer<_Up>::value);
134
135 if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
136 return __t == __u;
137 else if constexpr (is_signed_v<_Tp>)
138 return __t >= 0 && make_unsigned_t<_Tp>(__t) == __u;
139 else
140 return __u >= 0 && __t == make_unsigned_t<_Up>(__u);
141 }
142
143 template<typename _Tp, typename _Up>
144 constexpr bool
145 cmp_not_equal(_Tp __t, _Up __u) noexcept
146 { return !std::cmp_equal(__t, __u); }
147
148 template<typename _Tp, typename _Up>
149 constexpr bool
150 cmp_less(_Tp __t, _Up __u) noexcept
151 {
152 static_assert(__is_standard_integer<_Tp>::value);
153 static_assert(__is_standard_integer<_Up>::value);
154
155 if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
156 return __t < __u;
157 else if constexpr (is_signed_v<_Tp>)
158 return __t < 0 || make_unsigned_t<_Tp>(__t) < __u;
159 else
160 return __u >= 0 && __t < make_unsigned_t<_Up>(__u);
161 }
162
163 template<typename _Tp, typename _Up>
164 constexpr bool
165 cmp_greater(_Tp __t, _Up __u) noexcept
166 { return std::cmp_less(__u, __t); }
167
168 template<typename _Tp, typename _Up>
169 constexpr bool
170 cmp_less_equal(_Tp __t, _Up __u) noexcept
171 { return !std::cmp_less(__u, __t); }
172
173 template<typename _Tp, typename _Up>
174 constexpr bool
175 cmp_greater_equal(_Tp __t, _Up __u) noexcept
176 { return !std::cmp_less(__t, __u); }
177
178 template<typename _Res, typename _Tp>
179 constexpr bool
180 in_range(_Tp __t) noexcept
181 {
182 static_assert(__is_standard_integer<_Res>::value);
183 static_assert(__is_standard_integer<_Tp>::value);
185
186 if constexpr (is_signed_v<_Tp> == is_signed_v<_Res>)
187 return __int_traits<_Res>::__min <= __t
188 && __t <= __int_traits<_Res>::__max;
189 else if constexpr (is_signed_v<_Tp>)
190 return __t >= 0
191 && make_unsigned_t<_Tp>(__t) <= __int_traits<_Res>::__max;
192 else
193 return __t <= make_unsigned_t<_Res>(__int_traits<_Res>::__max);
194 }
195#endif // __cpp_lib_integer_comparison_functions
196
197#ifdef __cpp_lib_to_underlying // C++ >= 23
198 /// Convert an object of enumeration type to its underlying type.
199 template<typename _Tp>
200 [[nodiscard]]
201 constexpr underlying_type_t<_Tp>
202 to_underlying(_Tp __value) noexcept
203 { return static_cast<underlying_type_t<_Tp>>(__value); }
204#endif
205
206#ifdef __cpp_lib_unreachable // C++ >= 23
207 /// Informs the compiler that program control flow never reaches this point.
208 /**
209 * Evaluating a call to this function results in undefined behaviour.
210 * This can be used as an assertion informing the compiler that certain
211 * conditions are impossible, for when the compiler is unable to determine
212 * that by itself.
213 *
214 * For example, it can be used to prevent warnings about reaching the
215 * end of a non-void function without returning.
216 *
217 * @since C++23
218 */
219 [[noreturn,__gnu__::__always_inline__]]
220 inline void
221 unreachable()
222 {
223#ifdef _GLIBCXX_DEBUG
224 std::__glibcxx_assert_fail(nullptr, 0, "std::unreachable()", nullptr);
225#elif defined _GLIBCXX_ASSERTIONS
226 __builtin_trap();
227#else
228 __builtin_unreachable();
229#endif
230 }
231#endif
232
233_GLIBCXX_END_NAMESPACE_VERSION
234} // namespace
235
236#endif
237
238#endif /* _GLIBCXX_UTILITY */
ISO C++ entities toplevel namespace is std.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.