1 // <experimental/memory> -*- C++ -*-
3 // Copyright (C) 2015-2025 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file experimental/memory
26 * This is a TS C++ Library header.
31 // N4336 Working Draft, C++ Extensions for Library Fundamentals, Version 2
34 #ifndef _GLIBCXX_EXPERIMENTAL_MEMORY
35 #define _GLIBCXX_EXPERIMENTAL_MEMORY 1
37 #ifdef _GLIBCXX_SYSHDR
38 #pragma GCC system_header
41 #include <bits/requires_hosted.h> // experimental is currently omitted
43 #if __cplusplus >= 201402L
46 #include <type_traits>
47 #include <experimental/bits/shared_ptr.h>
48 #include <bits/functional_hash.h>
50 namespace std _GLIBCXX_VISIBILITY(default)
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
54 namespace experimental
56 inline namespace fundamentals_v2
58 #define __cpp_lib_experimental_observer_ptr 201411
60 template <typename _Tp>
64 // publish our template parameter and variations thereof
65 using element_type = _Tp;
66 using __pointer = add_pointer_t<_Tp>; // exposition-only
67 using __reference = add_lvalue_reference_t<_Tp>; // exposition-only
69 // 3.2.2, observer_ptr constructors
71 constexpr observer_ptr() noexcept
75 // pointer-accepting c'tors
76 constexpr observer_ptr(nullptr_t) noexcept
80 constexpr explicit observer_ptr(__pointer __p) noexcept
84 // copying c'tors (in addition to compiler-generated copy c'tor)
85 template <typename _Up,
86 typename = typename enable_if<
87 is_convertible<typename add_pointer<_Up>::type, __pointer
90 constexpr observer_ptr(observer_ptr<_Up> __p) noexcept
95 // 3.2.3, observer_ptr observers
102 constexpr __reference
109 operator->() const noexcept
114 constexpr explicit operator bool() const noexcept
116 return get() != nullptr;
119 // 3.2.4, observer_ptr conversions
120 constexpr explicit operator __pointer() const noexcept
125 // 3.2.5, observer_ptr modifiers
129 __pointer __tmp = get();
135 reset(__pointer __p = nullptr) noexcept
141 swap(observer_ptr& __p) noexcept
143 std::swap(__t, __p.__t);
150 template<typename _Tp>
152 swap(observer_ptr<_Tp>& __p1, observer_ptr<_Tp>& __p2) noexcept
157 template<typename _Tp>
159 make_observer(_Tp* __p) noexcept
161 return observer_ptr<_Tp>(__p);
164 template<typename _Tp, typename _Up>
166 operator==(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2)
168 return __p1.get() == __p2.get();
171 template<typename _Tp, typename _Up>
173 operator!=(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2)
175 return !(__p1 == __p2);
178 template<typename _Tp>
180 operator==(observer_ptr<_Tp> __p, nullptr_t) noexcept
185 template<typename _Tp>
187 operator==(nullptr_t, observer_ptr<_Tp> __p) noexcept
192 template<typename _Tp>
194 operator!=(observer_ptr<_Tp> __p, nullptr_t) noexcept
199 template<typename _Tp>
201 operator!=(nullptr_t, observer_ptr<_Tp> __p) noexcept
206 template<typename _Tp, typename _Up>
208 operator<(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2)
210 return std::less<typename common_type<typename add_pointer<_Tp>::type,
211 typename add_pointer<_Up>::type
213 >{}(__p1.get(), __p2.get());
216 template<typename _Tp, typename _Up>
218 operator>(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2)
223 template<typename _Tp, typename _Up>
225 operator<=(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2)
227 return !(__p2 < __p1);
230 template<typename _Tp, typename _Up>
232 operator>=(observer_ptr<_Tp> __p1, observer_ptr<_Up> __p2)
234 return !(__p1 < __p2);
236 } // namespace fundamentals_v2
237 } // namespace experimental
239 template <typename _Tp>
240 struct hash<experimental::observer_ptr<_Tp>>
242 using result_type = size_t;
243 using argument_type = experimental::observer_ptr<_Tp>;
246 operator()(const experimental::observer_ptr<_Tp>& __t) const
247 noexcept(noexcept(hash<typename add_pointer<_Tp>::type> {}(__t.get())))
249 return hash<typename add_pointer<_Tp>::type> {}(__t.get());
254 _GLIBCXX_END_NAMESPACE_VERSION
257 #endif // __cplusplus <= 201103L
259 #endif // _GLIBCXX_EXPERIMENTAL_MEMORY