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