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 // UNSUPPORTED: libcpp-no-concepts
11 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
12 
13 // constexpr explicit common_view(V r);
14 
15 #include <ranges>
16 #include <cassert>
17 
18 #include "test_iterators.h"
19 #include "test_range.h"
20 
21 struct ContiguousView : std::ranges::view_base {
22   int *ptr_;
23   constexpr ContiguousView(int* ptr) : ptr_(ptr) {}
24   constexpr ContiguousView(ContiguousView&&) = default;
25   constexpr ContiguousView& operator=(ContiguousView&&) = default;
26   friend constexpr int* begin(ContiguousView& view) { return view.ptr_; }
27   friend constexpr int* begin(ContiguousView const& view) { return view.ptr_; }
28   friend constexpr sentinel_wrapper<int*> end(ContiguousView& view) {
29     return sentinel_wrapper<int*>{view.ptr_ + 8};
30   }
31   friend constexpr sentinel_wrapper<int*> end(ContiguousView const& view) {
32     return sentinel_wrapper<int*>{view.ptr_ + 8};
33   }
34 };
35 
36 struct CopyableView : std::ranges::view_base {
37   int *ptr_;
38   constexpr CopyableView(int* ptr) : ptr_(ptr) {}
39   friend constexpr int* begin(CopyableView& view) { return view.ptr_; }
40   friend constexpr int* begin(CopyableView const& view) { return view.ptr_; }
41   friend constexpr sentinel_wrapper<int*> end(CopyableView& view) {
42     return sentinel_wrapper<int*>{view.ptr_ + 8};
43   }
44   friend constexpr sentinel_wrapper<int*> end(CopyableView const& view) {
45     return sentinel_wrapper<int*>{view.ptr_ + 8};
46   }
47 };
48 
49 constexpr bool test() {
50   int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
51 
52   {
53     std::ranges::common_view<ContiguousView> common(ContiguousView{buffer});
54     assert(std::move(common).base().ptr_ == buffer);
55   }
56 
57   {
58     ContiguousView v{buffer};
59     std::ranges::common_view<ContiguousView> common(std::move(v));
60     assert(std::move(common).base().ptr_ == buffer);
61   }
62 
63   {
64     const CopyableView v{buffer};
65     const std::ranges::common_view<CopyableView> common(v);
66     assert(common.base().ptr_ == buffer);
67   }
68 
69   return true;
70 }
71 
72 int main(int, char**) {
73   test();
74   static_assert(test());
75 
76   int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
77   const std::ranges::common_view<ContiguousView> common(ContiguousView{buffer});
78   assert(common.begin() == buffer);
79 
80   return 0;
81 }
82