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 begin();
12// constexpr auto begin() const requires range<const V>;
13
14#include <ranges>
15
16#include <cassert>
17#include <concepts>
18#include <utility>
19
20#include "test_iterators.h"
21#include "types.h"
22
23struct MutableView : std::ranges::view_base {
24 int* begin();
25 sentinel_wrapper<int*> end();
26};
27
28template<class View>
29concept BeginEnabled = requires(View v) { v.begin(); };
30
31constexpr bool test() {
32 int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
33
34 {
35 static_assert( BeginEnabled<std::ranges::common_view<CopyableView> const&>);
36 static_assert( BeginEnabled<std::ranges::common_view<MutableView>&>);
37 static_assert(!BeginEnabled<std::ranges::common_view<MutableView> const&>);
38 }
39
40 {
41 SizedRandomAccessView view{buf, buf + 8};
42 std::ranges::common_view<SizedRandomAccessView> common(view);
43 std::same_as<RandomAccessIter> auto begin = common.begin();
44 assert(begin == std::ranges::begin(view));
45 }
46
47 {
48 SizedRandomAccessView view{buf, buf + 8};
49 std::ranges::common_view<SizedRandomAccessView> const common(view);
50 std::same_as<RandomAccessIter> auto begin = common.begin();
51 assert(begin == std::ranges::begin(view));
52 }
53
54 return true;
55}
56
57int main(int, char**) {
58 test();
59 static_assert(test());
60
61 // The non-constexpr tests:
62 int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
63
64 {
65 SizedForwardView view{buf, buf + 8};
66 std::ranges::common_view<SizedForwardView> common(view);
67 using CommonIter = std::common_iterator<ForwardIter, sized_sentinel<ForwardIter>>;
68 std::same_as<CommonIter> auto begin = common.begin();
69 assert(begin == std::ranges::begin(view));
70 std::same_as<CommonIter> auto cbegin = std::as_const(common).begin();
71 assert(cbegin == std::ranges::begin(view));
72 }
73
74 {
75 MoveOnlyView view{buf, buf + 8};
76 std::ranges::common_view<MoveOnlyView> common(std::move(view));
77 using CommonIter = std::common_iterator<int*, sentinel_wrapper<int*>>;
78 std::same_as<CommonIter> auto begin = common.begin();
79 assert(begin == std::ranges::begin(view));
80 }
81
82 {
83 CopyableView view{buf, buf + 8};
84 std::ranges::common_view<CopyableView> const common(view);
85 using CommonIter = std::common_iterator<int*, sentinel_wrapper<int*>>;
86 std::same_as<CommonIter> auto begin = common.begin();
87 assert(begin == std::ranges::begin(view));
88 }
89
90 return 0;
91}
92

source code of libcxx/test/std/ranges/range.adaptors/range.common.view/begin.pass.cpp