1 // <experimental/propagate_const> -*- C++ -*-
3 // Copyright (C) 2015-2022 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/propagate_const
26 * This is a TS C++ Library header.
30 #ifndef _GLIBCXX_EXPERIMENTAL_PROPAGATE_CONST
31 #define _GLIBCXX_EXPERIMENTAL_PROPAGATE_CONST 1
33 #pragma GCC system_header
35 #if __cplusplus >= 201402L
37 #include <type_traits>
38 #include <bits/functional_hash.h>
39 #include <bits/move.h>
40 #include <bits/stl_function.h>
41 #include <experimental/bits/lfts_config.h>
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 namespace experimental
49 inline namespace fundamentals_v2
52 * @defgroup propagate_const Const-propagating wrapper
55 * A const-propagating wrapper that propagates const to pointer-like members,
56 * as described in n4388 "A Proposal to Add a Const-Propagating Wrapper
57 * to the Standard Library".
62 /// Const-propagating wrapper.
63 template <typename _Tp>
67 typedef remove_reference_t<decltype(*std::declval<_Tp&>())> element_type;
70 template <typename _Up>
71 struct __is_propagate_const : false_type
74 template <typename _Up>
75 struct __is_propagate_const<propagate_const<_Up>> : true_type
78 template <typename _Up>
79 friend constexpr const _Up&
80 get_underlying(const propagate_const<_Up>& __pt) noexcept;
81 template <typename _Up>
83 get_underlying(propagate_const<_Up>& __pt) noexcept;
85 template <typename _Up>
86 static constexpr element_type*
87 __to_raw_pointer(_Up* __u)
90 template <typename _Up>
91 static constexpr element_type*
92 __to_raw_pointer(_Up& __u)
95 template <typename _Up>
96 static constexpr const element_type*
97 __to_raw_pointer(const _Up* __u)
100 template <typename _Up>
101 static constexpr const element_type*
102 __to_raw_pointer(const _Up& __u)
103 { return __u.get(); }
106 static_assert(__and_<is_object<typename remove_pointer<_Tp>::type>,
107 __not_<is_array<_Tp>>,
108 __or_<is_class<_Tp>, is_pointer<_Tp>>>::value,
109 "propagate_const requires a class or a pointer to an"
112 // [propagate_const.ctor], constructors
113 constexpr propagate_const() = default;
114 propagate_const(const propagate_const& __p) = delete;
115 constexpr propagate_const(propagate_const&& __p) = default;
117 template <typename _Up, typename
118 enable_if<__and_<is_constructible<_Tp, _Up&&>,
119 is_convertible<_Up&&, _Tp>>::value, bool
121 constexpr propagate_const(propagate_const<_Up>&& __pu)
122 : _M_t(std::move(get_underlying(__pu)))
125 template <typename _Up, typename
126 enable_if<__and_<is_constructible<_Tp, _Up&&>,
127 __not_<is_convertible<_Up&&, _Tp>>>::value,
129 constexpr explicit propagate_const(propagate_const<_Up>&& __pu)
130 : _M_t(std::move(get_underlying(__pu)))
133 template <typename _Up, typename
134 enable_if<__and_<is_constructible<_Tp, _Up&&>,
135 is_convertible<_Up&&, _Tp>,
136 __not_<__is_propagate_const<
137 typename decay<_Up>::type>>
138 >::value, bool>::type=true>
139 constexpr propagate_const(_Up&& __u)
140 : _M_t(std::forward<_Up>(__u))
143 template <typename _Up, typename
144 enable_if<__and_<is_constructible<_Tp, _Up&&>,
145 __not_<is_convertible<_Up&&, _Tp>>,
146 __not_<__is_propagate_const<
147 typename decay<_Up>::type>>
148 >::value, bool>::type=false>
149 constexpr explicit propagate_const(_Up&& __u)
150 : _M_t(std::forward<_Up>(__u))
153 // [propagate_const.assignment], assignment
154 propagate_const& operator=(const propagate_const& __p) = delete;
155 constexpr propagate_const& operator=(propagate_const&& __p) = default;
157 template <typename _Up, typename =
158 typename enable_if<is_convertible<_Up&&, _Tp>::value>::type>
159 constexpr propagate_const& operator=(propagate_const<_Up>&& __pu)
161 _M_t = std::move(get_underlying(__pu));
165 template <typename _Up, typename =
166 typename enable_if<__and_<is_convertible<_Up&&, _Tp>,
167 __not_<__is_propagate_const<
168 typename decay<_Up>::type>>
170 constexpr propagate_const& operator=(_Up&& __u)
172 _M_t = std::forward<_Up>(__u);
176 // [propagate_const.const_observers], const observers
177 explicit constexpr operator bool() const
182 constexpr const element_type* operator->() const
187 template <typename _Up = _Tp,
188 typename enable_if<__or_<is_pointer<_Up>,
191 >::value, bool>::type = true>
192 constexpr operator const element_type*() const
197 constexpr const element_type& operator*() const
202 constexpr const element_type* get() const
204 return __to_raw_pointer(_M_t);
207 // [propagate_const.non_const_observers], non-const observers
208 constexpr element_type* operator->()
213 template <typename _Up = _Tp,
214 typename enable_if<__or_<is_pointer<_Up>,
217 >::value, bool>::type = true>
218 constexpr operator element_type*()
223 constexpr element_type& operator*()
228 constexpr element_type* get()
230 return __to_raw_pointer(_M_t);
233 // [propagate_const.modifiers], modifiers
235 swap(propagate_const& __pt) noexcept(__is_nothrow_swappable<_Tp>::value)
238 swap(_M_t, get_underlying(__pt));
245 // [propagate_const.relational], relational operators
246 template <typename _Tp>
248 operator==(const propagate_const<_Tp>& __pt, nullptr_t)
250 return get_underlying(__pt) == nullptr;
253 template <typename _Tp>
255 operator==(nullptr_t, const propagate_const<_Tp>& __pu)
257 return nullptr == get_underlying(__pu);
260 template <typename _Tp>
262 operator!=(const propagate_const<_Tp>& __pt, nullptr_t)
264 return get_underlying(__pt) != nullptr;
267 template <typename _Tp>
268 constexpr bool operator!=(nullptr_t, const propagate_const<_Tp>& __pu)
270 return nullptr != get_underlying(__pu);
273 template <typename _Tp, typename _Up>
275 operator==(const propagate_const<_Tp>& __pt,
276 const propagate_const<_Up>& __pu)
278 return get_underlying(__pt) == get_underlying(__pu);
281 template <typename _Tp, typename _Up>
283 operator!=(const propagate_const<_Tp>& __pt,
284 const propagate_const<_Up>& __pu)
286 return get_underlying(__pt) != get_underlying(__pu);
289 template <typename _Tp, typename _Up>
291 operator<(const propagate_const<_Tp>& __pt,
292 const propagate_const<_Up>& __pu)
294 return get_underlying(__pt) < get_underlying(__pu);
297 template <typename _Tp, typename _Up>
299 operator>(const propagate_const<_Tp>& __pt,
300 const propagate_const<_Up>& __pu)
302 return get_underlying(__pt) > get_underlying(__pu);
305 template <typename _Tp, typename _Up>
307 operator<=(const propagate_const<_Tp>& __pt,
308 const propagate_const<_Up>& __pu)
310 return get_underlying(__pt) <= get_underlying(__pu);
313 template <typename _Tp, typename _Up>
315 operator>=(const propagate_const<_Tp>& __pt,
316 const propagate_const<_Up>& __pu)
318 return get_underlying(__pt) >= get_underlying(__pu);
321 template <typename _Tp, typename _Up>
323 operator==(const propagate_const<_Tp>& __pt, const _Up& __u)
325 return get_underlying(__pt) == __u;
328 template <typename _Tp, typename _Up>
330 operator!=(const propagate_const<_Tp>& __pt, const _Up& __u)
332 return get_underlying(__pt) != __u;
335 template <typename _Tp, typename _Up>
337 operator<(const propagate_const<_Tp>& __pt, const _Up& __u)
339 return get_underlying(__pt) < __u;
342 template <typename _Tp, typename _Up>
344 operator>(const propagate_const<_Tp>& __pt, const _Up& __u)
346 return get_underlying(__pt) > __u;
349 template <typename _Tp, typename _Up>
351 operator<=(const propagate_const<_Tp>& __pt, const _Up& __u)
353 return get_underlying(__pt) <= __u;
356 template <typename _Tp, typename _Up>
358 operator>=(const propagate_const<_Tp>& __pt, const _Up& __u)
360 return get_underlying(__pt) >= __u;
363 template <typename _Tp, typename _Up>
365 operator==(const _Tp& __t, const propagate_const<_Up>& __pu)
367 return __t == get_underlying(__pu);
370 template <typename _Tp, typename _Up>
372 operator!=(const _Tp& __t, const propagate_const<_Up>& __pu)
374 return __t != get_underlying(__pu);
377 template <typename _Tp, typename _Up>
379 operator<(const _Tp& __t, const propagate_const<_Up>& __pu)
381 return __t < get_underlying(__pu);
384 template <typename _Tp, typename _Up>
386 operator>(const _Tp& __t, const propagate_const<_Up>& __pu)
388 return __t > get_underlying(__pu);
391 template <typename _Tp, typename _Up>
393 operator<=(const _Tp& __t, const propagate_const<_Up>& __pu)
395 return __t <= get_underlying(__pu);
398 template <typename _Tp, typename _Up>
400 operator>=(const _Tp& __t, const propagate_const<_Up>& __pu)
402 return __t >= get_underlying(__pu);
405 // [propagate_const.algorithms], specialized algorithms
406 // _GLIBCXX_RESOLVE_LIB_DEFECTS
407 // 3413. propagate_const's swap [...] needs to be constrained and use a trait
408 template <typename _Tp>
409 constexpr enable_if_t<__is_swappable<_Tp>::value, void>
410 swap(propagate_const<_Tp>& __pt, propagate_const<_Tp>& __pt2)
411 noexcept(__is_nothrow_swappable<_Tp>::value)
416 // [propagate_const.underlying], underlying pointer access
417 template <typename _Tp>
419 get_underlying(const propagate_const<_Tp>& __pt) noexcept
424 template <typename _Tp>
426 get_underlying(propagate_const<_Tp>& __pt) noexcept
431 /// @} group propagate_const
432 } // namespace fundamentals_v2
433 } // namespace experimental
435 // [propagate_const.hash], hash support
436 template <typename _Tp>
437 struct hash<experimental::propagate_const<_Tp>>
439 using result_type = size_t;
440 using argument_type = experimental::propagate_const<_Tp>;
443 operator()(const experimental::propagate_const<_Tp>& __t) const
444 noexcept(noexcept(hash<_Tp>{}(get_underlying(__t))))
446 return hash<_Tp>{}(get_underlying(__t));
450 // [propagate_const.comparison_function_objects], comparison function objects
451 template <typename _Tp>
452 struct equal_to<experimental::propagate_const<_Tp>>
455 operator()(const experimental::propagate_const<_Tp>& __x,
456 const experimental::propagate_const<_Tp>& __y) const
458 return equal_to<_Tp>{}(get_underlying(__x), get_underlying(__y));
461 typedef experimental::propagate_const<_Tp> first_argument_type;
462 typedef experimental::propagate_const<_Tp> second_argument_type;
463 typedef bool result_type;
466 template <typename _Tp>
467 struct not_equal_to<experimental::propagate_const<_Tp>>
470 operator()(const experimental::propagate_const<_Tp>& __x,
471 const experimental::propagate_const<_Tp>& __y) const
473 return not_equal_to<_Tp>{}(get_underlying(__x), get_underlying(__y));
476 typedef experimental::propagate_const<_Tp> first_argument_type;
477 typedef experimental::propagate_const<_Tp> second_argument_type;
478 typedef bool result_type;
481 template <typename _Tp>
482 struct less<experimental::propagate_const<_Tp>>
485 operator()(const experimental::propagate_const<_Tp>& __x,
486 const experimental::propagate_const<_Tp>& __y) const
488 return less<_Tp>{}(get_underlying(__x), get_underlying(__y));
491 typedef experimental::propagate_const<_Tp> first_argument_type;
492 typedef experimental::propagate_const<_Tp> second_argument_type;
493 typedef bool result_type;
496 template <typename _Tp>
497 struct greater<experimental::propagate_const<_Tp>>
500 operator()(const experimental::propagate_const<_Tp>& __x,
501 const experimental::propagate_const<_Tp>& __y) const
503 return greater<_Tp>{}(get_underlying(__x), get_underlying(__y));
506 typedef experimental::propagate_const<_Tp> first_argument_type;
507 typedef experimental::propagate_const<_Tp> second_argument_type;
508 typedef bool result_type;
511 template <typename _Tp>
512 struct less_equal<experimental::propagate_const<_Tp>>
515 operator()(const experimental::propagate_const<_Tp>& __x,
516 const experimental::propagate_const<_Tp>& __y) const
518 return less_equal<_Tp>{}(get_underlying(__x), get_underlying(__y));
521 typedef experimental::propagate_const<_Tp> first_argument_type;
522 typedef experimental::propagate_const<_Tp> second_argument_type;
523 typedef bool result_type;
526 template <typename _Tp>
527 struct greater_equal<experimental::propagate_const<_Tp>>
530 operator()(const experimental::propagate_const<_Tp>& __x,
531 const experimental::propagate_const<_Tp>& __y) const
533 return greater_equal<_Tp>{}(get_underlying(__x), get_underlying(__y));
536 typedef experimental::propagate_const<_Tp> first_argument_type;
537 typedef experimental::propagate_const<_Tp> second_argument_type;
538 typedef bool result_type;
541 _GLIBCXX_END_NAMESPACE_VERSION
546 #endif // _GLIBCXX_EXPERIMENTAL_PROPAGATE_CONST