// Concepts and traits for use with iterators -*- C++ -*- // Copyright (C) 2019 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the // Free Software Foundation; either version 3, or (at your option) // any later version. // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // Under Section 7 of GPL version 3, you are granted additional // permissions described in the GCC Runtime Library Exception, version // 3.1, as published by the Free Software Foundation. // You should have received a copy of the GNU General Public License and // a copy of the GCC Runtime Library Exception along with this program; // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see // . /** @file bits/iterator_concepts.h * This is an internal header file, included by other library headers. * Do not attempt to use it directly. @headername{iterator} */ #ifndef _ITERATOR_CONCEPTS_H #define _ITERATOR_CONCEPTS_H 1 #pragma GCC system_header #include #include // to_address #include // identity, ranges::less #if __cpp_lib_concepts namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION struct input_iterator_tag; struct output_iterator_tag; struct forward_iterator_tag; struct bidirectional_iterator_tag; struct random_access_iterator_tag; struct contiguous_iterator_tag; template struct iterator_traits; template requires is_object_v<_Tp> struct iterator_traits<_Tp*>; template struct __iterator_traits; namespace __detail { template using __with_ref = _Tp&; template concept __can_reference = requires { typename __with_ref<_Tp>; }; template concept __dereferenceable = requires(_Tp& __t) { { *__t } -> __can_reference; }; // FIXME: needed due to PR c++/67704 template<__detail::__dereferenceable _Tp> struct __iter_ref { using type = decltype(*std::declval<_Tp&>()); }; } // namespace __detail template using iter_reference_t = typename __detail::__iter_ref<_Tp>::type; namespace ranges { namespace __cust_imove { template concept __adl_imove = (std::__detail::__class_or_enum>) && requires(_Tp&& __t) { iter_move(static_cast<_Tp&&>(__t)); }; struct _IMove { private: template static constexpr bool _S_noexcept() { if constexpr (__adl_imove<_Tp>) return noexcept(iter_move(std::declval<_Tp>())); else return noexcept(*std::declval<_Tp>()); } public: template requires __adl_imove<_Tp> || requires(_Tp& __e) { *__e; } constexpr decltype(auto) operator()(_Tp&& __e) const noexcept(_S_noexcept<_Tp>()) { if constexpr (__adl_imove<_Tp>) return iter_move(static_cast<_Tp&&>(__e)); else if constexpr (is_reference_v>) return std::move(*__e); else return *__e; } }; } // namespace __cust_imove inline namespace __cust { inline constexpr __cust_imove::_IMove iter_move{}; } // inline namespace __cust } // namespace ranges namespace __detail { // FIXME: needed due to PR c++/67704 template<__detail::__dereferenceable _Tp> struct __iter_rvalue_ref { }; template<__detail::__dereferenceable _Tp> requires requires(_Tp& __t) { { ranges::iter_move(__t) } -> __detail::__can_reference; } struct __iter_rvalue_ref<_Tp> { using type = decltype(ranges::iter_move(std::declval<_Tp&>())); }; } // namespace __detail template using iter_rvalue_reference_t = typename __detail::__iter_rvalue_ref<_Tp>::type; template struct incrementable_traits { }; template requires is_object_v<_Tp> struct incrementable_traits<_Tp*> { using difference_type = ptrdiff_t; }; template struct incrementable_traits : incrementable_traits<_Iter> { }; template requires requires { typename _Tp::difference_type; } struct incrementable_traits<_Tp> { using difference_type = typename _Tp::difference_type; }; template requires (!requires { typename _Tp::difference_type; } && requires(const _Tp& __a, const _Tp& __b) { requires (!is_void_v>); // PR c++/78173 { __a - __b } -> integral; }) struct incrementable_traits<_Tp> { using difference_type = make_signed_t() - std::declval<_Tp>())>; }; namespace __detail { // An iterator such that iterator_traits<_Iter> names a specialization // generated from the primary template. template concept __primary_traits_iter = __is_base_of(__iterator_traits<_Iter, void>, iterator_traits<_Iter>); template struct __iter_traits_impl { using type = iterator_traits<_Iter>; }; template requires __primary_traits_iter<_Iter> struct __iter_traits_impl<_Iter, _Tp> { using type = _Tp; }; // ITER_TRAITS template using __iter_traits = typename __iter_traits_impl<_Iter, _Tp>::type; } // namespace __detail template using iter_difference_t = typename __detail::__iter_traits<_Tp, incrementable_traits<_Tp>>::difference_type; namespace __detail { template struct __cond_value_type { }; template requires is_object_v<_Tp> struct __cond_value_type<_Tp> { using value_type = remove_cv_t<_Tp>; }; } // namespace __detail template struct readable_traits { }; template struct readable_traits<_Tp*> : __detail::__cond_value_type<_Tp> { }; template requires is_array_v<_Iter> struct readable_traits<_Iter> { using value_type = remove_cv_t>; }; template struct readable_traits : readable_traits<_Iter> { }; template requires requires { typename _Tp::value_type; } struct readable_traits<_Tp> : __detail::__cond_value_type { }; template requires requires { typename _Tp::element_type; } struct readable_traits<_Tp> : __detail::__cond_value_type { }; template using iter_value_t = typename __detail::__iter_traits<_Tp, readable_traits<_Tp>>::value_type; namespace __detail { template concept __cpp17_iterator = copyable<_Iter> && requires(_Iter __it) { { *__it } -> __can_reference; { ++__it } -> same_as<_Iter&>; { *__it++ } -> __can_reference; }; template concept __cpp17_input_iterator = __cpp17_iterator<_Iter> && equality_comparable<_Iter> && requires(_Iter __it) { typename incrementable_traits<_Iter>::difference_type; typename readable_traits<_Iter>::value_type; typename common_reference_t&&, typename readable_traits<_Iter>::value_type&>; typename common_reference_t::value_type&>; requires signed_integral::difference_type>; }; template concept __cpp17_fwd_iterator = __cpp17_input_iterator<_Iter> && constructible_from<_Iter> && is_lvalue_reference_v> && same_as>, typename readable_traits<_Iter>::value_type> && requires(_Iter __it) { { __it++ } -> convertible_to; { *__it++ } -> same_as>; }; template concept __cpp17_bidi_iterator = __cpp17_fwd_iterator<_Iter> && requires(_Iter __it) { { --__it } -> same_as<_Iter&>; { __it-- } -> convertible_to; { *__it-- } -> same_as>; }; template concept __cpp17_randacc_iterator = __cpp17_bidi_iterator<_Iter> && totally_ordered<_Iter> && requires(_Iter __it, typename incrementable_traits<_Iter>::difference_type __n) { { __it += __n } -> same_as<_Iter&>; { __it -= __n } -> same_as<_Iter&>; { __it + __n } -> same_as<_Iter>; { __n + __it } -> same_as<_Iter>; { __it - __n } -> same_as<_Iter>; { __it - __it } -> same_as; { __it[__n] } -> convertible_to>; }; template concept __iter_with_nested_types = requires { typename _Iter::iterator_category; typename _Iter::value_type; typename _Iter::difference_type; typename _Iter::reference; }; template concept __iter_without_nested_types = !__iter_with_nested_types<_Iter>; template struct __ptr { using type = void; }; template requires requires { typename _Iter::pointer; } struct __ptr<_Iter, true> { using type = typename _Iter::pointer; }; template requires requires { typename _Iter::pointer; } struct __ptr<_Iter, false> { using type = typename _Iter::pointer; }; template requires (!requires { typename _Iter::pointer; } && requires(_Iter& __it) { __it.operator->(); }) struct __ptr<_Iter, true> { using type = decltype(std::declval<_Iter&>().operator->()); }; template struct __ref { using type = iter_reference_t<_Iter>; }; template requires requires { typename _Iter::reference; } struct __ref<_Iter> { using type = typename _Iter::reference; }; template struct __cat { using type = input_iterator_tag; }; template requires requires { typename _Iter::iterator_category; } struct __cat<_Iter> { using type = typename _Iter::iterator_category; }; template requires (!requires { typename _Iter::iterator_category; } && __detail::__cpp17_randacc_iterator<_Iter>) struct __cat<_Iter> { using type = random_access_iterator_tag; }; template requires (!requires { typename _Iter::iterator_category; } && __detail::__cpp17_bidi_iterator<_Iter>) struct __cat<_Iter> { using type = bidirectional_iterator_tag; }; template requires (!requires { typename _Iter::iterator_category; } && __detail::__cpp17_fwd_iterator<_Iter>) struct __cat<_Iter> { using type = forward_iterator_tag; }; template struct __diff { using type = void; }; template requires requires { typename incrementable_traits<_Iter>::difference_type; } struct __diff<_Iter> { using type = typename incrementable_traits<_Iter>::difference_type; }; } // namespace __detail template requires __detail::__iter_with_nested_types<_Iterator> struct __iterator_traits<_Iterator, void> { using iterator_category = typename _Iterator::iterator_category; using value_type = typename _Iterator::value_type; using difference_type = typename _Iterator::difference_type; using pointer = typename __detail::__ptr<_Iterator>::type; using reference = typename _Iterator::reference; }; template requires __detail::__iter_without_nested_types<_Iterator> && __detail::__cpp17_input_iterator<_Iterator> struct __iterator_traits<_Iterator, void> { using iterator_category = typename __detail::__cat<_Iterator>::type; using value_type = typename readable_traits<_Iterator>::value_type; using difference_type = typename incrementable_traits<_Iterator>::difference_type; using pointer = typename __detail::__ptr<_Iterator, true>::type; using reference = typename __detail::__ref<_Iterator>::type; }; template requires __detail::__iter_without_nested_types<_Iterator> && __detail::__cpp17_iterator<_Iterator> struct __iterator_traits<_Iterator, void> { using iterator_category = output_iterator_tag; using value_type = void; using difference_type = typename __detail::__diff<_Iterator>::type; using pointer = void; using reference = void; }; namespace __detail { template struct __iter_concept_impl { }; template requires requires { typename __iter_traits<_Iter>::iterator_concept; } struct __iter_concept_impl<_Iter> { using type = typename __iter_traits<_Iter>::iterator_concept; }; template requires (!requires { typename __iter_traits<_Iter>::iterator_concept; } && requires { typename __iter_traits<_Iter>::iterator_category; }) struct __iter_concept_impl<_Iter> { using type = typename __iter_traits<_Iter>::iterator_category; }; template requires (!requires { typename __iter_traits<_Iter>::iterator_concept; } && !requires { typename __iter_traits<_Iter>::iterator_category; } && __primary_traits_iter<_Iter>) struct __iter_concept_impl<_Iter> { using type = random_access_iterator_tag; }; // ITER_TRAITS template using __iter_concept = typename __iter_concept_impl<_Iter>::type; } // namespace __detail /// Requirements for types that are readable by applying operator*. template concept readable = requires { typename iter_value_t<_In>; typename iter_reference_t<_In>; typename iter_rvalue_reference_t<_In>; } && common_reference_with&&, iter_value_t<_In>&> && common_reference_with&&, iter_rvalue_reference_t<_In>&&> && common_reference_with&&, const iter_value_t<_In>&>; namespace __detail { // FIXME: needed due to PR c++/67704 template struct __iter_common_ref : common_reference, iter_value_t<_Tp>&> { }; // FIXME: needed due to PR c++/67704 template struct __indirect_result { }; template requires (readable<_Is> && ...) && invocable<_Fn, iter_reference_t<_Is>...> struct __indirect_result<_Fn, _Is...> : invoke_result<_Fn, iter_reference_t<_Is>...> { }; } // namespace __detail template using iter_common_reference_t = typename __detail::__iter_common_ref<_Tp>::type; /// Requirements for writing a value into an iterator's referenced object. template concept writable = requires(_Out&& __o, _Tp&& __t) { *__o = std::forward<_Tp>(__t); *std::forward<_Out>(__o) = std::forward<_Tp>(__t); const_cast&&>(*__o) = std::forward<_Tp>(__t); const_cast&&>(*std::forward<_Out>(__o)) = std::forward<_Tp>(__t); }; /// Requirements on types that can be incremented with ++. template concept weakly_incrementable = default_constructible<_Iter> && movable<_Iter> && requires(_Iter __i) { typename iter_difference_t<_Iter>; requires signed_integral>; { ++__i } -> same_as<_Iter&>; __i++; }; template concept incrementable = regular<_Iter> && weakly_incrementable<_Iter> && requires(_Iter __i) { { __i++ } -> same_as<_Iter>; }; template concept input_or_output_iterator = requires(_Iter __i) { { *__i } -> __detail::__can_reference; } && weakly_incrementable<_Iter>; template concept sentinel_for = semiregular<_Sent> && input_or_output_iterator<_Iter> && __detail::__weakly_eq_cmp_with<_Sent, _Iter>; template inline constexpr bool disable_sized_sentinel = false; template concept sized_sentinel_for = sentinel_for<_Sent, _Iter> && !disable_sized_sentinel, remove_cv_t<_Iter>> && requires(const _Iter& __i, const _Sent& __s) { { __s - __i } -> same_as>; { __i - __s } -> same_as>; }; template concept input_iterator = input_or_output_iterator<_Iter> && readable<_Iter> && requires { typename __detail::__iter_concept<_Iter>; } && derived_from<__detail::__iter_concept<_Iter>, input_iterator_tag>; template concept output_iterator = input_or_output_iterator<_Iter> && writable<_Iter, _Tp> && requires(_Iter __i, _Tp&& __t) { *__i++ = std::forward<_Tp>(__t); }; template concept forward_iterator = input_iterator<_Iter> && derived_from<__detail::__iter_concept<_Iter>, forward_iterator_tag> && incrementable<_Iter> && sentinel_for<_Iter, _Iter>; template concept bidirectional_iterator = forward_iterator<_Iter> && derived_from<__detail::__iter_concept<_Iter>, bidirectional_iterator_tag> && requires(_Iter __i) { { --__i } -> same_as<_Iter&>; { __i-- } -> same_as<_Iter>; }; template concept random_access_iterator = bidirectional_iterator<_Iter> && derived_from<__detail::__iter_concept<_Iter>, random_access_iterator_tag> && totally_ordered<_Iter> && sized_sentinel_for<_Iter, _Iter> && requires(_Iter __i, const _Iter __j, const iter_difference_t<_Iter> __n) { { __i += __n } -> same_as<_Iter&>; { __j + __n } -> same_as<_Iter>; { __n + __j } -> same_as<_Iter>; { __i -= __n } -> same_as<_Iter&>; { __j - __n } -> same_as<_Iter>; { __j[__n] } -> same_as>; }; template concept contiguous_iterator = random_access_iterator<_Iter> && derived_from<__detail::__iter_concept<_Iter>, contiguous_iterator_tag> && is_lvalue_reference_v> && same_as, remove_cvref_t>> && requires(const _Iter& __i) { { std::to_address(__i) } -> same_as>>; }; // [indirectcallable], indirect callable requirements // [indirectcallable.indirectinvocable], indirect callables template concept indirectly_unary_invocable = readable<_Iter> && copy_constructible<_Fn> && invocable<_Fn&, iter_value_t<_Iter>&> && invocable<_Fn&, iter_reference_t<_Iter>> && invocable<_Fn&, iter_common_reference_t<_Iter>> && common_reference_with&>, invoke_result_t<_Fn&, iter_reference_t<_Iter>>>; template concept indirectly_regular_unary_invocable = readable<_Iter> && copy_constructible<_Fn> && regular_invocable<_Fn&, iter_value_t<_Iter>&> && regular_invocable<_Fn&, iter_reference_t<_Iter>> && regular_invocable<_Fn&, iter_common_reference_t<_Iter>> && common_reference_with&>, invoke_result_t<_Fn&, iter_reference_t<_Iter>>>; template concept indirect_unary_predicate = readable<_Iter> && copy_constructible<_Fn> && predicate<_Fn&, iter_value_t<_Iter>&> && predicate<_Fn&, iter_reference_t<_Iter>> && predicate<_Fn&, iter_common_reference_t<_Iter>>; template concept indirect_relation = readable<_I1> && readable<_I2> && copy_constructible<_Fn> && relation<_Fn&, iter_value_t<_I1>&, iter_value_t<_I2>&> && relation<_Fn&, iter_value_t<_I1>&, iter_reference_t<_I2>> && relation<_Fn&, iter_reference_t<_I1>, iter_value_t<_I2>&> && relation<_Fn&, iter_reference_t<_I1>, iter_reference_t<_I2>> && relation<_Fn&, iter_common_reference_t<_I1>, iter_common_reference_t<_I2>>; template concept indirect_strict_weak_order = readable<_I1> && readable<_I2> && copy_constructible<_Fn> && strict_weak_order<_Fn&, iter_value_t<_I1>&, iter_value_t<_I2>&> && strict_weak_order<_Fn&, iter_value_t<_I1>&, iter_reference_t<_I2>> && strict_weak_order<_Fn&, iter_reference_t<_I1>, iter_value_t<_I2>&> && strict_weak_order<_Fn&, iter_reference_t<_I1>, iter_reference_t<_I2>> && strict_weak_order<_Fn&, iter_common_reference_t<_I1>, iter_common_reference_t<_I2>>; template using indirect_result_t = typename __detail::__indirect_result<_Fn, iter_reference_t<_Is>...>::type; /// [projected], projected template _Proj> struct projected { using value_type = remove_cvref_t>; indirect_result_t<_Proj&, _Iter> operator*() const; // not defined }; template struct incrementable_traits> { using difference_type = iter_difference_t<_Iter>; }; // [alg.req], common algorithm requirements /// [alg.req.ind.move], concept `indirectly_movable` template concept indirectly_movable = readable<_In> && writable<_Out, iter_rvalue_reference_t<_In>>; template concept indirectly_movable_storable = indirectly_movable<_In, _Out> && writable<_Out, iter_value_t<_In>> && movable> && constructible_from, iter_rvalue_reference_t<_In>> && assignable_from&, iter_rvalue_reference_t<_In>>; /// [alg.req.ind.copy], concept `indirectly_copyable` template concept indirectly_copyable = readable<_In> && writable<_Out, iter_reference_t<_In>>; template concept indirectly_copyable_storable = indirectly_copyable<_In, _Out> && writable<_Out, const iter_value_t<_In>&> && copyable> && constructible_from, iter_reference_t<_In>> && assignable_from&, iter_reference_t<_In>>; namespace ranges { namespace __cust_iswap { template void iter_swap(_It1&, _It2&) = delete; template concept __adl_iswap = (std::__detail::__class_or_enum> || std::__detail::__class_or_enum>) && requires(_Tp&& __t, _Up&& __u) { iter_swap(static_cast<_Tp&&>(__t), static_cast<_Up&&>(__u)); }; template constexpr iter_value_t> __iter_exchange_move(_Xp&& __x, _Yp&& __y) noexcept(noexcept(iter_value_t>(iter_move(__x))) && noexcept(*__x = iter_move(__y))) { iter_value_t> __old_value(iter_move(__x)); *__x = iter_move(__y); return __old_value; } struct _IterSwap { private: template static constexpr bool _S_noexcept() { if constexpr (__adl_iswap<_Tp, _Up>) return noexcept(iter_swap(std::declval<_Tp>(), std::declval<_Up>())); else if constexpr (readable<_Tp> && readable<_Up> && swappable_with, iter_reference_t<_Up>>) return noexcept(ranges::swap(*std::declval<_Tp>(), *std::declval<_Up>())); else return noexcept(*std::declval<_Tp>() = __iter_exchange_move(std::declval<_Up>(), std::declval<_Tp>())); } public: template requires __adl_iswap<_Tp, _Up> || (readable<_Tp> && readable<_Up> && swappable_with, iter_reference_t<_Up>>) || (indirectly_movable_storable<_Tp, _Up> && indirectly_movable_storable<_Up, _Tp>) constexpr void operator()(_Tp&& __e1, _Up&& __e2) const noexcept(_S_noexcept<_Tp, _Up>()) { if constexpr (__adl_iswap<_Tp, _Up>) iter_swap(static_cast<_Tp&&>(__e1), static_cast<_Up&&>(__e2)); else if constexpr (readable<_Tp> && readable<_Up> && swappable_with, iter_reference_t<_Up>>) ranges::swap(*__e1, *__e2); else *__e1 = __iter_exchange_move(__e2, __e1); } }; } // namespace __cust_iswap inline namespace __cust { inline constexpr __cust_iswap::_IterSwap iter_swap{}; } // inline namespace __cust } // namespace ranges /// [alg.req.ind.swap], concept `indirectly_swappable` template concept indirectly_swappable = readable<_I1> && readable<_I2> && requires(_I1& __i1, _I2& __i2) { ranges::iter_swap(__i1, __i1); ranges::iter_swap(__i2, __i2); ranges::iter_swap(__i1, __i2); ranges::iter_swap(__i2, __i1); }; /// [alg.req.ind.cmp], concept `indirectly_comparable` template concept indirectly_comparable = indirect_relation<_Rel, projected<_I1, _P1>, projected<_I2, _P2>>; /// [alg.req.permutable], concept `permutable` template concept permutable = forward_iterator<_Iter> && indirectly_movable_storable<_Iter, _Iter> && indirectly_swappable<_Iter, _Iter>; /// [alg.req.mergeable], concept `mergeable` template concept mergeable = input_iterator<_I1> && input_iterator<_I2> && weakly_incrementable<_Out> && indirectly_copyable<_I1, _Out> && indirectly_copyable<_I2, _Out> && indirect_strict_weak_order<_Rel, projected<_I1, _P1>, projected<_I2, _P2>>; /// [alg.req.sortable], concept `sortable` template concept sortable = permutable<_Iter> && indirect_strict_weak_order<_Rel, projected<_Iter, _Proj>>; struct unreachable_sentinel_t { template friend constexpr bool operator==(unreachable_sentinel_t, const _It&) noexcept { return false; } }; inline constexpr unreachable_sentinel_t unreachable_sentinel{}; struct default_sentinel_t { }; inline constexpr default_sentinel_t default_sentinel{}; _GLIBCXX_END_NAMESPACE_VERSION } // namespace std #endif // C++20 library concepts #endif // _ITERATOR_CONCEPTS_H