// Copyright (C) 2019 T. Zachary Laine // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_STL_INTERFACES_CONTAINER_INTERFACE_HPP #define BOOST_STL_INTERFACES_CONTAINER_INTERFACE_HPP #include #include #include #include #include #include namespace boost { namespace stl_interfaces { namespace detail { template struct n_iter : iterator_interface< n_iter, std::random_access_iterator_tag, T> { n_iter() : x_(nullptr), n_(0) {} n_iter(T const & x, SizeType n) : x_(&x), n_(n) {} T const & operator*() const { return *x_; } constexpr std::ptrdiff_t operator-(n_iter other) const noexcept { return std::ptrdiff_t(n_) - std::ptrdiff_t(other.n_); } n_iter & operator+=(std::ptrdiff_t offset) { n_ += offset; return *this; } private: T const * x_; SizeType n_; }; template constexpr auto make_n_iter(T const & x, SizeType n) noexcept( noexcept(n_iter(x, n))) { using result_type = n_iter; return result_type(x, SizeType(0)); } template constexpr auto make_n_iter_end(T const & x, SizeType n) noexcept( noexcept(n_iter(x, n))) { return n_iter(x, n); } template std::size_t fake_capacity(Container const & c) { return SIZE_MAX; } template< typename Container, typename Enable = decltype( std::size_t() = std::declval().capacity())> std::size_t fake_capacity(Container const & c) { return c.capacity(); } }}} namespace boost { namespace stl_interfaces { BOOST_STL_INTERFACES_NAMESPACE_V1 { /** A CRTP template that one may derive from to make it easier to define container types. The template parameter `D` for `sequence_container_interface` may be an incomplete type. Before any member of the resulting specialization of `sequence_container_interface` other than special member functions is referenced, `D` shall be complete; shall model `std::derived_from>`, `std::semiregular`, and `std::forward_range`; and shall contain all the nested types required in Table 72: Container requirements and, for those whose iterator nested type models `std::bidirectinal_iterator`, those in Table 73: Reversible container requirements. For an object `d` of type `D`, a call to `std::ranges::begin(d)` sxhall not mutate any data members of `d`, and `d`'s destructor shall end the lifetimes of the objects in `[std::ranges::begin(d), std::ranges::end(d))`. */ template< typename Derived, element_layout Contiguity = element_layout::discontiguous #ifndef BOOST_STL_INTERFACES_DOXYGEN , typename E = std::enable_if_t< std::is_class::value && std::is_same>::value> #endif > struct sequence_container_interface; namespace v1_dtl { template using in_iter = std::is_convertible< typename std::iterator_traits::iterator_category, std::input_iterator_tag>; template struct clear_impl { static constexpr void call(D & d) noexcept {} }; template struct clear_impl().clear())>> { static constexpr void call(D & d) noexcept { d.clear(); } }; template void derived_container(sequence_container_interface const &); } template< typename Derived, element_layout Contiguity #ifndef BOOST_STL_INTERFACES_DOXYGEN , typename E #endif > struct sequence_container_interface { #ifndef BOOST_STL_INTERFACES_DOXYGEN private: constexpr Derived & derived() noexcept { return static_cast(*this); } constexpr const Derived & derived() const noexcept { return static_cast(*this); } constexpr Derived & mutable_derived() const noexcept { return const_cast(static_cast(*this)); } #endif public: template constexpr auto empty() noexcept( noexcept(std::declval().begin() == std::declval().end())) -> decltype( std::declval().begin() == std::declval().end()) { return derived().begin() == derived().end(); } template constexpr auto empty() const noexcept(noexcept( std::declval().begin() == std::declval().end())) -> decltype( std::declval().begin() == std::declval().end()) { return derived().begin() == derived().end(); } template< typename D = Derived, element_layout C = Contiguity, typename Enable = std::enable_if_t> constexpr auto data() noexcept(noexcept(std::declval().begin())) -> decltype(std::addressof(*std::declval().begin())) { return std::addressof(*derived().begin()); } template< typename D = Derived, element_layout C = Contiguity, typename Enable = std::enable_if_t> constexpr auto data() const noexcept(noexcept(std::declval().begin())) -> decltype(std::addressof(*std::declval().begin())) { return std::addressof(*derived().begin()); } template constexpr auto size() #if !BOOST_CLANG noexcept(noexcept( std::declval().end() - std::declval().begin())) #endif -> decltype(typename D::size_type( std::declval().end() - std::declval().begin())) { return derived().end() - derived().begin(); } template constexpr auto size() const noexcept(noexcept( std::declval().end() - std::declval().begin())) -> decltype(typename D::size_type( #if !BOOST_CLANG std::declval().end() - std::declval().begin() #endif )) { return derived().end() - derived().begin(); } template constexpr auto front() noexcept(noexcept(*std::declval().begin())) -> decltype(*std::declval().begin()) { return *derived().begin(); } template constexpr auto front() const noexcept(noexcept(*std::declval().begin())) -> decltype(*std::declval().begin()) { return *derived().begin(); } template constexpr auto push_front(typename D::value_type const & x) noexcept( noexcept(std::declval().emplace_front(x))) -> decltype((void)std::declval().emplace_front(x)) { derived().emplace_front(x); } template constexpr auto push_front(typename D::value_type && x) noexcept( noexcept(std::declval().emplace_front(std::move(x)))) -> decltype((void)std::declval().emplace_front(std::move(x))) { derived().emplace_front(std::move(x)); } template constexpr auto pop_front() noexcept -> decltype( std::declval().emplace_front( std::declval()), (void)std::declval().erase(std::declval().begin())) { derived().erase(derived().begin()); } template< typename D = Derived, typename Enable = std::enable_if_t< v1_dtl::decrementable_sentinel::value && v1_dtl::common_range::value>> constexpr auto back() noexcept(noexcept(*std::prev(std::declval().end()))) -> decltype(*std::prev(std::declval().end())) { return *std::prev(derived().end()); } template< typename D = Derived, typename Enable = std::enable_if_t< v1_dtl::decrementable_sentinel::value && v1_dtl::common_range::value>> constexpr auto back() const noexcept(noexcept(*std::prev(std::declval().end()))) -> decltype(*std::prev(std::declval().end())) { return *std::prev(derived().end()); } template constexpr auto push_back(typename D::value_type const & x) noexcept( noexcept(std::declval().emplace_back(x))) -> decltype((void)std::declval().emplace_back(x)) { derived().emplace_back(x); } template constexpr auto push_back(typename D::value_type && x) noexcept( noexcept(std::declval().emplace_back(std::move(x)))) -> decltype((void)std::declval().emplace_back(std::move(x))) { derived().emplace_back(std::move(x)); } template constexpr auto pop_back() noexcept -> decltype( std::declval().emplace_back( std::declval()), (void)std::declval().erase( std::prev(std::declval().end()))) { derived().erase(std::prev(derived().end())); } template constexpr auto operator[](typename D::size_type n) noexcept( noexcept(std::declval().begin()[n])) -> decltype(std::declval().begin()[n]) { return derived().begin()[n]; } template constexpr auto operator[](typename D::size_type n) const noexcept(noexcept(std::declval().begin()[n])) -> decltype(std::declval().begin()[n]) { return derived().begin()[n]; } template constexpr auto at(typename D::size_type i) -> decltype(std::declval().size(), std::declval()[i]) { if (derived().size() <= i) { throw std::out_of_range( "Bounds check failed in sequence_container_interface::at()"); } return derived()[i]; } template constexpr auto at(typename D::size_type i) const -> decltype( std::declval().size(), std::declval()[i]) { if (derived().size() <= i) { throw std::out_of_range( "Bounds check failed in sequence_container_interface::at()"); } return derived()[i]; } template constexpr Iter begin() const noexcept(noexcept(std::declval().begin())) { return Iter(mutable_derived().begin()); } template constexpr Iter end() const noexcept(noexcept(std::declval().end())) { return Iter(mutable_derived().end()); } template constexpr auto cbegin() const noexcept(noexcept(std::declval().begin())) -> decltype(std::declval().begin()) { return derived().begin(); } template constexpr auto cend() const noexcept(noexcept(std::declval().end())) -> decltype(std::declval().end()) { return derived().end(); } template< typename D = Derived, typename Enable = std::enable_if_t::value>> constexpr auto rbegin() noexcept(noexcept( stl_interfaces::make_reverse_iterator(std::declval().end()))) { return stl_interfaces::make_reverse_iterator(derived().end()); } template< typename D = Derived, typename Enable = std::enable_if_t::value>> constexpr auto rend() noexcept(noexcept( stl_interfaces::make_reverse_iterator(std::declval().begin()))) { return stl_interfaces::make_reverse_iterator(derived().begin()); } template constexpr auto rbegin() const noexcept(noexcept(std::declval().rbegin())) { return typename D::const_reverse_iterator(mutable_derived().rbegin()); } template constexpr auto rend() const noexcept(noexcept(std::declval().rend())) { return typename D::const_reverse_iterator(mutable_derived().rend()); } template constexpr auto crbegin() const noexcept(noexcept(std::declval().rbegin())) -> decltype(std::declval().rbegin()) { return derived().rbegin(); } template constexpr auto crend() const noexcept(noexcept(std::declval().rend())) -> decltype(std::declval().rend()) { return derived().rend(); } template constexpr auto insert( typename D::const_iterator pos, typename D::value_type const & x) noexcept(noexcept(std::declval().emplace(pos, x))) -> decltype(std::declval().emplace(pos, x)) { return derived().emplace(pos, x); } template constexpr auto insert( typename D::const_iterator pos, typename D::value_type && x) noexcept(noexcept(std::declval() .emplace(pos, std::move(x)))) -> decltype(std::declval().emplace(pos, std::move(x))) { return derived().emplace(pos, std::move(x)); } template constexpr auto insert( typename D::const_iterator pos, typename D::size_type n, typename D::value_type const & x) // If you see an error in this noexcept() expression, that's // because this function is not properly constrained. In other // words, Derived does not have a "range" insert like // insert(position, first, last). If that is the case, this // function should be removed via SFINAE from overload resolution. // However, both the trailing decltype code below and a // std::enable_if in the template parameters do not work. Sorry // about that. See below for details. noexcept(noexcept(std::declval().insert( pos, detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)))) // This causes the compiler to infinitely recurse into this function's // declaration, even though the call below does not match the // signature of this function. #if 0 -> decltype(std::declval().insert( pos, detail::make_n_iter(x, n), detail::make_n_iter_end(x, n))) #endif { return derived().insert( pos, detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)); } template constexpr auto insert( typename D::const_iterator pos, std::initializer_list il) noexcept(noexcept(std::declval() .insert(pos, il.begin(), il.end()))) -> decltype(std::declval().insert(pos, il.begin(), il.end())) { return derived().insert(pos, il.begin(), il.end()); } template constexpr auto erase(typename D::const_iterator pos) noexcept -> decltype(std::declval().erase(pos, std::next(pos))) { return derived().erase(pos, std::next(pos)); } template< typename InputIterator, typename D = Derived, typename Enable = std::enable_if_t::value>> constexpr auto assign(InputIterator first, InputIterator last) noexcept( noexcept(std::declval().insert( std::declval().begin(), first, last))) -> decltype( std::declval().erase( std::declval().begin(), std::declval().end()), (void)std::declval().insert( std::declval().begin(), first, last)) { auto out = derived().begin(); auto const out_last = derived().end(); for (; out != out_last && first != last; ++first, ++out) { *out = *first; } if (out != out_last) derived().erase(out, out_last); if (first != last) derived().insert(derived().end(), first, last); } template constexpr auto assign( typename D::size_type n, typename D::value_type const & x) noexcept(noexcept(std::declval() .insert( std::declval().begin(), detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)))) -> decltype( std::declval().size(), std::declval().erase( std::declval().begin(), std::declval().end()), (void)std::declval().insert( std::declval().begin(), detail::make_n_iter(x, n), detail::make_n_iter_end(x, n))) { if (detail::fake_capacity(derived()) < n) { Derived temp(n, x); derived().swap(temp); } else { auto const min_size = std::min(n, derived().size()); auto const fill_end = std::fill_n(derived().begin(), min_size, x); if (min_size < (std::ptrdiff_t)derived().size()) { derived().erase(fill_end, derived().end()); } else { n -= min_size; derived().insert( derived().begin(), detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)); } } } template constexpr auto assign(std::initializer_list il) noexcept( noexcept(std::declval().assign(il.begin(), il.end()))) -> decltype((void)std::declval().assign(il.begin(), il.end())) { derived().assign(il.begin(), il.end()); } template constexpr auto operator=(std::initializer_list il) noexcept( noexcept(std::declval().assign(il.begin(), il.end()))) -> decltype( std::declval().assign(il.begin(), il.end()), std::declval()) { derived().assign(il.begin(), il.end()); return *this; } template constexpr auto clear() noexcept -> decltype((void)std::declval().erase( std::declval().begin(), std::declval().end())) { derived().erase(derived().begin(), derived().end()); } }; /** Implementation of free function `swap()` for all containers derived from `sequence_container_interface`. */ template constexpr auto swap( ContainerInterface & lhs, ContainerInterface & rhs) noexcept(noexcept(lhs.swap(rhs))) -> decltype(v1_dtl::derived_container(lhs), lhs.swap(rhs)) { return lhs.swap(rhs); } /** Implementation of `operator==()` for all containers derived from `sequence_container_interface`. */ template constexpr auto operator==(ContainerInterface const & lhs, ContainerInterface const & rhs) noexcept( noexcept(lhs.size() == rhs.size()) && noexcept(*lhs.begin() == *rhs.begin())) -> decltype( v1_dtl::derived_container(lhs), lhs.size() == rhs.size(), *lhs.begin() == *rhs.begin(), true) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()); } /** Implementation of `operator!=()` for all containers derived from `sequence_container_interface`. */ template constexpr auto operator!=( ContainerInterface const & lhs, ContainerInterface const & rhs) noexcept(noexcept(lhs == rhs)) -> decltype(v1_dtl::derived_container(lhs), lhs == rhs) { return !(lhs == rhs); } /** Implementation of `operator<()` for all containers derived from `sequence_container_interface`. */ template constexpr auto operator<( ContainerInterface const & lhs, ContainerInterface const & rhs) noexcept(noexcept(*lhs.begin() < *rhs.begin())) -> decltype( v1_dtl::derived_container(lhs), *lhs.begin() < *rhs.begin(), true) { auto it1 = lhs.begin(); auto const last1 = lhs.end(); auto it2 = rhs.begin(); auto const last2 = rhs.end(); for (; it1 != last1 && it2 != last2; ++it1, ++it2) { if (*it1 < *it2) return true; if (*it2 < *it1) return false; } return it1 == last1 && it2 != last2; } /** Implementation of `operator<=()` for all containers derived from `sequence_container_interface`. */ template constexpr auto operator<=( ContainerInterface const & lhs, ContainerInterface const & rhs) noexcept(noexcept(lhs < rhs)) -> decltype(v1_dtl::derived_container(lhs), lhs < rhs) { return !(rhs < lhs); } /** Implementation of `operator>()` for all containers derived from `sequence_container_interface`. */ template constexpr auto operator>( ContainerInterface const & lhs, ContainerInterface const & rhs) noexcept(noexcept(lhs < rhs)) -> decltype(v1_dtl::derived_container(lhs), lhs < rhs) { return rhs < lhs; } /** Implementation of `operator>=()` for all containers derived from `sequence_container_interface`. */ template constexpr auto operator>=( ContainerInterface const & lhs, ContainerInterface const & rhs) noexcept(noexcept(lhs < rhs)) -> decltype(v1_dtl::derived_container(lhs), lhs < rhs) { return !(lhs < rhs); } }}} #if defined(BOOST_STL_INTERFACES_DOXYGEN) || BOOST_STL_INTERFACES_USE_CONCEPTS namespace boost { namespace stl_interfaces { BOOST_STL_INTERFACES_NAMESPACE_V2 { namespace v2_dtl { // This needs to become an exposition-only snake-case template alias // when standardized. template using container_size_t = typename T::size_type; template // clang-format off concept range_insert = requires (T t, std::ranges::iterator_t t_it, I it) { t.template insert(t_it, it, it); // clang-format on }; template using n_iter_t = detail::n_iter, container_size_t>; } // clang-format off /** A CRTP template that one may derive from to make it easier to define container types. The template parameter `D` for `sequence_container_interface` may be an incomplete type. Before any member of the resulting specialization of `sequence_container_interface` other than special member functions is referenced, `D` shall be complete; shall model `std::derived_from>`, `std::semiregular`, and `std::forward_range`; and shall contain all the nested types required in Table 72: Container requirements and, for those whose iterator nested type models `std::bidirectinal_iterator`, those in Table 73: Reversible container requirements. For an object `d` of type `D`, a call to `std::ranges::begin(d)` shall not mutate any data members of `d`, and `d`'s destructor shall end the lifetimes of the objects in `[std::ranges::begin(d), std::ranges::end(d))`. The `Contiguity` template parameter is not needed, and is unused. It only exists to make the transition from `namespace v1` to `namespace v2` seamless. */ template requires std::is_class_v && std::same_as> struct sequence_container_interface { private: constexpr D& derived() noexcept { return static_cast(*this); } constexpr const D& derived() const noexcept { return static_cast(*this); } constexpr D & mutable_derived() const noexcept { return const_cast(static_cast(*this)); } static constexpr void clear_impl(D& d) noexcept {} static constexpr void clear_impl(D& d) noexcept requires requires { d.clear(); } { d.clear(); } public: constexpr bool empty() const { return std::ranges::begin(derived()) == std::ranges::end(derived()); } constexpr auto data() requires std::contiguous_iterator> { return std::to_address(std::ranges::begin(derived())); } constexpr auto data() const requires std::contiguous_iterator> { return std::to_address(std::ranges::begin(derived())); } template constexpr v2_dtl::container_size_t size() const requires std::sized_sentinel_for, std::ranges::iterator_t> { return v2_dtl::container_size_t( std::ranges::end(derived()) - std::ranges::begin(derived())); } constexpr decltype(auto) front() { BOOST_ASSERT(!empty()); return *std::ranges::begin(derived()); } constexpr decltype(auto) front() const { BOOST_ASSERT(!empty()); return *std::ranges::begin(derived()); } template constexpr void push_front(const std::ranges::range_value_t& x) requires requires (D d) { d.emplace_front(x); } { derived().emplace_front(x); } template constexpr void push_front(std::ranges::range_value_t&& x) requires requires (D d) { d.emplace_front(std::move(x)); } { derived().emplace_front(std::move(x)); } constexpr void pop_front() noexcept requires requires (D d, const std::ranges::range_value_t& x, std::ranges::iterator_t position) { d.emplace_front(x); d.erase(position); } { return derived().erase(std::ranges::begin(derived())); } constexpr decltype(auto) back() requires std::ranges::bidirectional_range && std::ranges::common_range { BOOST_ASSERT(!empty()); return *std::ranges::prev(std::ranges::end(derived())); } constexpr decltype(auto) back() const requires std::ranges::bidirectional_range && std::ranges::common_range { BOOST_ASSERT(!empty()); return *std::ranges::prev(std::ranges::end(derived())); } template constexpr void push_back(const std::ranges::range_value_t& x) requires std::ranges::common_range && requires (D d) { d.emplace_back(x); } { derived().emplace_back(x); } template constexpr void push_back(std::ranges::range_value_t&& x) requires std::ranges::common_range && requires (D d) { d.emplace_back(std::move(x)); } { derived().emplace_back(std::move(x)); } constexpr void pop_back() noexcept requires std::ranges::bidirectional_range && std::ranges::common_range && requires (D d, std::ranges::range_value_t x, std::ranges::iterator_t position) { d.emplace_back(std::move(x)); d.erase(position); } { return derived().erase(std::ranges::prev(std::ranges::end(derived()))); } template constexpr decltype(auto) operator[](v2_dtl::container_size_t n) { return std::ranges::begin(derived())[n]; } template constexpr decltype(auto) operator[](v2_dtl::container_size_t n) const { return std::ranges::begin(derived())[n]; } template constexpr decltype(auto) at(v2_dtl::container_size_t n) { if (derived().size() <= n) throw std::out_of_range("Bounds check failed in sequence_container_interface::at()"); return std::ranges::begin(derived())[n]; } template constexpr decltype(auto) at(v2_dtl::container_size_t n) const { if (derived().size() <= n) throw std::out_of_range("Bounds check failed in sequence_container_interface::at()"); return std::ranges::begin(derived())[n]; } constexpr auto begin() const { return typename D::const_iterator(mutable_derived().begin()); } constexpr auto end() const { return typename D::const_iterator(mutable_derived().end()); } constexpr auto cbegin() const { return derived().begin(); } constexpr auto cend() const { return derived().end(); } constexpr auto rbegin() requires std::ranges::bidirectional_range && std::ranges::common_range { return std::reverse_iterator(std::ranges::end(derived())); } constexpr auto rend() requires std::ranges::bidirectional_range && std::ranges::common_range { return std::reverse_iterator(std::ranges::begin(derived())); } constexpr auto rbegin() const requires std::ranges::bidirectional_range && std::ranges::common_range { return std::reverse_iterator(std::ranges::iterator_t( mutable_derived().end())); } constexpr auto rend() const requires std::ranges::bidirectional_range && std::ranges::common_range { return std::reverse_iterator(std::ranges::iterator_t( mutable_derived().begin())); } constexpr auto crbegin() const requires std::ranges::bidirectional_range && std::ranges::common_range { return std::reverse_iterator(std::ranges::iterator_t( mutable_derived().end())); } constexpr auto crend() const requires std::ranges::bidirectional_range && std::ranges::common_range { return std::reverse_iterator(std::ranges::iterator_t( mutable_derived().begin())); } template constexpr auto insert(std::ranges::iterator_t position, const std::ranges::range_value_t& x) requires requires (D d) { d.emplace(position, x); } { return derived().emplace(position, x); } template constexpr auto insert(std::ranges::iterator_t position, std::ranges::range_value_t&& x) requires requires (D d) { d.emplace(position, std::move(x)); } { return derived().emplace(position, std::move(x)); } template constexpr auto insert(std::ranges::iterator_t position, v2_dtl::container_size_t n, const std::ranges::range_value_t& x) requires v2_dtl::range_insert> { auto first = detail::make_n_iter(x, n); auto last = detail::make_n_iter_end(x, n); return derived().insert( position, detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)); } template constexpr auto insert(std::ranges::iterator_t position, std::initializer_list> il) requires requires (D d) { d.template insert( position, il.begin(), il.end()); } { return derived().insert(position, il.begin(), il.end()); } template constexpr void erase(typename C::const_iterator position) requires requires (D d) { d.erase(position, std::ranges::next(position)); } { derived().erase(position, std::ranges::next(position)); } template constexpr void assign(Iter first, Iter last) requires requires (D d) { d.erase(std::ranges::begin(d), std::ranges::end(d)); d.insert(std::ranges::begin(d), first, last); } { auto out = derived().begin(); auto const out_last = derived().end(); for (; out != out_last && first != last; ++first, ++out) { *out = *first; } if (out != out_last) derived().erase(out, out_last); if (first != last) derived().insert(derived().end(), first, last); } template constexpr void assign(v2_dtl::container_size_t n, const std::ranges::range_value_t& x) requires requires (D d) { { d.size() } -> std::convertible_to; d.erase(std::ranges::begin(d), std::ranges::end(d)); d.insert(std::ranges::begin(d), detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)); } { if (detail::fake_capacity(derived()) < n) { C temp(n, x); derived().swap(temp); } else { auto const min_size = std::min(n, derived().size()); auto const fill_end = std::fill_n(derived().begin(), min_size, x); if (min_size < (std::ptrdiff_t)derived().size()) { derived().erase(fill_end, derived().end()); } else { n -= min_size; derived().insert( derived().begin(), detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)); } } } template constexpr void assign(std::initializer_list> il) requires requires (D d) { d.assign(il.begin(), il.end()); } { derived().assign(il.begin(), il.end()); } constexpr void clear() noexcept requires requires (D d) { d.erase(std::ranges::begin(d), std::ranges::end(d)); } { derived().erase(std::ranges::begin(derived()), std::ranges::end(derived())); } template constexpr decltype(auto) operator=( std::initializer_list> il) requires requires (D d) { d.assign(il.begin(), il.end()); } { derived().assign(il.begin(), il.end()); return *this; } friend constexpr void swap(D& lhs, D& rhs) requires requires { lhs.swap(rhs); } { return lhs.swap(rhs); } friend constexpr bool operator==(const D& lhs, const D& rhs) requires std::ranges::sized_range && requires { std::ranges::equal(lhs, rhs); } { return lhs.size() == rhs.size() && std::ranges::equal(lhs, rhs); } #if 0 // TODO: This appears to work, but as of this writing (and using GCC // 10), op<=> is not yet being used to evaluate op==, op<, etc. friend constexpr std::compare_three_way_result_t> operator<=>(const D& lhs, const D& rhs) requires std::three_way_comparable> { return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); } #else friend constexpr bool operator!=(const D& lhs, const D& rhs) requires requires { lhs == rhs; } { return !(lhs == rhs); } friend constexpr bool operator<(D lhs, D rhs) requires std::totally_ordered> { return std::ranges::lexicographical_compare(lhs, rhs); } friend constexpr bool operator<=(D lhs, D rhs) requires std::totally_ordered> { return lhs == rhs || lhs < rhs; } friend constexpr bool operator>(D lhs, D rhs) requires std::totally_ordered> { return !(lhs <= rhs); } friend constexpr bool operator>=(D lhs, D rhs) requires std::totally_ordered> { return rhs <= lhs; } #endif }; // clang-format on }}} #endif #endif