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 auto data() requires contiguous_range<R>
14 // constexpr auto data() const requires contiguous_range<const R>
15 
16 #include <ranges>
17 
18 #include <array>
19 #include <cassert>
20 #include <concepts>
21 
22 #include "test_iterators.h"
23 #include "test_macros.h"
24 
25 template <class T>
26 concept HasData = requires (T t) {
27   t.data();
28 };
29 
30 constexpr bool test()
31 {
32   {
33     struct ContiguousIters {
34       contiguous_iterator<int*> begin();
35       sentinel_wrapper<contiguous_iterator<int*>> end();
36     };
37     using OwningView = std::ranges::owning_view<ContiguousIters>;
38     static_assert(std::ranges::contiguous_range<OwningView&>);
39     static_assert(!std::ranges::range<const OwningView&>); // no begin/end
40     static_assert(HasData<OwningView&>);
41     static_assert(HasData<OwningView&&>);
42     static_assert(!HasData<const OwningView&>);
43     static_assert(!HasData<const OwningView&&>);
44   }
45   {
46     struct NoData {
47       random_access_iterator<int*> begin();
48       random_access_iterator<int*> end();
49     };
50     using OwningView = std::ranges::owning_view<NoData>;
51     static_assert(!HasData<OwningView&>);
52     static_assert(!HasData<OwningView&&>);
53     static_assert(!HasData<const OwningView&>);
54     static_assert(!HasData<const OwningView&&>);
55   }
56   {
57     // Test a view.
58     int a[] = {1};
59     auto ov = std::ranges::owning_view(std::ranges::subrange(a, a+1));
60     assert(ov.data() == a);
61     assert(std::as_const(ov).data() == a);
62   }
63   {
64     // Test a non-view.
65     std::array<int, 2> a = {1, 2};
66     auto ov = std::ranges::owning_view(std::move(a));
67     assert(ov.data() != a.data()); // because it points into the copy
68     assert(std::as_const(ov).data() != a.data());
69   }
70   return true;
71 }
72 
73 int main(int, char**) {
74   test();
75   static_assert(test());
76 
77   return 0;
78 }
79