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 end() requires (!simple-view<V>)
12 // { return sentinel<false>(ranges::end(base_), addressof(*pred_)); }
13 // constexpr auto end() const
14 // requires range<const V> &&
15 // indirect_unary_predicate<const Pred, iterator_t<const V>>
16 // { return sentinel<true>(ranges::end(base_), addressof(*pred_)); }
17
18 #include <cassert>
19 #include <ranges>
20 #include <type_traits>
21 #include <utility>
22
23 #include "types.h"
24
25 // Test Constraints
26 template <class T>
27 concept HasConstEnd = requires(const T& ct) { ct.end(); };
28
29 template <class T>
30 concept HasEnd = requires(T& t) { t.end(); };
31
32 template <class T>
33 concept HasConstAndNonConstEnd =
34 HasConstEnd<T> && requires(T& t, const T& ct) { requires !std::same_as<decltype(t.end()), decltype(ct.end())>; };
35
36 template <class T>
37 concept HasOnlyNonConstEnd = HasEnd<T> && !HasConstEnd<T>;
38
39 template <class T>
40 concept HasOnlyConstEnd = HasConstEnd<T> && !HasConstAndNonConstEnd<T>;
41
42 struct Pred {
operator ()Pred43 constexpr bool operator()(int i) const { return i < 5; }
44 };
45
46 static_assert(HasOnlyConstEnd<std::ranges::take_while_view<SimpleView, Pred>>);
47
48 static_assert(HasOnlyNonConstEnd<std::ranges::take_while_view<ConstNotRange, Pred>>);
49
50 static_assert(HasConstAndNonConstEnd<std::ranges::take_while_view<NonSimple, Pred>>);
51
52 struct NotPredForConst {
operator ()NotPredForConst53 constexpr bool operator()(int& i) const { return i > 5; }
54 };
55 static_assert(HasOnlyNonConstEnd<std::ranges::take_while_view<NonSimple, NotPredForConst>>);
56
test()57 constexpr bool test() {
58 // simple-view
59 {
60 int buffer[] = {1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1};
61 SimpleView v{buffer};
62 std::ranges::take_while_view twv(v, Pred{});
63 decltype(auto) it1 = twv.end();
64 assert(it1 == buffer + 4);
65 decltype(auto) it2 = std::as_const(twv).end();
66 assert(it2 == buffer + 4);
67
68 static_assert(std::same_as<decltype(it1), decltype(it2)>);
69 }
70
71 // const not range
72 {
73 int buffer[] = {1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1};
74 ConstNotRange v{buffer};
75 std::ranges::take_while_view twv(v, Pred{});
76 decltype(auto) it1 = twv.end();
77 assert(it1 == buffer + 4);
78 }
79
80 // NonSimple
81 {
82 int buffer[] = {1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1};
83 NonSimple v{buffer};
84 std::ranges::take_while_view twv(v, Pred{});
85 decltype(auto) it1 = twv.end();
86 assert(it1 == buffer + 4);
87 decltype(auto) it2 = std::as_const(twv).end();
88 assert(it2 == buffer + 4);
89
90 static_assert(!std::same_as<decltype(it1), decltype(it2)>);
91 }
92
93 // NotPredForConst
94 // LWG 3450: The const overloads of `take_while_view::begin/end` are underconstrained
95 {
96 int buffer[] = {1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1};
97 NonSimple v{buffer};
98 std::ranges::take_while_view twv(v, NotPredForConst{});
99 decltype(auto) it1 = twv.end();
100 assert(it1 == buffer);
101 }
102
103 return true;
104 }
105
main(int,char **)106 int main(int, char**) {
107 test();
108 static_assert(test());
109 return 0;
110 }
111