You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

concepts.hpp 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_ITERATOR_CONCEPTS_HPP
  14. #define RANGES_V3_ITERATOR_CONCEPTS_HPP
  15. #include <iterator>
  16. #include <type_traits>
  17. #include <meta/meta.hpp>
  18. #include <concepts/concepts.hpp>
  19. #include <range/v3/range_fwd.hpp>
  20. #include <range/v3/functional/comparisons.hpp>
  21. #include <range/v3/functional/concepts.hpp>
  22. #include <range/v3/functional/identity.hpp>
  23. #include <range/v3/functional/invoke.hpp>
  24. #include <range/v3/iterator/access.hpp>
  25. #include <range/v3/iterator/traits.hpp>
  26. #ifdef _GLIBCXX_DEBUG
  27. #include <debug/safe_iterator.h>
  28. #endif
  29. namespace ranges
  30. {
  31. /// \addtogroup group-iterator
  32. /// @{
  33. /// \cond
  34. namespace detail
  35. {
  36. template<typename I>
  37. using iter_traits_t = if_then_t<is_std_iterator_traits_specialized_v<I>,
  38. std::iterator_traits<I>, I>;
  39. #if defined(_GLIBCXX_DEBUG)
  40. template<typename I, typename T, typename Seq>
  41. auto iter_concept_(__gnu_debug::_Safe_iterator<T *, Seq>, priority_tag<3>)
  42. -> CPP_ret(ranges::contiguous_iterator_tag)( //
  43. requires same_as<I, __gnu_debug::_Safe_iterator<T *, Seq>>);
  44. #endif
  45. #if defined(__GLIBCXX__)
  46. template<typename I, typename T, typename Seq>
  47. auto iter_concept_(__gnu_cxx::__normal_iterator<T *, Seq>, priority_tag<3>)
  48. -> CPP_ret(ranges::contiguous_iterator_tag)( //
  49. requires same_as<I, __gnu_cxx::__normal_iterator<T *, Seq>>);
  50. #endif
  51. #if defined(_LIBCPP_VERSION)
  52. template<typename I, typename T>
  53. auto iter_concept_(std::__wrap_iter<T *>, priority_tag<3>)
  54. -> CPP_ret(ranges::contiguous_iterator_tag)( //
  55. requires same_as<I, std::__wrap_iter<T *>>);
  56. #endif
  57. #if defined(_MSVC_STL_VERSION)
  58. template<typename I>
  59. auto iter_concept_(I, priority_tag<3>)
  60. -> CPP_ret(ranges::contiguous_iterator_tag)( //
  61. requires same_as<I, class I::_Array_iterator>);
  62. template<typename I>
  63. auto iter_concept_(I, priority_tag<3>)
  64. -> CPP_ret(ranges::contiguous_iterator_tag)( //
  65. requires same_as<I, class I::_Array_const_iterator>);
  66. template<typename I>
  67. auto iter_concept_(I, priority_tag<3>)
  68. -> CPP_ret(ranges::contiguous_iterator_tag)( //
  69. requires same_as<I, class I::_Vector_iterator>);
  70. template<typename I>
  71. auto iter_concept_(I, priority_tag<3>)
  72. -> CPP_ret(ranges::contiguous_iterator_tag)( //
  73. requires same_as<I, class I::_Vector_const_iterator>);
  74. template<typename I>
  75. auto iter_concept_(I, priority_tag<3>)
  76. -> CPP_ret(ranges::contiguous_iterator_tag)( //
  77. requires same_as<I, class I::_String_iterator>);
  78. template<typename I>
  79. auto iter_concept_(I, priority_tag<3>)
  80. -> CPP_ret(ranges::contiguous_iterator_tag)( //
  81. requires same_as<I, class I::_String_const_iterator>);
  82. template<typename I>
  83. auto iter_concept_(I, priority_tag<3>)
  84. -> CPP_ret(ranges::contiguous_iterator_tag)( //
  85. requires same_as<I, class I::_String_view_iterator>);
  86. #endif
  87. template<typename I, typename T>
  88. auto iter_concept_(T *, priority_tag<3>)
  89. -> CPP_ret(ranges::contiguous_iterator_tag)( //
  90. requires same_as<I, T *>);
  91. template<typename I>
  92. auto iter_concept_(I, priority_tag<2>) ->
  93. typename iter_traits_t<I>::iterator_concept;
  94. template<typename I>
  95. auto iter_concept_(I, priority_tag<1>) ->
  96. typename iter_traits_t<I>::iterator_category;
  97. template<typename I>
  98. auto iter_concept_(I, priority_tag<0>)
  99. -> enable_if_t<!is_std_iterator_traits_specialized_v<I>,
  100. std::random_access_iterator_tag>;
  101. template<typename I>
  102. using iter_concept_t =
  103. decltype(iter_concept_<I>(std::declval<I>(), priority_tag<3>{}));
  104. using ::concepts::detail::weakly_equality_comparable_with_;
  105. } // namespace detail
  106. /// \endcond
  107. // clang-format off
  108. CPP_def
  109. (
  110. template(typename I)
  111. concept readable,
  112. common_reference_with<iter_reference_t<I> &&,
  113. iter_value_t<std::remove_reference_t<I>> &> &&
  114. common_reference_with<iter_reference_t<I> &&,
  115. iter_rvalue_reference_t<I> &&> &&
  116. common_reference_with<iter_rvalue_reference_t<I> &&,
  117. iter_value_t<std::remove_reference_t<I>> const &>
  118. );
  119. CPP_def
  120. (
  121. template(typename Out, typename T)
  122. concept writable,
  123. requires (Out &&o, T &&t)
  124. (
  125. *o = static_cast<T &&>(t),
  126. *((Out &&) o) = static_cast<T &&>(t),
  127. const_cast<iter_reference_t<Out> const &&>(*o) = static_cast<T &&>(t),
  128. const_cast<iter_reference_t<Out> const &&>(*((Out &&) o)) =
  129. static_cast<T &&>(t)
  130. )
  131. );
  132. // clang-format on
  133. /// \cond
  134. namespace detail
  135. {
  136. template<typename D>
  137. RANGES_INLINE_VAR constexpr bool _is_integer_like_ = std::is_integral<D>::value;
  138. // clang-format off
  139. CPP_def
  140. (
  141. template(typename D)
  142. concept integer_like_,
  143. _is_integer_like_<D>
  144. // TODO additional syntactic and semantic requirements
  145. );
  146. #ifdef RANGES_WORKAROUND_MSVC_792338
  147. CPP_def
  148. (
  149. template(typename D)
  150. concept signed_integer_like_,
  151. concepts::type<decltype(std::integral_constant<bool, (D(-1) < D(0))>{})> &&
  152. std::integral_constant<bool, (D(-1) < D(0))>::value
  153. );
  154. #else // ^^^ workaround / no workaround vvv
  155. CPP_def
  156. (
  157. template(typename D)
  158. concept signed_integer_like_,
  159. integer_like_<D> &&
  160. concepts::type<std::integral_constant<bool, (D(-1) < D(0))>> &&
  161. std::integral_constant<bool, (D(-1) < D(0))>::value
  162. );
  163. #endif // RANGES_WORKAROUND_MSVC_792338
  164. // clang-format on
  165. } // namespace detail
  166. /// \endcond
  167. // clang-format off
  168. CPP_def
  169. (
  170. template(typename I)
  171. concept weakly_incrementable,
  172. requires (I i)
  173. (
  174. ++i,
  175. i++,
  176. concepts::requires_<same_as<I&, decltype(++i)>>
  177. ) &&
  178. concepts::type<iter_difference_t<I>> &&
  179. detail::signed_integer_like_<iter_difference_t<I>> &&
  180. semiregular<I>
  181. );
  182. CPP_def
  183. (
  184. template(typename I)
  185. concept incrementable,
  186. requires (I i)
  187. (
  188. concepts::requires_<same_as<I, decltype(i++)>>
  189. ) &&
  190. regular<I> && weakly_incrementable<I>
  191. );
  192. CPP_def
  193. (
  194. template(typename I)
  195. concept input_or_output_iterator,
  196. requires (I i)
  197. (
  198. *i
  199. ) &&
  200. weakly_incrementable<I>
  201. );
  202. CPP_def
  203. (
  204. template(typename S, typename I)
  205. concept sentinel_for,
  206. semiregular<S> && input_or_output_iterator<I> &&
  207. detail::weakly_equality_comparable_with_<S, I>
  208. );
  209. CPP_def
  210. (
  211. template(typename S, typename I)
  212. concept sized_sentinel_for,
  213. requires (S const &s, I const &i)
  214. (
  215. s - i,
  216. i - s,
  217. concepts::requires_<same_as<iter_difference_t<I>, decltype(s - i)>>,
  218. concepts::requires_<same_as<iter_difference_t<I>, decltype(i - s)>>
  219. ) &&
  220. // Short-circuit the test for sentinel_for if we're emulating concepts:
  221. (!defer::is_true<disable_sized_sentinel<meta::_t<std::remove_cv<S>>, meta::_t<std::remove_cv<I>>>> &&
  222. defer::sentinel_for<S, I>)
  223. );
  224. CPP_def
  225. (
  226. template(typename Out, typename T)
  227. concept output_iterator,
  228. requires (Out o, T &&t)
  229. (
  230. *o++ = static_cast<T &&>(t)
  231. ) &&
  232. input_or_output_iterator<Out> && writable<Out, T>
  233. );
  234. CPP_def
  235. (
  236. template(typename I)
  237. concept input_iterator,
  238. input_or_output_iterator<I> && readable<I> &&
  239. derived_from<detail::iter_concept_t<I>, std::input_iterator_tag>
  240. );
  241. CPP_def
  242. (
  243. template(typename I)
  244. concept forward_iterator,
  245. input_iterator<I> && incrementable<I> &&
  246. sentinel_for<I, I> &&
  247. derived_from<detail::iter_concept_t<I>, std::forward_iterator_tag>
  248. );
  249. CPP_def
  250. (
  251. template(typename I)
  252. concept bidirectional_iterator,
  253. requires (I i)
  254. (
  255. --i,
  256. i--,
  257. concepts::requires_<same_as<I&, decltype(--i)>>,
  258. concepts::requires_<same_as<I, decltype(i--)>>
  259. ) &&
  260. forward_iterator<I> &&
  261. derived_from<detail::iter_concept_t<I>, std::bidirectional_iterator_tag>
  262. );
  263. CPP_def
  264. (
  265. template(typename I)
  266. concept random_access_iterator,
  267. requires (I i, iter_difference_t<I> n)
  268. (
  269. i + n,
  270. n + i,
  271. i - n,
  272. i += n,
  273. i -= n,
  274. concepts::requires_<same_as<decltype(i + n), I>>,
  275. concepts::requires_<same_as<decltype(n + i), I>>,
  276. concepts::requires_<same_as<decltype(i - n), I>>,
  277. concepts::requires_<same_as<decltype(i += n), I&>>,
  278. concepts::requires_<same_as<decltype(i -= n), I&>>,
  279. concepts::requires_<same_as<decltype(i[n]), iter_reference_t<I>>>
  280. ) &&
  281. bidirectional_iterator<I> &&
  282. totally_ordered<I> &&
  283. sized_sentinel_for<I, I> &&
  284. derived_from<detail::iter_concept_t<I>, std::random_access_iterator_tag>
  285. );
  286. CPP_def
  287. (
  288. template(typename I)
  289. concept contiguous_iterator,
  290. random_access_iterator<I> &&
  291. derived_from<detail::iter_concept_t<I>, ranges::contiguous_iterator_tag> &&
  292. std::is_lvalue_reference<iter_reference_t<I>>::value &&
  293. same_as<iter_value_t<I>, uncvref_t<iter_reference_t<I>>>
  294. );
  295. // clang-format on
  296. /////////////////////////////////////////////////////////////////////////////////////
  297. // iterator_tag_of
  298. template<typename T>
  299. using iterator_tag_of = concepts::tag_of<
  300. meta::list<contiguous_iterator_concept, random_access_iterator_concept,
  301. bidirectional_iterator_concept, forward_iterator_concept,
  302. input_iterator_concept>,
  303. T>;
  304. /// \cond
  305. namespace detail
  306. {
  307. template<typename, bool>
  308. struct iterator_category_
  309. {};
  310. template<typename I>
  311. struct iterator_category_<I, true>
  312. {
  313. private:
  314. static std::input_iterator_tag test(detail::input_iterator_tag_);
  315. static std::forward_iterator_tag test(detail::forward_iterator_tag_);
  316. static std::bidirectional_iterator_tag test(
  317. detail::bidirectional_iterator_tag_);
  318. static std::random_access_iterator_tag test(
  319. detail::random_access_iterator_tag_);
  320. static ranges::contiguous_iterator_tag test(detail::contiguous_iterator_tag_);
  321. public:
  322. using type = decltype(iterator_category_::test(iterator_tag_of<I>{}));
  323. };
  324. template<typename T>
  325. using iterator_category =
  326. iterator_category_<meta::_t<std::remove_const<T>>,
  327. (bool)input_iterator<meta::_t<std::remove_const<T>>>>;
  328. } // namespace detail
  329. /// \endcond
  330. /// \cond
  331. // Generally useful to know if an iterator is single-pass or not:
  332. // clang-format off
  333. CPP_def
  334. (
  335. template(typename I)
  336. concept single_pass_iterator_,
  337. input_or_output_iterator<I> && !forward_iterator<I>
  338. );
  339. // clang-format on
  340. /// \endcond
  341. ////////////////////////////////////////////////////////////////////////////////////////////
  342. // indirect_result_t
  343. template<typename Fun, typename... Is>
  344. using indirect_result_t =
  345. detail::enable_if_t<(bool)and_v<(bool)readable<Is>...>,
  346. invoke_result_t<Fun, iter_reference_t<Is>...>>;
  347. /// \cond
  348. namespace detail
  349. {
  350. // clang-format off
  351. CPP_def
  352. (
  353. template(typename T1, typename T2, typename T3, typename T4)
  354. concept common_reference_with_4_,
  355. concepts::type<common_reference_t<T1, T2, T3, T4>> &&
  356. convertible_to<T1, common_reference_t<T1, T2, T3, T4>> &&
  357. convertible_to<T2, common_reference_t<T1, T2, T3, T4>> &&
  358. convertible_to<T3, common_reference_t<T1, T2, T3, T4>> &&
  359. convertible_to<T4, common_reference_t<T1, T2, T3, T4>>
  360. // axiom: all permutations of T1,T2,T3,T4 have the same
  361. // common reference type.
  362. );
  363. CPP_def
  364. (
  365. template(typename F, typename I)
  366. concept indirectly_unary_invocable_,
  367. readable<I> &&
  368. invocable<F &, iter_value_t<I> &> &&
  369. invocable<F &, iter_reference_t<I>> &&
  370. invocable<F &, iter_common_reference_t<I>> &&
  371. common_reference_with<
  372. invoke_result_t<F &, iter_value_t<I> &>,
  373. invoke_result_t<F &, iter_reference_t<I>>>
  374. );
  375. // clang-format on
  376. } // namespace detail
  377. /// \endcond
  378. // clang-format off
  379. CPP_def
  380. (
  381. template(typename F, typename I)
  382. concept indirectly_unary_invocable,
  383. detail::indirectly_unary_invocable_<F, I> &&
  384. copy_constructible<F>
  385. );
  386. CPP_def
  387. (
  388. template(typename F, typename I)
  389. concept indirectly_regular_unary_invocable,
  390. readable<I> &&
  391. copy_constructible<F> &&
  392. regular_invocable<F &, iter_value_t<I> &> &&
  393. regular_invocable<F &, iter_reference_t<I>> &&
  394. regular_invocable<F &, iter_common_reference_t<I>> &&
  395. common_reference_with<
  396. invoke_result_t<F &, iter_value_t<I> &>,
  397. invoke_result_t<F &, iter_reference_t<I>>>
  398. );
  399. /// \cond
  400. // Non-standard indirect invocable concepts
  401. CPP_def
  402. (
  403. template(typename F, typename I1, typename I2)
  404. concept indirectly_binary_invocable_,
  405. readable<I1> && readable<I2> &&
  406. copy_constructible<F> &&
  407. invocable<F &, iter_value_t<I1> &, iter_value_t<I2> &> &&
  408. invocable<F &, iter_value_t<I1> &, iter_reference_t<I2>> &&
  409. invocable<F &, iter_reference_t<I1>, iter_value_t<I2> &> &&
  410. invocable<F &, iter_reference_t<I1>, iter_reference_t<I2>> &&
  411. invocable<F &, iter_common_reference_t<I1>, iter_common_reference_t<I2>> &&
  412. detail::common_reference_with_4_<
  413. invoke_result_t<F &, iter_value_t<I1> &, iter_value_t<I2> &>,
  414. invoke_result_t<F &, iter_value_t<I1> &, iter_reference_t<I2>>,
  415. invoke_result_t<F &, iter_reference_t<I1>, iter_value_t<I2> &>,
  416. invoke_result_t<F &, iter_reference_t<I1>, iter_reference_t<I2>>>
  417. );
  418. CPP_def
  419. (
  420. template(typename F, typename I1, typename I2)
  421. concept indirectly_regular_binary_invocable_,
  422. readable<I1> && readable<I2> &&
  423. copy_constructible<F> &&
  424. regular_invocable<F &, iter_value_t<I1> &, iter_value_t<I2> &> &&
  425. regular_invocable<F &, iter_value_t<I1> &, iter_reference_t<I2>> &&
  426. regular_invocable<F &, iter_reference_t<I1>, iter_value_t<I2> &> &&
  427. regular_invocable<F &, iter_reference_t<I1>, iter_reference_t<I2>> &&
  428. regular_invocable<F &, iter_common_reference_t<I1>, iter_common_reference_t<I2>> &&
  429. detail::common_reference_with_4_<
  430. invoke_result_t<F &, iter_value_t<I1> &, iter_value_t<I2> &>,
  431. invoke_result_t<F &, iter_value_t<I1> &, iter_reference_t<I2>>,
  432. invoke_result_t<F &, iter_reference_t<I1>, iter_value_t<I2> &>,
  433. invoke_result_t<F &, iter_reference_t<I1>, iter_reference_t<I2>>>
  434. );
  435. /// \endcond
  436. CPP_def
  437. (
  438. template(typename F, typename I)
  439. concept indirect_unary_predicate,
  440. readable<I> &&
  441. copy_constructible<F> &&
  442. predicate<F &, iter_value_t<I> &> &&
  443. predicate<F &, iter_reference_t<I>> &&
  444. predicate<F &, iter_common_reference_t<I>>
  445. );
  446. CPP_def
  447. (
  448. template(typename F, typename I1, typename I2)
  449. concept indirect_binary_predicate_,
  450. readable<I1> && readable<I2> &&
  451. copy_constructible<F> &&
  452. predicate<F &, iter_value_t<I1> &, iter_value_t<I2> &> &&
  453. predicate<F &, iter_value_t<I1> &, iter_reference_t<I2>> &&
  454. predicate<F &, iter_reference_t<I1>, iter_value_t<I2> &> &&
  455. predicate<F &, iter_reference_t<I1>, iter_reference_t<I2>> &&
  456. predicate<F &, iter_common_reference_t<I1>, iter_common_reference_t<I2>>
  457. );
  458. CPP_def
  459. (
  460. template(typename F, typename I1, typename I2 = I1)
  461. (concept indirect_relation)(F, I1, I2),
  462. readable<I1> && readable<I2> &&
  463. copy_constructible<F> &&
  464. relation<F &, iter_value_t<I1> &, iter_value_t<I2> &> &&
  465. relation<F &, iter_value_t<I1> &, iter_reference_t<I2>> &&
  466. relation<F &, iter_reference_t<I1>, iter_value_t<I2> &> &&
  467. relation<F &, iter_reference_t<I1>, iter_reference_t<I2>> &&
  468. relation<F &, iter_common_reference_t<I1>, iter_common_reference_t<I2>>
  469. );
  470. CPP_def
  471. (
  472. template(class F, class I1, class I2 = I1)
  473. (concept indirect_strict_weak_order)(F, I1, I2),
  474. readable<I1> && readable<I2> &&
  475. copy_constructible<F> &&
  476. strict_weak_order<F &, iter_value_t<I1> &, iter_value_t<I2> &> &&
  477. strict_weak_order<F &, iter_value_t<I1> &, iter_reference_t<I2>> &&
  478. strict_weak_order<F &, iter_reference_t<I1>, iter_value_t<I2> &> &&
  479. strict_weak_order<F &, iter_reference_t<I1>, iter_reference_t<I2>> &&
  480. strict_weak_order<F &, iter_common_reference_t<I1>, iter_common_reference_t<I2>>
  481. );
  482. // clang-format on
  483. ////////////////////////////////////////////////////////////////////////////////////////////
  484. // projected struct, for "projecting" a readable with a unary callable
  485. /// \cond
  486. namespace detail
  487. {
  488. RANGES_DIAGNOSTIC_PUSH
  489. RANGES_DIAGNOSTIC_IGNORE_UNDEFINED_INTERNAL
  490. template<typename I, typename Proj>
  491. struct projected_
  492. {
  493. using reference = indirect_result_t<Proj &, I>;
  494. using value_type = uncvref_t<reference>;
  495. reference operator*() const;
  496. };
  497. RANGES_DIAGNOSTIC_POP
  498. template<typename Proj>
  499. struct select_projected_
  500. {
  501. template<typename I>
  502. using apply =
  503. detail::enable_if_t<(bool)indirectly_regular_unary_invocable<Proj, I>,
  504. detail::projected_<I, Proj>>;
  505. };
  506. template<>
  507. struct select_projected_<identity>
  508. {
  509. template<typename I>
  510. using apply = detail::enable_if_t<(bool)readable<I>, I>;
  511. };
  512. } // namespace detail
  513. /// \endcond
  514. template<typename I, typename Proj>
  515. using projected = typename detail::select_projected_<Proj>::template apply<I>;
  516. template<typename I, typename Proj>
  517. struct incrementable_traits<detail::projected_<I, Proj>> : incrementable_traits<I>
  518. {};
  519. // clang-format off
  520. CPP_def
  521. (
  522. template(typename I, typename O)
  523. concept indirectly_movable,
  524. readable<I> && writable<O, iter_rvalue_reference_t<I>>
  525. );
  526. CPP_def
  527. (
  528. template(typename I, typename O)
  529. concept indirectly_movable_storable,
  530. indirectly_movable<I, O> &&
  531. writable<O, iter_value_t<I>> &&
  532. movable<iter_value_t<I>> &&
  533. constructible_from<iter_value_t<I>, iter_rvalue_reference_t<I>> &&
  534. assignable_from<iter_value_t<I> &, iter_rvalue_reference_t<I>>
  535. );
  536. CPP_def
  537. (
  538. template(typename I, typename O)
  539. concept indirectly_copyable,
  540. readable<I> &&
  541. writable<O, iter_reference_t<I>>
  542. );
  543. CPP_def
  544. (
  545. template(typename I, typename O)
  546. concept indirectly_copyable_storable,
  547. indirectly_copyable<I, O> &&
  548. writable<O, iter_value_t<I> const &> &&
  549. copyable<iter_value_t<I>> &&
  550. constructible_from<iter_value_t<I>, iter_reference_t<I>> &&
  551. assignable_from<iter_value_t<I> &, iter_reference_t<I>>
  552. );
  553. CPP_def
  554. (
  555. template(typename I1, typename I2 = I1)
  556. (concept indirectly_swappable)(I1, I2),
  557. requires (I1 && i1, I2 && i2)
  558. (
  559. ranges::iter_swap((I1 &&) i1, (I2 &&) i2),
  560. ranges::iter_swap((I1 &&) i1, (I1 &&) i1),
  561. ranges::iter_swap((I2 &&) i2, (I2 &&) i2),
  562. ranges::iter_swap((I2 &&) i2, (I1 &&) i1)
  563. ) &&
  564. readable<I1> && readable<I2>
  565. );
  566. CPP_def
  567. (
  568. template(typename I1, typename I2, typename C,
  569. typename P1 = identity, typename P2 = identity)
  570. (concept indirectly_comparable)(I1, I2, C, P1, P2),
  571. indirect_relation<C, projected<I1, P1>, projected<I2, P2>>
  572. );
  573. ////////////////////////////////////////////////////////////////////////////////////////////
  574. // Composite concepts for use defining algorithms:
  575. CPP_def
  576. (
  577. template(typename I)
  578. concept permutable,
  579. forward_iterator<I> &&
  580. indirectly_swappable<I, I> &&
  581. indirectly_movable_storable<I, I>
  582. );
  583. CPP_def
  584. (
  585. template(typename I0, typename I1, typename Out, typename C = less,
  586. typename P0 = identity, typename P1 = identity)
  587. (concept mergeable)(I0, I1, Out, C, P0, P1),
  588. input_iterator<I0> &&
  589. input_iterator<I1> &&
  590. weakly_incrementable<Out> &&
  591. indirectly_copyable<I0, Out> &&
  592. indirectly_copyable<I1, Out> &&
  593. indirect_strict_weak_order<C, projected<I0, P0>, projected<I1, P1>>
  594. );
  595. CPP_def
  596. (
  597. template(typename I, typename C = less, typename P = identity)
  598. (concept sortable)(I, C, P),
  599. permutable<I> &&
  600. indirect_strict_weak_order<C, projected<I, P>>
  601. );
  602. // clang-format on
  603. using sentinel_tag = concepts::tag<sentinel_for_concept>;
  604. using sized_sentinel_tag = concepts::tag<sized_sentinel_for_concept, sentinel_tag>;
  605. template<typename S, typename I>
  606. using sentinel_tag_of =
  607. concepts::tag_of<meta::list<sized_sentinel_for_concept, sentinel_for_concept>, S,
  608. I>;
  609. // Deprecated things:
  610. /// \cond
  611. template<typename I>
  612. using iterator_category RANGES_DEPRECATED(
  613. "iterator_category is deprecated. Use the iterator concepts instead") =
  614. detail::iterator_category<I>;
  615. template<typename I>
  616. using iterator_category_t RANGES_DEPRECATED(
  617. "iterator_category_t is deprecated. Use the iterator concepts instead") =
  618. meta::_t<detail::iterator_category<I>>;
  619. template<typename Fun, typename... Is>
  620. using indirect_invoke_result_t RANGES_DEPRECATED(
  621. "Please switch to indirect_result_t") = indirect_result_t<Fun, Is...>;
  622. template<typename Fun, typename... Is>
  623. struct RANGES_DEPRECATED("Please switch to indirect_result_t") indirect_invoke_result
  624. : meta::defer<indirect_result_t, Fun, Is...>
  625. {};
  626. template<typename Sig>
  627. struct indirect_result_of
  628. {};
  629. template<typename Fun, typename... Is>
  630. struct RANGES_DEPRECATED("Please switch to indirect_result_t")
  631. indirect_result_of<Fun(Is...)> : meta::defer<indirect_result_t, Fun, Is...>
  632. {};
  633. template<typename Sig>
  634. using indirect_result_of_t RANGES_DEPRECATED("Please switch to indirect_result_t") =
  635. meta::_t<indirect_result_of<Sig>>;
  636. /// \endcond
  637. namespace cpp20
  638. {
  639. using ranges::bidirectional_iterator;
  640. using ranges::contiguous_iterator;
  641. using ranges::forward_iterator;
  642. using ranges::incrementable;
  643. using ranges::indirect_relation;
  644. using ranges::indirect_result_t;
  645. using ranges::indirect_strict_weak_order;
  646. using ranges::indirect_unary_predicate;
  647. using ranges::indirectly_comparable;
  648. using ranges::indirectly_copyable;
  649. using ranges::indirectly_copyable_storable;
  650. using ranges::indirectly_movable;
  651. using ranges::indirectly_movable_storable;
  652. using ranges::indirectly_regular_unary_invocable;
  653. using ranges::indirectly_swappable;
  654. using ranges::indirectly_unary_invocable;
  655. using ranges::input_iterator;
  656. using ranges::input_or_output_iterator;
  657. using ranges::mergeable;
  658. using ranges::output_iterator;
  659. using ranges::permutable;
  660. using ranges::projected;
  661. using ranges::random_access_iterator;
  662. using ranges::readable;
  663. using ranges::sentinel_for;
  664. using ranges::sortable;
  665. using ranges::weakly_incrementable;
  666. using ranges::writable;
  667. } // namespace cpp20
  668. /// @}
  669. } // namespace ranges
  670. #ifdef _GLIBCXX_DEBUG
  671. // HACKHACK: workaround underconstrained operator- for libstdc++ debug iterator wrapper
  672. // by intentionally creating an ambiguity when the wrapped types don't support the
  673. // necessary operation.
  674. namespace __gnu_debug
  675. {
  676. template<typename I1, typename I2, typename Seq>
  677. auto operator-(_Safe_iterator<I1, Seq> const &, _Safe_iterator<I2, Seq> const &)
  678. -> CPP_ret(void)( //
  679. requires(!::ranges::sized_sentinel_for<I1, I2>)) = delete;
  680. template<typename I1, typename Seq>
  681. auto operator-(_Safe_iterator<I1, Seq> const &, _Safe_iterator<I1, Seq> const &)
  682. -> CPP_ret(void)( //
  683. requires(!::ranges::sized_sentinel_for<I1, I1>)) = delete;
  684. } // namespace __gnu_debug
  685. #endif
  686. #if defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && _LIBCPP_VERSION <= 3900)
  687. // HACKHACK: workaround libc++ (https://llvm.org/bugs/show_bug.cgi?id=28421)
  688. // and libstdc++ (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71771)
  689. // underconstrained operator- for reverse_iterator by disabling sized_sentinel_for
  690. // when the base iterators do not model sized_sentinel_for.
  691. namespace ranges
  692. {
  693. template<typename S, typename I>
  694. /*inline*/ constexpr bool
  695. disable_sized_sentinel<std::reverse_iterator<S>, std::reverse_iterator<I>> =
  696. !static_cast<bool>(sized_sentinel_for<I, S>);
  697. }
  698. #endif // defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && _LIBCPP_VERSION <= 3900)
  699. #endif // RANGES_V3_ITERATOR_CONCEPTS_HPP