1 // <experimental/propagate_const> -*- C++ -*-
3 // Copyright (C) 2015-2020 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;
116 template <typename _Up, typename
117 enable_if<__and_<is_constructible<_Tp, _Up&&>,
118 is_convertible<_Up&&, _Tp>>::value, bool
120 constexpr propagate_const(propagate_const<_Up>&& __pu)
121 : _M_t(std::move(get_underlying(__pu)))
123 template <typename _Up, typename
124 enable_if<__and_<is_constructible<_Tp, _Up&&>,
125 __not_<is_convertible<_Up&&, _Tp>>>::value,
127 constexpr explicit propagate_const(propagate_const<_Up>&& __pu)
128 : _M_t(std::move(get_underlying(__pu)))
130 template <typename _Up, typename
131 enable_if<__and_<is_constructible<_Tp, _Up&&>,
132 is_convertible<_Up&&, _Tp>,
133 __not_<__is_propagate_const<
134 typename decay<_Up>::type>>
135 >::value, bool>::type=true>
136 constexpr propagate_const(_Up&& __u)
137 : _M_t(std::forward<_Up>(__u))
139 template <typename _Up, typename
140 enable_if<__and_<is_constructible<_Tp, _Up&&>,
141 __not_<is_convertible<_Up&&, _Tp>>,
142 __not_<__is_propagate_const<
143 typename decay<_Up>::type>>
144 >::value, bool>::type=false>
145 constexpr explicit propagate_const(_Up&& __u)
146 : _M_t(std::forward<_Up>(__u))
149 // [propagate_const.assignment], assignment
150 propagate_const& operator=(const propagate_const& __p) = delete;
151 constexpr propagate_const& operator=(propagate_const&& __p) = default;
153 template <typename _Up, typename =
154 typename enable_if<is_convertible<_Up&&, _Tp>::value>::type>
155 constexpr propagate_const& operator=(propagate_const<_Up>&& __pu)
157 _M_t = std::move(get_underlying(__pu));
161 template <typename _Up, typename =
162 typename enable_if<__and_<is_convertible<_Up&&, _Tp>,
163 __not_<__is_propagate_const<
164 typename decay<_Up>::type>>
166 constexpr propagate_const& operator=(_Up&& __u)
168 _M_t = std::forward<_Up>(__u);
172 // [propagate_const.const_observers], const observers
173 explicit constexpr operator bool() const
178 constexpr const element_type* operator->() const
183 template <typename _Up = _Tp,
184 typename enable_if<__or_<is_pointer<_Up>,
187 >::value, bool>::type = true>
188 constexpr operator const element_type*() const
193 constexpr const element_type& operator*() const
198 constexpr const element_type* get() const
200 return __to_raw_pointer(_M_t);
203 // [propagate_const.non_const_observers], non-const observers
204 constexpr element_type* operator->()
209 template <typename _Up = _Tp,
210 typename enable_if<__or_<is_pointer<_Up>,
213 >::value, bool>::type = true>
214 constexpr operator element_type*()
219 constexpr element_type& operator*()
224 constexpr element_type* get()
226 return __to_raw_pointer(_M_t);
229 // [propagate_const.modifiers], modifiers
231 swap(propagate_const& __pt) noexcept(__is_nothrow_swappable<_Tp>::value)
234 swap(_M_t, get_underlying(__pt));
241 // [propagate_const.relational], relational operators
242 template <typename _Tp>
244 operator==(const propagate_const<_Tp>& __pt, nullptr_t)
246 return get_underlying(__pt) == nullptr;
249 template <typename _Tp>
251 operator==(nullptr_t, const propagate_const<_Tp>& __pu)
253 return nullptr == get_underlying(__pu);
256 template <typename _Tp>
258 operator!=(const propagate_const<_Tp>& __pt, nullptr_t)
260 return get_underlying(__pt) != nullptr;
263 template <typename _Tp>
264 constexpr bool operator!=(nullptr_t, const propagate_const<_Tp>& __pu)
266 return nullptr != get_underlying(__pu);
269 template <typename _Tp, typename _Up>
271 operator==(const propagate_const<_Tp>& __pt,
272 const propagate_const<_Up>& __pu)
274 return get_underlying(__pt) == get_underlying(__pu);
277 template <typename _Tp, typename _Up>
279 operator!=(const propagate_const<_Tp>& __pt,
280 const propagate_const<_Up>& __pu)
282 return get_underlying(__pt) != get_underlying(__pu);
285 template <typename _Tp, typename _Up>
287 operator<(const propagate_const<_Tp>& __pt,
288 const propagate_const<_Up>& __pu)
290 return get_underlying(__pt) < get_underlying(__pu);
293 template <typename _Tp, typename _Up>
295 operator>(const propagate_const<_Tp>& __pt,
296 const propagate_const<_Up>& __pu)
298 return get_underlying(__pt) > get_underlying(__pu);
301 template <typename _Tp, typename _Up>
303 operator<=(const propagate_const<_Tp>& __pt,
304 const propagate_const<_Up>& __pu)
306 return get_underlying(__pt) <= get_underlying(__pu);
309 template <typename _Tp, typename _Up>
311 operator>=(const propagate_const<_Tp>& __pt,
312 const propagate_const<_Up>& __pu)
314 return get_underlying(__pt) >= get_underlying(__pu);
317 template <typename _Tp, typename _Up>
319 operator==(const propagate_const<_Tp>& __pt, const _Up& __u)
321 return get_underlying(__pt) == __u;
324 template <typename _Tp, typename _Up>
326 operator!=(const propagate_const<_Tp>& __pt, const _Up& __u)
328 return get_underlying(__pt) != __u;
331 template <typename _Tp, typename _Up>
333 operator<(const propagate_const<_Tp>& __pt, const _Up& __u)
335 return get_underlying(__pt) < __u;
338 template <typename _Tp, typename _Up>
340 operator>(const propagate_const<_Tp>& __pt, const _Up& __u)
342 return get_underlying(__pt) > __u;
345 template <typename _Tp, typename _Up>
347 operator<=(const propagate_const<_Tp>& __pt, const _Up& __u)
349 return get_underlying(__pt) <= __u;
352 template <typename _Tp, typename _Up>
354 operator>=(const propagate_const<_Tp>& __pt, const _Up& __u)
356 return get_underlying(__pt) >= __u;
359 template <typename _Tp, typename _Up>
361 operator==(const _Tp& __t, const propagate_const<_Up>& __pu)
363 return __t == get_underlying(__pu);
366 template <typename _Tp, typename _Up>
368 operator!=(const _Tp& __t, const propagate_const<_Up>& __pu)
370 return __t != get_underlying(__pu);
373 template <typename _Tp, typename _Up>
375 operator<(const _Tp& __t, const propagate_const<_Up>& __pu)
377 return __t < get_underlying(__pu);
380 template <typename _Tp, typename _Up>
382 operator>(const _Tp& __t, const propagate_const<_Up>& __pu)
384 return __t > get_underlying(__pu);
387 template <typename _Tp, typename _Up>
389 operator<=(const _Tp& __t, const propagate_const<_Up>& __pu)
391 return __t <= get_underlying(__pu);
394 template <typename _Tp, typename _Up>
396 operator>=(const _Tp& __t, const propagate_const<_Up>& __pu)
398 return __t >= get_underlying(__pu);
401 // [propagate_const.algorithms], specialized algorithms
402 template <typename _Tp>
404 swap(propagate_const<_Tp>& __pt, propagate_const<_Tp>& __pt2)
405 noexcept(__is_nothrow_swappable<_Tp>::value)
410 // [propagate_const.underlying], underlying pointer access
411 template <typename _Tp>
413 get_underlying(const propagate_const<_Tp>& __pt) noexcept
418 template <typename _Tp>
420 get_underlying(propagate_const<_Tp>& __pt) noexcept
425 /// @} group propagate_const
426 } // namespace fundamentals_v2
427 } // namespace experimental
429 // [propagate_const.hash], hash support
430 template <typename _Tp>
431 struct hash<experimental::propagate_const<_Tp>>
433 using result_type = size_t;
434 using argument_type = experimental::propagate_const<_Tp>;
437 operator()(const experimental::propagate_const<_Tp>& __t) const
438 noexcept(noexcept(hash<_Tp>{}(get_underlying(__t))))
440 return hash<_Tp>{}(get_underlying(__t));
444 // [propagate_const.comparison_function_objects], comparison function objects
445 template <typename _Tp>
446 struct equal_to<experimental::propagate_const<_Tp>>
449 operator()(const experimental::propagate_const<_Tp>& __x,
450 const experimental::propagate_const<_Tp>& __y) const
452 return equal_to<_Tp>{}(get_underlying(__x), get_underlying(__y));
455 typedef experimental::propagate_const<_Tp> first_argument_type;
456 typedef experimental::propagate_const<_Tp> second_argument_type;
457 typedef bool result_type;
460 template <typename _Tp>
461 struct not_equal_to<experimental::propagate_const<_Tp>>
464 operator()(const experimental::propagate_const<_Tp>& __x,
465 const experimental::propagate_const<_Tp>& __y) const
467 return not_equal_to<_Tp>{}(get_underlying(__x), get_underlying(__y));
470 typedef experimental::propagate_const<_Tp> first_argument_type;
471 typedef experimental::propagate_const<_Tp> second_argument_type;
472 typedef bool result_type;
475 template <typename _Tp>
476 struct less<experimental::propagate_const<_Tp>>
479 operator()(const experimental::propagate_const<_Tp>& __x,
480 const experimental::propagate_const<_Tp>& __y) const
482 return less<_Tp>{}(get_underlying(__x), get_underlying(__y));
485 typedef experimental::propagate_const<_Tp> first_argument_type;
486 typedef experimental::propagate_const<_Tp> second_argument_type;
487 typedef bool result_type;
490 template <typename _Tp>
491 struct greater<experimental::propagate_const<_Tp>>
494 operator()(const experimental::propagate_const<_Tp>& __x,
495 const experimental::propagate_const<_Tp>& __y) const
497 return greater<_Tp>{}(get_underlying(__x), get_underlying(__y));
500 typedef experimental::propagate_const<_Tp> first_argument_type;
501 typedef experimental::propagate_const<_Tp> second_argument_type;
502 typedef bool result_type;
505 template <typename _Tp>
506 struct less_equal<experimental::propagate_const<_Tp>>
509 operator()(const experimental::propagate_const<_Tp>& __x,
510 const experimental::propagate_const<_Tp>& __y) const
512 return less_equal<_Tp>{}(get_underlying(__x), get_underlying(__y));
515 typedef experimental::propagate_const<_Tp> first_argument_type;
516 typedef experimental::propagate_const<_Tp> second_argument_type;
517 typedef bool result_type;
520 template <typename _Tp>
521 struct greater_equal<experimental::propagate_const<_Tp>>
524 operator()(const experimental::propagate_const<_Tp>& __x,
525 const experimental::propagate_const<_Tp>& __y) const
527 return greater_equal<_Tp>{}(get_underlying(__x), get_underlying(__y));
530 typedef experimental::propagate_const<_Tp> first_argument_type;
531 typedef experimental::propagate_const<_Tp> second_argument_type;
532 typedef bool result_type;
535 _GLIBCXX_END_NAMESPACE_VERSION
540 #endif // _GLIBCXX_EXPERIMENTAL_PROPAGATE_CONST