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 // common_view() requires default_initializable<V> = default;
15 
16 #include <ranges>
17 #include <cassert>
18 
19 #include "test_iterators.h"
20 #include "test_range.h"
21 
22 int globalBuffer[4] = {1,2,3,4};
23 
24 struct ContiguousView : std::ranges::view_base {
25   int *ptr_;
26   constexpr ContiguousView(int* ptr) : ptr_(ptr) {}
27   constexpr ContiguousView(ContiguousView&&) = default;
28   constexpr ContiguousView& operator=(ContiguousView&&) = default;
29   friend constexpr int* begin(ContiguousView& view) { return view.ptr_; }
30   friend constexpr int* begin(ContiguousView const& view) { return view.ptr_; }
31   friend constexpr sentinel_wrapper<int*> end(ContiguousView& view) {
32     return sentinel_wrapper<int*>{view.ptr_ + 8};
33   }
34   friend constexpr sentinel_wrapper<int*> end(ContiguousView const& view) {
35     return sentinel_wrapper<int*>{view.ptr_ + 8};
36   }
37 };
38 
39 struct CopyableView : std::ranges::view_base {
40   int *ptr_;
41   constexpr CopyableView(int* ptr = globalBuffer) : ptr_(ptr) {}
42   friend constexpr int* begin(CopyableView& view) { return view.ptr_; }
43   friend constexpr int* begin(CopyableView const& view) { return view.ptr_; }
44   friend constexpr sentinel_wrapper<int*> end(CopyableView& view) {
45     return sentinel_wrapper<int*>{view.ptr_ + 4};
46   }
47   friend constexpr sentinel_wrapper<int*> end(CopyableView const& view) {
48     return sentinel_wrapper<int*>{view.ptr_ + 4};
49   }
50 };
51 
52 struct DefaultConstructibleView : std::ranges::view_base {
53   DefaultConstructibleView();
54   friend int* begin(DefaultConstructibleView& view);
55   friend int* begin(DefaultConstructibleView const& view);
56   friend sentinel_wrapper<int*> end(DefaultConstructibleView& view);
57   friend sentinel_wrapper<int*> end(DefaultConstructibleView const& view);
58 };
59 
60 int main(int, char**) {
61   static_assert(!std::default_initializable<std::ranges::common_view<ContiguousView>>);
62   static_assert( std::default_initializable<std::ranges::common_view<DefaultConstructibleView>>);
63 
64   std::ranges::common_view<CopyableView> common;
65   assert(*common.begin() == 1);
66 
67   return 0;
68 }
69