libstdc++
utility
Go to the documentation of this file.
1// <utility> -*- C++ -*-
2
3// Copyright (C) 2001-2022 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#pragma GCC system_header
59
60/**
61 * @defgroup utilities Utilities
62 *
63 * Basic function and class templates used with the rest of the library.
64 * Includes pair, swap, forward/move helpers, declval, integer_sequence.
65 */
66
67#include <bits/c++config.h>
68#include <bits/stl_relops.h>
69#include <bits/stl_pair.h>
70
71#if __cplusplus >= 201103L
72
73#include <initializer_list>
74#include <type_traits>
75#include <bits/move.h>
76#include <bits/utility.h>
77
78#if __cplusplus >= 202002L
79#include <ext/numeric_traits.h> // __is_standard_integer, __int_traits
80#endif
81
82namespace std _GLIBCXX_VISIBILITY(default)
83{
84_GLIBCXX_BEGIN_NAMESPACE_VERSION
85
86#if __cplusplus >= 201402L
87#define __cpp_lib_exchange_function 201304L
88
89 /// Assign @p __new_val to @p __obj and return its previous value.
90 template <typename _Tp, typename _Up = _Tp>
91 _GLIBCXX20_CONSTEXPR
92 inline _Tp
93 exchange(_Tp& __obj, _Up&& __new_val)
94 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
95 is_nothrow_assignable<_Tp&, _Up>>::value)
96 { return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
97
98#if __cplusplus >= 201703L
99
100#define __cpp_lib_as_const 201510L
101 template<typename _Tp>
102 [[nodiscard]]
103 constexpr add_const_t<_Tp>&
104 as_const(_Tp& __t) noexcept
105 { return __t; }
106
107 template<typename _Tp>
108 void as_const(const _Tp&&) = delete;
109
110#if __cplusplus > 201703L
111#define __cpp_lib_integer_comparison_functions 202002L
112
113 template<typename _Tp, typename _Up>
114 constexpr bool
115 cmp_equal(_Tp __t, _Up __u) noexcept
116 {
117 static_assert(__is_standard_integer<_Tp>::value);
118 static_assert(__is_standard_integer<_Up>::value);
119
120 if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
121 return __t == __u;
122 else if constexpr (is_signed_v<_Tp>)
123 return __t >= 0 && make_unsigned_t<_Tp>(__t) == __u;
124 else
125 return __u >= 0 && __t == make_unsigned_t<_Up>(__u);
126 }
127
128 template<typename _Tp, typename _Up>
129 constexpr bool
130 cmp_not_equal(_Tp __t, _Up __u) noexcept
131 { return !std::cmp_equal(__t, __u); }
132
133 template<typename _Tp, typename _Up>
134 constexpr bool
135 cmp_less(_Tp __t, _Up __u) noexcept
136 {
137 static_assert(__is_standard_integer<_Tp>::value);
138 static_assert(__is_standard_integer<_Up>::value);
139
140 if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
141 return __t < __u;
142 else if constexpr (is_signed_v<_Tp>)
143 return __t < 0 || make_unsigned_t<_Tp>(__t) < __u;
144 else
145 return __u >= 0 && __t < make_unsigned_t<_Up>(__u);
146 }
147
148 template<typename _Tp, typename _Up>
149 constexpr bool
150 cmp_greater(_Tp __t, _Up __u) noexcept
151 { return std::cmp_less(__u, __t); }
152
153 template<typename _Tp, typename _Up>
154 constexpr bool
155 cmp_less_equal(_Tp __t, _Up __u) noexcept
156 { return !std::cmp_less(__u, __t); }
157
158 template<typename _Tp, typename _Up>
159 constexpr bool
160 cmp_greater_equal(_Tp __t, _Up __u) noexcept
161 { return !std::cmp_less(__t, __u); }
162
163 template<typename _Up, typename _Tp>
164 constexpr bool
165 in_range(_Tp __t) noexcept
166 {
167 static_assert(__is_standard_integer<_Up>::value);
168 static_assert(__is_standard_integer<_Tp>::value);
169 using __gnu_cxx::__int_traits;
170
171 if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
172 return __int_traits<_Up>::__min <= __t
173 && __t <= __int_traits<_Up>::__max;
174 else if constexpr (is_signed_v<_Tp>)
175 return __t >= 0
176 && make_unsigned_t<_Tp>(__t) <= __int_traits<_Up>::__max;
177 else
178 return __t <= make_unsigned_t<_Up>(__int_traits<_Up>::__max);
179 }
180
181#if __cplusplus > 202002L
182#define __cpp_lib_to_underlying 202102L
183 /// Convert an object of enumeration type to its underlying type.
184 template<typename _Tp>
185 [[nodiscard]]
186 constexpr underlying_type_t<_Tp>
187 to_underlying(_Tp __value) noexcept
188 { return static_cast<underlying_type_t<_Tp>>(__value); }
189
190#define __cpp_lib_unreachable 202202L
191 /// Informs the compiler that program control flow never reaches this point.
192 /**
193 * Evaluating a call to this function results in undefined behaviour.
194 * This can be used as an assertion informing the compiler that certain
195 * conditions are impossible, for when the compiler is unable to determine
196 * that by itself.
197 *
198 * For example, it can be used to prevent warnings about reaching the
199 * end of a non-void function without returning.
200 *
201 * @since C++23
202 */
203 [[noreturn,__gnu__::__always_inline__]]
204 inline void
205 unreachable()
206 {
207#ifdef _GLIBCXX_DEBUG
208 std::__glibcxx_assert_fail(nullptr, 0, "std::unreachable()", nullptr);
209#elif defined _GLIBCXX_ASSERTIONS
210 __builtin_trap();
211#else
212 __builtin_unreachable();
213#endif
214 }
215#endif // C++23
216#endif // C++20
217#endif // C++17
218#endif // C++14
219
220_GLIBCXX_END_NAMESPACE_VERSION
221} // namespace
222
223#endif
224
225#endif /* _GLIBCXX_UTILITY */