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

source code of libcxx/test/std/ranges/range.adaptors/range.all/range.owning.view/data.pass.cpp