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