sequence_container_interface.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. // Copyright (C) 2019 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_STL_INTERFACES_CONTAINER_INTERFACE_HPP
  7. #define BOOST_STL_INTERFACES_CONTAINER_INTERFACE_HPP
  8. #include <boost/stl_interfaces/reverse_iterator.hpp>
  9. #include <boost/assert.hpp>
  10. #include <boost/config.hpp>
  11. #include <algorithm>
  12. #include <stdexcept>
  13. #include <cstddef>
  14. namespace boost { namespace stl_interfaces { namespace detail {
  15. template<typename T, typename SizeType>
  16. struct n_iter : iterator_interface<
  17. n_iter<T, SizeType>,
  18. std::random_access_iterator_tag,
  19. T>
  20. {
  21. n_iter() : x_(nullptr), n_(0) {}
  22. n_iter(T const & x, SizeType n) : x_(&x), n_(n) {}
  23. constexpr std::ptrdiff_t operator-(n_iter other) const noexcept
  24. {
  25. return std::ptrdiff_t(n_) - std::ptrdiff_t(other.n_);
  26. }
  27. n_iter & operator+=(std::ptrdiff_t offset)
  28. {
  29. n_ += offset;
  30. return *this;
  31. }
  32. private:
  33. friend access;
  34. constexpr T const *& base_reference() noexcept { return x_; }
  35. constexpr T const * base_reference() const noexcept { return x_; }
  36. T const * x_;
  37. SizeType n_;
  38. };
  39. template<typename T, typename SizeType>
  40. constexpr auto make_n_iter(T const & x, SizeType n) noexcept(
  41. noexcept(n_iter<T, SizeType>(x, n)))
  42. {
  43. using result_type = n_iter<T, SizeType>;
  44. return result_type(x, SizeType(0));
  45. }
  46. template<typename T, typename SizeType>
  47. constexpr auto make_n_iter_end(T const & x, SizeType n) noexcept(
  48. noexcept(n_iter<T, SizeType>(x, n)))
  49. {
  50. return n_iter<T, SizeType>(x, n);
  51. }
  52. template<typename Container>
  53. std::size_t fake_capacity(Container const & c)
  54. {
  55. return SIZE_MAX;
  56. }
  57. template<
  58. typename Container,
  59. typename Enable = decltype(
  60. std::size_t() = std::declval<Container const &>().capacity())>
  61. std::size_t fake_capacity(Container const & c)
  62. {
  63. return c.capacity();
  64. }
  65. }}}
  66. namespace boost { namespace stl_interfaces { inline namespace v1 {
  67. /** A CRTP template that one may derive from to make it easier to define
  68. container types.
  69. The template parameter `D` for `sequence_container_interface` may be
  70. an incomplete type. Before any member of the resulting specialization
  71. of `sequence_container_interface` other than special member functions
  72. is referenced, `D` shall be complete; shall model
  73. `std::derived_from<sequence_container_interface<D>>`,
  74. `std::semiregular`, and `std::forward_range`; and shall contain all
  75. the nested types required in Table 72: Container requirements and, for
  76. those whose iterator nested type models `std::bidirectinal_iterator`,
  77. those in Table 73: Reversible container requirements.
  78. For an object `d` of type `D`, a call to `std::ranges::begin(d)` sxhall
  79. not mutate any data members of `d`, and `d`'s destructor shall end the
  80. lifetimes of the objects in `[std::ranges::begin(d),
  81. std::ranges::end(d))`. */
  82. template<
  83. typename Derived,
  84. element_layout Contiguity = element_layout::discontiguous
  85. #ifndef BOOST_STL_INTERFACES_DOXYGEN
  86. ,
  87. typename E = std::enable_if_t<
  88. std::is_class<Derived>::value &&
  89. std::is_same<Derived, std::remove_cv_t<Derived>>::value>
  90. #endif
  91. >
  92. struct sequence_container_interface;
  93. namespace v1_dtl {
  94. template<typename Iter>
  95. using in_iter = std::is_convertible<
  96. typename std::iterator_traits<Iter>::iterator_category,
  97. std::input_iterator_tag>;
  98. template<typename D, typename = void>
  99. struct clear_impl
  100. {
  101. static constexpr void call(D & d) noexcept {}
  102. };
  103. template<typename D>
  104. struct clear_impl<D, void_t<decltype(std::declval<D>().clear())>>
  105. {
  106. static constexpr void call(D & d) noexcept { d.clear(); }
  107. };
  108. template<typename D, element_layout Contiguity>
  109. void derived_container(sequence_container_interface<D, Contiguity> const &);
  110. }
  111. template<
  112. typename Derived,
  113. element_layout Contiguity
  114. #ifndef BOOST_STL_INTERFACES_DOXYGEN
  115. ,
  116. typename E
  117. #endif
  118. >
  119. struct sequence_container_interface
  120. {
  121. #ifndef BOOST_STL_INTERFACES_DOXYGEN
  122. private:
  123. constexpr Derived & derived() noexcept
  124. {
  125. return static_cast<Derived &>(*this);
  126. }
  127. constexpr const Derived & derived() const noexcept
  128. {
  129. return static_cast<Derived const &>(*this);
  130. }
  131. constexpr Derived & mutable_derived() const noexcept
  132. {
  133. return const_cast<Derived &>(static_cast<Derived const &>(*this));
  134. }
  135. #endif
  136. public:
  137. template<typename D = Derived>
  138. constexpr auto empty() noexcept(
  139. noexcept(std::declval<D &>().begin() == std::declval<D &>().end()))
  140. -> decltype(
  141. std::declval<D &>().begin() == std::declval<D &>().end())
  142. {
  143. return derived().begin() == derived().end();
  144. }
  145. template<typename D = Derived>
  146. constexpr auto empty() const noexcept(noexcept(
  147. std::declval<D const &>().begin() ==
  148. std::declval<D const &>().end()))
  149. -> decltype(
  150. std::declval<D const &>().begin() ==
  151. std::declval<D const &>().end())
  152. {
  153. return derived().begin() == derived().end();
  154. }
  155. template<
  156. typename D = Derived,
  157. element_layout C = Contiguity,
  158. typename Enable = std::enable_if_t<C == element_layout::contiguous>>
  159. constexpr auto data() noexcept(noexcept(std::declval<D &>().begin()))
  160. -> decltype(std::addressof(*std::declval<D &>().begin()))
  161. {
  162. return std::addressof(*derived().begin());
  163. }
  164. template<
  165. typename D = Derived,
  166. element_layout C = Contiguity,
  167. typename Enable = std::enable_if_t<C == element_layout::contiguous>>
  168. constexpr auto data() const
  169. noexcept(noexcept(std::declval<D const &>().begin()))
  170. -> decltype(std::addressof(*std::declval<D const &>().begin()))
  171. {
  172. return std::addressof(*derived().begin());
  173. }
  174. template<typename D = Derived>
  175. constexpr auto size()
  176. #if !BOOST_CLANG
  177. noexcept(noexcept(
  178. std::declval<D &>().end() - std::declval<D &>().begin()))
  179. #endif
  180. -> decltype(typename D::size_type(
  181. std::declval<D &>().end() - std::declval<D &>().begin()))
  182. {
  183. return derived().end() - derived().begin();
  184. }
  185. template<typename D = Derived>
  186. constexpr auto size() const noexcept(noexcept(
  187. std::declval<D const &>().end() -
  188. std::declval<D const &>().begin()))
  189. -> decltype(typename D::size_type(
  190. #if !BOOST_CLANG
  191. std::declval<D const &>().end() -
  192. std::declval<D const &>().begin()
  193. #endif
  194. ))
  195. {
  196. return derived().end() - derived().begin();
  197. }
  198. template<typename D = Derived>
  199. constexpr auto front() noexcept(noexcept(*std::declval<D &>().begin()))
  200. -> decltype(*std::declval<D &>().begin())
  201. {
  202. return *derived().begin();
  203. }
  204. template<typename D = Derived>
  205. constexpr auto front() const
  206. noexcept(noexcept(*std::declval<D const &>().begin()))
  207. -> decltype(*std::declval<D const &>().begin())
  208. {
  209. return *derived().begin();
  210. }
  211. template<typename D = Derived>
  212. constexpr auto push_front(typename D::value_type const & x) noexcept(
  213. noexcept(std::declval<D &>().emplace_front(x)))
  214. -> decltype((void)std::declval<D &>().emplace_front(x))
  215. {
  216. derived().emplace_front(x);
  217. }
  218. template<typename D = Derived>
  219. constexpr auto push_front(typename D::value_type && x) noexcept(
  220. noexcept(std::declval<D &>().emplace_front(std::move(x))))
  221. -> decltype((void)std::declval<D &>().emplace_front(std::move(x)))
  222. {
  223. derived().emplace_front(std::move(x));
  224. }
  225. template<typename D = Derived>
  226. constexpr auto pop_front() noexcept -> decltype(
  227. std::declval<D &>().emplace_front(
  228. std::declval<typename D::value_type &>()),
  229. (void)std::declval<D &>().erase(std::declval<D &>().begin()))
  230. {
  231. derived().erase(derived().begin());
  232. }
  233. template<
  234. typename D = Derived,
  235. typename Enable = std::enable_if_t<
  236. v1_dtl::decrementable_sentinel<D>::value &&
  237. v1_dtl::common_range<D>::value>>
  238. constexpr auto
  239. back() noexcept(noexcept(*std::prev(std::declval<D &>().end())))
  240. -> decltype(*std::prev(std::declval<D &>().end()))
  241. {
  242. return *std::prev(derived().end());
  243. }
  244. template<
  245. typename D = Derived,
  246. typename Enable = std::enable_if_t<
  247. v1_dtl::decrementable_sentinel<D>::value &&
  248. v1_dtl::common_range<D>::value>>
  249. constexpr auto back() const
  250. noexcept(noexcept(*std::prev(std::declval<D const &>().end())))
  251. -> decltype(*std::prev(std::declval<D const &>().end()))
  252. {
  253. return *std::prev(derived().end());
  254. }
  255. template<typename D = Derived>
  256. constexpr auto push_back(typename D::value_type const & x) noexcept(
  257. noexcept(std::declval<D &>().emplace_back(x)))
  258. -> decltype((void)std::declval<D &>().emplace_back(x))
  259. {
  260. derived().emplace_back(x);
  261. }
  262. template<typename D = Derived>
  263. constexpr auto push_back(typename D::value_type && x) noexcept(
  264. noexcept(std::declval<D &>().emplace_back(std::move(x))))
  265. -> decltype((void)std::declval<D &>().emplace_back(std::move(x)))
  266. {
  267. derived().emplace_back(std::move(x));
  268. }
  269. template<typename D = Derived>
  270. constexpr auto pop_back() noexcept -> decltype(
  271. std::declval<D &>().emplace_back(
  272. std::declval<typename D::value_type &>()),
  273. (void)std::declval<D &>().erase(
  274. std::prev(std::declval<D &>().end())))
  275. {
  276. derived().erase(std::prev(derived().end()));
  277. }
  278. template<typename D = Derived>
  279. constexpr auto operator[](typename D::size_type n) noexcept(
  280. noexcept(std::declval<D &>().begin()[n]))
  281. -> decltype(std::declval<D &>().begin()[n])
  282. {
  283. return derived().begin()[n];
  284. }
  285. template<typename D = Derived>
  286. constexpr auto operator[](typename D::size_type n) const
  287. noexcept(noexcept(std::declval<D const &>().begin()[n]))
  288. -> decltype(std::declval<D const &>().begin()[n])
  289. {
  290. return derived().begin()[n];
  291. }
  292. template<typename D = Derived>
  293. constexpr auto at(typename D::size_type i)
  294. -> decltype(std::declval<D &>().size(), std::declval<D &>()[i])
  295. {
  296. if (derived().size() <= i) {
  297. throw std::out_of_range(
  298. "Bounds check failed in sequence_container_interface::at()");
  299. }
  300. return derived()[i];
  301. }
  302. template<typename D = Derived>
  303. constexpr auto at(typename D::size_type i) const -> decltype(
  304. std::declval<D const &>().size(), std::declval<D const &>()[i])
  305. {
  306. if (derived().size() <= i) {
  307. throw std::out_of_range(
  308. "Bounds check failed in sequence_container_interface::at()");
  309. }
  310. return derived()[i];
  311. }
  312. template<typename D = Derived, typename Iter = typename D::const_iterator>
  313. constexpr Iter begin() const
  314. noexcept(noexcept(std::declval<D &>().begin()))
  315. {
  316. return Iter(mutable_derived().begin());
  317. }
  318. template<typename D = Derived, typename Iter = typename D::const_iterator>
  319. constexpr Iter end() const noexcept(noexcept(std::declval<D &>().end()))
  320. {
  321. return Iter(mutable_derived().end());
  322. }
  323. template<typename D = Derived>
  324. constexpr auto cbegin() const
  325. noexcept(noexcept(std::declval<D const &>().begin()))
  326. -> decltype(std::declval<D const &>().begin())
  327. {
  328. return derived().begin();
  329. }
  330. template<typename D = Derived>
  331. constexpr auto cend() const
  332. noexcept(noexcept(std::declval<D const &>().end()))
  333. -> decltype(std::declval<D const &>().end())
  334. {
  335. return derived().end();
  336. }
  337. template<
  338. typename D = Derived,
  339. typename Enable = std::enable_if_t<v1_dtl::common_range<D>::value>>
  340. constexpr auto rbegin() noexcept(noexcept(
  341. stl_interfaces::make_reverse_iterator(std::declval<D &>().end())))
  342. {
  343. return stl_interfaces::make_reverse_iterator(derived().end());
  344. }
  345. template<
  346. typename D = Derived,
  347. typename Enable = std::enable_if_t<v1_dtl::common_range<D>::value>>
  348. constexpr auto rend() noexcept(noexcept(
  349. stl_interfaces::make_reverse_iterator(std::declval<D &>().begin())))
  350. {
  351. return stl_interfaces::make_reverse_iterator(derived().begin());
  352. }
  353. template<typename D = Derived>
  354. constexpr auto rbegin() const
  355. noexcept(noexcept(std::declval<D &>().rbegin()))
  356. {
  357. return
  358. typename D::const_reverse_iterator(mutable_derived().rbegin());
  359. }
  360. template<typename D = Derived>
  361. constexpr auto rend() const
  362. noexcept(noexcept(std::declval<D &>().rend()))
  363. {
  364. return typename D::const_reverse_iterator(mutable_derived().rend());
  365. }
  366. template<typename D = Derived>
  367. constexpr auto crbegin() const
  368. noexcept(noexcept(std::declval<D const &>().rbegin()))
  369. -> decltype(std::declval<D const &>().rbegin())
  370. {
  371. return derived().rbegin();
  372. }
  373. template<typename D = Derived>
  374. constexpr auto crend() const
  375. noexcept(noexcept(std::declval<D const &>().rend()))
  376. -> decltype(std::declval<D const &>().rend())
  377. {
  378. return derived().rend();
  379. }
  380. template<typename D = Derived>
  381. constexpr auto insert(
  382. typename D::const_iterator pos,
  383. typename D::value_type const &
  384. x) noexcept(noexcept(std::declval<D &>().emplace(pos, x)))
  385. -> decltype(std::declval<D &>().emplace(pos, x))
  386. {
  387. return derived().emplace(pos, x);
  388. }
  389. template<typename D = Derived>
  390. constexpr auto insert(
  391. typename D::const_iterator pos,
  392. typename D::value_type &&
  393. x) noexcept(noexcept(std::declval<D &>()
  394. .emplace(pos, std::move(x))))
  395. -> decltype(std::declval<D &>().emplace(pos, std::move(x)))
  396. {
  397. return derived().emplace(pos, std::move(x));
  398. }
  399. template<typename D = Derived>
  400. constexpr auto insert(
  401. typename D::const_iterator pos,
  402. typename D::size_type n,
  403. typename D::value_type const & x)
  404. // If you see an error in this noexcept() expression, that's
  405. // because this function is not properly constrained. In other
  406. // words, Derived does not have a "range" insert like
  407. // insert(position, first, last). If that is the case, this
  408. // function should be removed via SFINAE from overload resolution.
  409. // However, both the trailing decltype code below and a
  410. // std::enable_if in the template parameters do not work. Sorry
  411. // about that. See below for details.
  412. noexcept(noexcept(std::declval<D &>().insert(
  413. pos, detail::make_n_iter(x, n), detail::make_n_iter_end(x, n))))
  414. // This causes the compiler to infinitely recurse into this function's
  415. // declaration, even though the call below does not match the
  416. // signature of this function.
  417. #if 0
  418. -> decltype(std::declval<D &>().insert(
  419. pos, detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)))
  420. #endif
  421. {
  422. return derived().insert(
  423. pos, detail::make_n_iter(x, n), detail::make_n_iter_end(x, n));
  424. }
  425. template<typename D = Derived>
  426. constexpr auto insert(
  427. typename D::const_iterator pos,
  428. std::initializer_list<typename D::value_type>
  429. il) noexcept(noexcept(std::declval<D &>()
  430. .insert(pos, il.begin(), il.end())))
  431. -> decltype(std::declval<D &>().insert(pos, il.begin(), il.end()))
  432. {
  433. return derived().insert(pos, il.begin(), il.end());
  434. }
  435. template<typename D = Derived>
  436. constexpr auto erase(typename D::const_iterator pos) noexcept
  437. -> decltype(std::declval<D &>().erase(pos, std::next(pos)))
  438. {
  439. return derived().erase(pos, std::next(pos));
  440. }
  441. template<
  442. typename InputIterator,
  443. typename D = Derived,
  444. typename Enable =
  445. std::enable_if_t<v1_dtl::in_iter<InputIterator>::value>>
  446. constexpr auto assign(InputIterator first, InputIterator last) noexcept(
  447. noexcept(std::declval<D &>().insert(
  448. std::declval<D &>().begin(), first, last)))
  449. -> decltype(
  450. std::declval<D &>().erase(
  451. std::declval<D &>().begin(), std::declval<D &>().end()),
  452. (void)std::declval<D &>().insert(
  453. std::declval<D &>().begin(), first, last))
  454. {
  455. auto out = derived().begin();
  456. auto const out_last = derived().end();
  457. for (; out != out_last && first != last; ++first, ++out) {
  458. *out = *first;
  459. }
  460. if (out != out_last)
  461. derived().erase(out, out_last);
  462. if (first != last)
  463. derived().insert(derived().end(), first, last);
  464. }
  465. template<typename D = Derived>
  466. constexpr auto assign(
  467. typename D::size_type n,
  468. typename D::value_type const &
  469. x) noexcept(noexcept(std::declval<D &>()
  470. .insert(
  471. std::declval<D &>().begin(),
  472. detail::make_n_iter(x, n),
  473. detail::make_n_iter_end(x, n))))
  474. -> decltype(
  475. std::declval<D &>().size(),
  476. std::declval<D &>().erase(
  477. std::declval<D &>().begin(), std::declval<D &>().end()),
  478. (void)std::declval<D &>().insert(
  479. std::declval<D &>().begin(),
  480. detail::make_n_iter(x, n),
  481. detail::make_n_iter_end(x, n)))
  482. {
  483. if (detail::fake_capacity(derived()) < n) {
  484. Derived temp(n, x);
  485. derived().swap(temp);
  486. } else {
  487. auto const min_size =
  488. std::min<std::ptrdiff_t>(n, derived().size());
  489. auto const fill_end =
  490. std::fill_n(derived().begin(), min_size, x);
  491. if (min_size < (std::ptrdiff_t)derived().size()) {
  492. derived().erase(fill_end, derived().end());
  493. } else {
  494. n -= min_size;
  495. derived().insert(
  496. derived().begin(),
  497. detail::make_n_iter(x, n),
  498. detail::make_n_iter_end(x, n));
  499. }
  500. }
  501. }
  502. template<typename D = Derived>
  503. constexpr auto
  504. assign(std::initializer_list<typename D::value_type> il) noexcept(
  505. noexcept(std::declval<D &>().assign(il.begin(), il.end())))
  506. -> decltype((void)std::declval<D &>().assign(il.begin(), il.end()))
  507. {
  508. derived().assign(il.begin(), il.end());
  509. }
  510. template<typename D = Derived>
  511. constexpr auto
  512. operator=(std::initializer_list<typename D::value_type> il) noexcept(
  513. noexcept(std::declval<D &>().assign(il.begin(), il.end())))
  514. -> decltype(
  515. std::declval<D &>().assign(il.begin(), il.end()),
  516. std::declval<D &>())
  517. {
  518. derived().assign(il.begin(), il.end());
  519. return *this;
  520. }
  521. template<typename D = Derived>
  522. constexpr auto clear() noexcept
  523. -> decltype((void)std::declval<D &>().erase(
  524. std::declval<D &>().begin(), std::declval<D &>().end()))
  525. {
  526. derived().erase(derived().begin(), derived().end());
  527. }
  528. };
  529. /** Implementation of free function `swap()` for all containers derived
  530. from `sequence_container_interface`. */
  531. template<typename ContainerInterface>
  532. constexpr auto swap(
  533. ContainerInterface & lhs,
  534. ContainerInterface & rhs) noexcept(noexcept(lhs.swap(rhs)))
  535. -> decltype(v1_dtl::derived_container(lhs), lhs.swap(rhs))
  536. {
  537. return lhs.swap(rhs);
  538. }
  539. /** Implementation of `operator==()` for all containers derived from
  540. `sequence_container_interface`. */
  541. template<typename ContainerInterface>
  542. constexpr auto
  543. operator==(ContainerInterface const & lhs, ContainerInterface const & rhs) noexcept(
  544. noexcept(lhs.size() == rhs.size()) &&
  545. noexcept(*lhs.begin() == *rhs.begin()))
  546. -> decltype(
  547. v1_dtl::derived_container(lhs),
  548. lhs.size() == rhs.size(),
  549. *lhs.begin() == *rhs.begin(),
  550. true)
  551. {
  552. return lhs.size() == rhs.size() &&
  553. std::equal(lhs.begin(), lhs.end(), rhs.begin());
  554. }
  555. /** Implementation of `operator!=()` for all containers derived from
  556. `sequence_container_interface`. */
  557. template<typename ContainerInterface>
  558. constexpr auto operator!=(
  559. ContainerInterface const & lhs,
  560. ContainerInterface const & rhs) noexcept(noexcept(lhs == rhs))
  561. -> decltype(v1_dtl::derived_container(lhs), lhs == rhs)
  562. {
  563. return !(lhs == rhs);
  564. }
  565. /** Implementation of `operator<()` for all containers derived from
  566. `sequence_container_interface`. */
  567. template<typename ContainerInterface>
  568. constexpr auto operator<(
  569. ContainerInterface const & lhs,
  570. ContainerInterface const &
  571. rhs) noexcept(noexcept(*lhs.begin() < *rhs.begin()))
  572. -> decltype(
  573. v1_dtl::derived_container(lhs), *lhs.begin() < *rhs.begin(), true)
  574. {
  575. auto it1 = lhs.begin();
  576. auto const last1 = lhs.end();
  577. auto it2 = rhs.begin();
  578. auto const last2 = rhs.end();
  579. for (; it1 != last1 && it2 != last2; ++it1, ++it2) {
  580. if (*it1 < *it2)
  581. return true;
  582. if (*it2 < *it1)
  583. return false;
  584. }
  585. return it1 == last1 && it2 != last2;
  586. }
  587. /** Implementation of `operator<=()` for all containers derived from
  588. `sequence_container_interface`. */
  589. template<typename ContainerInterface>
  590. constexpr auto operator<=(
  591. ContainerInterface const & lhs,
  592. ContainerInterface const & rhs) noexcept(noexcept(lhs < rhs))
  593. -> decltype(v1_dtl::derived_container(lhs), lhs < rhs)
  594. {
  595. return !(rhs < lhs);
  596. }
  597. /** Implementation of `operator>()` for all containers derived from
  598. `sequence_container_interface`. */
  599. template<typename ContainerInterface>
  600. constexpr auto operator>(
  601. ContainerInterface const & lhs,
  602. ContainerInterface const & rhs) noexcept(noexcept(lhs < rhs))
  603. -> decltype(v1_dtl::derived_container(lhs), lhs < rhs)
  604. {
  605. return rhs < lhs;
  606. }
  607. /** Implementation of `operator>=()` for all containers derived from
  608. `sequence_container_interface`. */
  609. template<typename ContainerInterface>
  610. constexpr auto operator>=(
  611. ContainerInterface const & lhs,
  612. ContainerInterface const & rhs) noexcept(noexcept(lhs < rhs))
  613. -> decltype(v1_dtl::derived_container(lhs), lhs < rhs)
  614. {
  615. return !(lhs < rhs);
  616. }
  617. }}}
  618. #endif