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 // common_view() requires default_initializable<V> = default;
14 
15 #include <ranges>
16 #include <cassert>
17 
18 #include "test_iterators.h"
19 #include "test_range.h"
20 
21 int globalBuffer[4] = {1,2,3,4};
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 = globalBuffer) : 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_ + 4};
45   }
46   friend constexpr sentinel_wrapper<int*> end(CopyableView const& view) {
47     return sentinel_wrapper<int*>{view.ptr_ + 4};
48   }
49 };
50 
51 struct DefaultConstructibleView : std::ranges::view_base {
52   DefaultConstructibleView();
53   friend int* begin(DefaultConstructibleView& view);
54   friend int* begin(DefaultConstructibleView const& view);
55   friend sentinel_wrapper<int*> end(DefaultConstructibleView& view);
56   friend sentinel_wrapper<int*> end(DefaultConstructibleView const& view);
57 };
58 
59 int main(int, char**) {
60   static_assert(!std::default_initializable<std::ranges::common_view<ContiguousView>>);
61   static_assert( std::default_initializable<std::ranges::common_view<DefaultConstructibleView>>);
62 
63   std::ranges::common_view<CopyableView> common;
64   assert(*common.begin() == 1);
65 
66   return 0;
67 }
68