| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 10 | |
| 11 | // <ranges> |
| 12 | |
| 13 | // template<class T> |
| 14 | // concept view = ...; |
| 15 | |
| 16 | #include <ranges> |
| 17 | |
| 18 | // The type would be a view, but it's not moveable. |
| 19 | struct NotMoveable : std::ranges::view_base { |
| 20 | NotMoveable() = default; |
| 21 | NotMoveable(NotMoveable&&) = delete; |
| 22 | NotMoveable& operator=(NotMoveable&&) = delete; |
| 23 | friend int* begin(NotMoveable&); |
| 24 | friend int* begin(NotMoveable const&); |
| 25 | friend int* end(NotMoveable&); |
| 26 | friend int* end(NotMoveable const&); |
| 27 | }; |
| 28 | static_assert(std::ranges::range<NotMoveable>); |
| 29 | static_assert(!std::movable<NotMoveable>); |
| 30 | static_assert(std::default_initializable<NotMoveable>); |
| 31 | static_assert(std::ranges::enable_view<NotMoveable>); |
| 32 | static_assert(!std::ranges::view<NotMoveable>); |
| 33 | |
| 34 | // The type would be a view, but it's not default initializable |
| 35 | struct NotDefaultInit : std::ranges::view_base { |
| 36 | NotDefaultInit() = delete; |
| 37 | friend int* begin(NotDefaultInit&); |
| 38 | friend int* begin(NotDefaultInit const&); |
| 39 | friend int* end(NotDefaultInit&); |
| 40 | friend int* end(NotDefaultInit const&); |
| 41 | }; |
| 42 | static_assert(std::ranges::range<NotDefaultInit>); |
| 43 | static_assert(std::movable<NotDefaultInit>); |
| 44 | static_assert(!std::default_initializable<NotDefaultInit>); |
| 45 | static_assert(std::ranges::enable_view<NotDefaultInit>); |
| 46 | static_assert(std::ranges::view<NotDefaultInit>); |
| 47 | |
| 48 | // The type would be a view, but it doesn't enable it with enable_view |
| 49 | struct NotExplicitlyEnabled { |
| 50 | NotExplicitlyEnabled() = default; |
| 51 | NotExplicitlyEnabled(NotExplicitlyEnabled&&) = default; |
| 52 | NotExplicitlyEnabled& operator=(NotExplicitlyEnabled&&) = default; |
| 53 | friend int* begin(NotExplicitlyEnabled&); |
| 54 | friend int* begin(NotExplicitlyEnabled const&); |
| 55 | friend int* end(NotExplicitlyEnabled&); |
| 56 | friend int* end(NotExplicitlyEnabled const&); |
| 57 | }; |
| 58 | static_assert(std::ranges::range<NotExplicitlyEnabled>); |
| 59 | static_assert(std::movable<NotExplicitlyEnabled>); |
| 60 | static_assert(std::default_initializable<NotExplicitlyEnabled>); |
| 61 | static_assert(!std::ranges::enable_view<NotExplicitlyEnabled>); |
| 62 | static_assert(!std::ranges::view<NotExplicitlyEnabled>); |
| 63 | |
| 64 | // The type has everything else, but it's not a range |
| 65 | struct NotARange : std::ranges::view_base { |
| 66 | NotARange() = default; |
| 67 | NotARange(NotARange&&) = default; |
| 68 | NotARange& operator=(NotARange&&) = default; |
| 69 | }; |
| 70 | static_assert(!std::ranges::range<NotARange>); |
| 71 | static_assert(std::movable<NotARange>); |
| 72 | static_assert(std::default_initializable<NotARange>); |
| 73 | static_assert(std::ranges::enable_view<NotARange>); |
| 74 | static_assert(!std::ranges::view<NotARange>); |
| 75 | |
| 76 | // The type satisfies all requirements |
| 77 | struct View : std::ranges::view_base { |
| 78 | View() = default; |
| 79 | View(View&&) = default; |
| 80 | View& operator=(View&&) = default; |
| 81 | friend int* begin(View&); |
| 82 | friend int* begin(View const&); |
| 83 | friend int* end(View&); |
| 84 | friend int* end(View const&); |
| 85 | }; |
| 86 | static_assert(std::ranges::range<View>); |
| 87 | static_assert(std::movable<View>); |
| 88 | static_assert(std::default_initializable<View>); |
| 89 | static_assert(std::ranges::enable_view<View>); |
| 90 | static_assert(std::ranges::view<View>); |
| 91 | |
| 92 | // const view types |
| 93 | |
| 94 | struct ConstView1 : std::ranges::view_base { |
| 95 | ConstView1(const ConstView1&&); |
| 96 | const ConstView1& operator=(const ConstView1&&) const; |
| 97 | |
| 98 | friend void swap(const ConstView1&, const ConstView1&); |
| 99 | |
| 100 | friend int* begin(const ConstView1&); |
| 101 | friend int* end(const ConstView1&); |
| 102 | }; |
| 103 | static_assert(std::ranges::range<const ConstView1>); |
| 104 | static_assert(std::movable<const ConstView1>); |
| 105 | static_assert(!std::default_initializable<const ConstView1>); |
| 106 | static_assert(std::ranges::enable_view<const ConstView1>); |
| 107 | static_assert(std::ranges::view<const ConstView1>); |
| 108 | |
| 109 | struct ConstView2 : std::ranges::view_interface<ConstView2> { |
| 110 | ConstView2(const ConstView2&&); |
| 111 | const ConstView2& operator=(const ConstView2&&) const; |
| 112 | |
| 113 | friend void swap(const ConstView2&, const ConstView2&); |
| 114 | |
| 115 | friend int* begin(const ConstView2&); |
| 116 | friend int* end(const ConstView2&); |
| 117 | }; |
| 118 | static_assert(std::ranges::range<const ConstView2>); |
| 119 | static_assert(std::movable<const ConstView2>); |
| 120 | static_assert(!std::default_initializable<const ConstView2>); |
| 121 | static_assert(std::ranges::enable_view<const ConstView2>); |
| 122 | static_assert(std::ranges::view<const ConstView2>); |
| 123 | |
| 124 | // volatile view types |
| 125 | struct VolatileView1 : std::ranges::view_base { |
| 126 | VolatileView1(volatile VolatileView1&&); |
| 127 | volatile VolatileView1& operator=(volatile VolatileView1&&) volatile; |
| 128 | |
| 129 | friend void swap(volatile VolatileView1&, volatile VolatileView1&); |
| 130 | |
| 131 | friend int* begin(volatile VolatileView1&); |
| 132 | friend int* end(volatile VolatileView1&); |
| 133 | }; |
| 134 | static_assert(std::ranges::range<volatile VolatileView1>); |
| 135 | static_assert(std::movable<volatile VolatileView1>); |
| 136 | static_assert(!std::default_initializable<volatile VolatileView1>); |
| 137 | static_assert(std::ranges::enable_view<volatile VolatileView1>); |
| 138 | static_assert(std::ranges::view<volatile VolatileView1>); |
| 139 | |
| 140 | struct VolatileView2 : std::ranges::view_interface<VolatileView2> { |
| 141 | VolatileView2(volatile VolatileView2&&); |
| 142 | volatile VolatileView2& operator=(volatile VolatileView2&&) volatile; |
| 143 | |
| 144 | friend void swap(volatile VolatileView2&, volatile VolatileView2&); |
| 145 | |
| 146 | friend int* begin(volatile VolatileView2&); |
| 147 | friend int* end(volatile VolatileView2&); |
| 148 | }; |
| 149 | static_assert(std::ranges::range<volatile VolatileView2>); |
| 150 | static_assert(std::movable<volatile VolatileView2>); |
| 151 | static_assert(!std::default_initializable<volatile VolatileView2>); |
| 152 | static_assert(std::ranges::enable_view<volatile VolatileView2>); |
| 153 | static_assert(std::ranges::view<volatile VolatileView2>); |
| 154 | |
| 155 | // const-volatile view types |
| 156 | |
| 157 | struct ConstVolatileView1 : std::ranges::view_base { |
| 158 | ConstVolatileView1(const volatile ConstVolatileView1&&); |
| 159 | const volatile ConstVolatileView1& operator=(const volatile ConstVolatileView1&&) const volatile; |
| 160 | |
| 161 | friend void swap(const volatile ConstVolatileView1&, const volatile ConstVolatileView1&); |
| 162 | |
| 163 | friend int* begin(const volatile ConstVolatileView1&); |
| 164 | friend int* end(const volatile ConstVolatileView1&); |
| 165 | }; |
| 166 | static_assert(std::ranges::range<const volatile ConstVolatileView1>); |
| 167 | static_assert(std::movable<const volatile ConstVolatileView1>); |
| 168 | static_assert(!std::default_initializable<const volatile ConstVolatileView1>); |
| 169 | static_assert(std::ranges::enable_view<const volatile ConstVolatileView1>); |
| 170 | static_assert(std::ranges::view<const volatile ConstVolatileView1>); |
| 171 | |
| 172 | struct ConstVolatileView2 : std::ranges::view_interface<ConstVolatileView2> { |
| 173 | ConstVolatileView2(const volatile ConstVolatileView2&&); |
| 174 | const volatile ConstVolatileView2& operator=(const volatile ConstVolatileView2&&) const volatile; |
| 175 | |
| 176 | friend void swap(const volatile ConstVolatileView2&, const volatile ConstVolatileView2&); |
| 177 | |
| 178 | friend int* begin(const volatile ConstVolatileView2&); |
| 179 | friend int* end(const volatile ConstVolatileView2&); |
| 180 | }; |
| 181 | static_assert(std::ranges::range<const volatile ConstVolatileView2>); |
| 182 | static_assert(std::movable<const volatile ConstVolatileView2>); |
| 183 | static_assert(!std::default_initializable<const volatile ConstVolatileView2>); |
| 184 | static_assert(std::ranges::enable_view<const volatile ConstVolatileView2>); |
| 185 | static_assert(std::ranges::view<const volatile ConstVolatileView2>); |
| 186 | |