Horizon
const.hpp
Go to the documentation of this file.
1 // 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 
14 #ifndef RANGES_V3_VIEW_CONST_HPP
15 #define RANGES_V3_VIEW_CONST_HPP
16 
17 #include <type_traits>
18 #include <utility>
19 
20 #include <range/v3/range_fwd.hpp>
21 
27 #include <range/v3/utility/static_const.hpp>
29 #include <range/v3/view/all.hpp>
30 #include <range/v3/view/view.hpp>
31 
32 #include <range/v3/detail/prologue.hpp>
33 
34 namespace ranges
35 {
38  template<typename Rng>
39  struct const_view : view_adaptor<const_view<Rng>, Rng>
40  {
41  private:
42  friend range_access;
43  template<bool Const>
44  struct adaptor : adaptor_base
45  {
46  using CRng = meta::const_if_c<Const, Rng>;
47  using value_ = range_value_t<CRng>;
48  using reference_ =
49  common_reference_t<value_ const &&, range_reference_t<CRng>>;
50  using rvalue_reference_ =
51  common_reference_t<value_ const &&, range_rvalue_reference_t<CRng>>;
52  adaptor() = default;
53  template(bool Other)(
54  requires Const && CPP_NOT(Other)) //
55  constexpr adaptor(adaptor<Other>)
56  {}
57  reference_ read(iterator_t<CRng> const & it) const
58  {
59  return *it;
60  }
61  rvalue_reference_ iter_move(iterator_t<CRng> const & it) const
62  noexcept(noexcept(rvalue_reference_(ranges::iter_move(it))))
63  {
64  return ranges::iter_move(it);
65  }
66  };
67  adaptor<simple_view<Rng>()> begin_adaptor()
68  {
69  return {};
70  }
71  CPP_member
72  auto begin_adaptor() const //
73  -> CPP_ret(adaptor<true>)(
74  requires range<Rng const>)
75  {
76  return {};
77  }
78  adaptor<simple_view<Rng>()> end_adaptor()
79  {
80  return {};
81  }
82  CPP_member
83  auto end_adaptor() const //
84  -> CPP_ret(adaptor<true>)(
85  requires range<Rng const>)
86  {
87  return {};
88  }
89 
90  public:
91  const_view() = default;
92  explicit const_view(Rng rng)
93  : const_view::view_adaptor{std::move(rng)}
94  {}
95  CPP_auto_member
96  constexpr auto CPP_fun(size)()(
97  requires sized_range<Rng>)
98  {
99  return ranges::size(this->base());
100  }
101  CPP_auto_member
102  constexpr auto CPP_fun(size)()(const
103  requires sized_range<Rng const>)
104  {
105  return ranges::size(this->base());
106  }
107  };
108 
109  template<typename Rng>
110  RANGES_INLINE_VAR constexpr bool enable_borrowed_range<const_view<Rng>> = //
111  enable_borrowed_range<Rng>;
112 
113 #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
114  template<typename Rng>
115  const_view(Rng &&) //
117 #endif
118 
119  namespace views
120  {
121  struct const_fn
122  {
123  template(typename Rng)(
124  requires viewable_range<Rng> AND input_range<Rng>)
125  const_view<all_t<Rng>> operator()(Rng && rng) const
126  {
127  return const_view<all_t<Rng>>{all(static_cast<Rng &&>(rng))};
128  }
129  };
130 
134  } // namespace views
136 } // namespace ranges
137 
138 #include <range/v3/detail/epilogue.hpp>
139 #include <range/v3/detail/satisfy_boost_range.hpp>
140 RANGES_SATISFY_BOOST_RANGE(::ranges::const_view)
141 
142 #endif
decltype(begin(declval(Rng &))) iterator_t
Definition: access.hpp:698
RANGES_INLINE_VARIABLE(detail::to_container_fn< detail::from_range< std::vector >>, to_vector) template< template< typename... > class ContT > auto to(RANGES_HIDDEN_DETAIL(detail
For initializing a container of the specified type with the elements of an Range.
Definition: conversion.hpp:399
meta::size_t< L::size()> size
An integral constant wrapper that is the size of the meta::list L.
Definition: meta.hpp:1696
Definition: adaptor.hpp:110
Definition: const.hpp:40
Definition: adaptor.hpp:475
Definition: const.hpp:122
Definition: view.hpp:178