subrange.h 966 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #pragma once
  2. #include <iterator>
  3. #include <stream/forward.h>
  4. #include <stream/detail/traits.h>
  5. #include <stream/view/interface.h>
  6. #include <stream/detail/macro.h>
  7. namespace stream::ranges {
  8. template <typename It, typename S = It>
  9. class subrange : public view_interface<subrange<It, S>, It, S> {
  10. private:
  11. It begin_;
  12. S end_;
  13. public:
  14. subrange(It begin, S end) : begin_(begin), end_(end) {}
  15. template <typename C, REQUIRES(detail::is_container_v<C>)>
  16. subrange(C && container)
  17. : subrange(std::begin(container), std::end(container)) {
  18. // TODO: permissible dangling
  19. static_assert(std::is_reference_v<C>,
  20. "Cannot access iterator of a temporary");
  21. }
  22. auto begin() const { return begin_; }
  23. auto end() const { return end_; }
  24. };
  25. template <typename It, typename S> subrange(It, S) -> subrange<It, S>;
  26. template <typename C>
  27. subrange(C &&) -> subrange<detail::begin_t<C>, detail::end_t<C>>;
  28. }
  29. #include <stream/detail/undef.h>