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