libstdc++: utility Source File

libstdc++

utility
Go to the documentation of this file.
1// <utility> -*- C++ -*-
2
3// Copyright (C) 2001-2026 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 <bits/intcmp.h>
82#endif
83
84#if __cplusplus > 202302L
85# include <bits/monostate.h>
86#endif
87
88#define __glibcxx_want_addressof_constexpr
89#define __glibcxx_want_as_const
90#define __glibcxx_want_constant_wrapper
91#define __glibcxx_want_constexpr_algorithms
92#define __glibcxx_want_constexpr_utility
93#define __glibcxx_want_exchange_function
94#define __glibcxx_want_forward_like
95#define __glibcxx_want_integer_comparison_functions
96#define __glibcxx_want_integer_sequence
97#define __glibcxx_want_ranges_zip
98#define __glibcxx_want_to_underlying
99#define __glibcxx_want_tuple_element_t
100#define __glibcxx_want_tuples_by_type
101#define __glibcxx_want_unreachable
102#define __glibcxx_want_observable_checkpoint
103#define __glibcxx_want_tuple_like
104#define __glibcxx_want_constrained_equality
105#include <bits/version.h>
106
107namespace std _GLIBCXX_VISIBILITY(default)
108{
109_GLIBCXX_BEGIN_NAMESPACE_VERSION
110
111#ifdef __cpp_lib_exchange_function // C++ >= 14
112 /// Assign @p __new_val to @p __obj and return its previous value.
113 template <typename _Tp, typename _Up = _Tp>
114 _GLIBCXX20_CONSTEXPR
115 inline _Tp
116 exchange(_Tp& __obj, _Up&& __new_val)
117 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
118 is_nothrow_assignable<_Tp&, _Up>>::value)
119 { return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
120#endif
121
122#ifdef __cpp_lib_as_const // C++ >= 17
123 template<typename _Tp>
124 [[nodiscard]]
125 constexpr add_const_t<_Tp>&
126 as_const(_Tp& __t) noexcept
127 { return __t; }
128
129 template<typename _Tp>
130 void as_const(const _Tp&&) = delete;
131#endif
132
133#ifdef __cpp_lib_to_underlying // C++ >= 23
134 /// Convert an object of enumeration type to its underlying type.
135 template<typename _Tp>
136 [[nodiscard, __gnu__::__always_inline__]]
137 constexpr underlying_type_t<_Tp>
138 to_underlying(_Tp __value) noexcept
139 { return static_cast<underlying_type_t<_Tp>>(__value); }
140#endif
141
142#ifdef __cpp_lib_unreachable // C++ >= 23
143 /// Informs the compiler that program control flow never reaches this point.
144 /**
145 * Evaluating a call to this function results in undefined behaviour.
146 * This can be used as an assertion informing the compiler that certain
147 * conditions are impossible, for when the compiler is unable to determine
148 * that by itself.
149 *
150 * For example, it can be used to prevent warnings about reaching the
151 * end of a non-void function without returning.
152 *
153 * @since C++23
154 */
155 [[noreturn,__gnu__::__always_inline__]]
156 inline void
157 unreachable()
158 {
159#ifdef _GLIBCXX_DEBUG
160 std::__glibcxx_assert_fail(nullptr, 0, "std::unreachable()", nullptr);
161#elif defined _GLIBCXX_ASSERTIONS
162 __builtin_trap();
163#else
164 __builtin_unreachable();
165#endif
166 }
167#endif
168
169
170#ifdef __cpp_lib_observable_checkpoint // C++ >= 26
171 /// Informs the compiler that prior actions are considered observable.
172 /**
173 * This may be used to limit the extent to which optimisations based on the
174 * assumed unreachability of undefined behaviour can propagate to earlier
175 * code.
176 *
177 * @since C++26
178 */
179 [[__gnu__::__always_inline__]]
180 inline void
181 observable_checkpoint() noexcept
182 {
183 return __builtin_observable_checkpoint();
184 }
185#endif
186
187_GLIBCXX_END_NAMESPACE_VERSION
188} // namespace
189
190#endif
191
192#endif /* _GLIBCXX_UTILITY */
initializer_list
type_traits
intcmp.h
monostate.h
move.h
stl_pair.h
stl_relops.h
version.h
utility.h
c++config.h
std::underlying_type_t
typename underlying_type< _Tp >::type underlying_type_t
Alias template for underlying_type.
Definition type_traits:2979
std::forward
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
std
ISO C++ entities toplevel namespace is std.
std::is_nothrow_assignable
is_nothrow_assignable
Definition type_traits:1418