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 // filter_view() requires std::default_initializable<View> &&
13 //                        std::default_initializable<Pred> = default;
14 
15 #include <ranges>
16 
17 #include <cassert>
18 #include <type_traits>
19 
20 constexpr int buff[] = {1, 2, 3, 4, 5, 6, 7, 8};
21 
22 struct DefaultConstructibleView : std::ranges::view_base {
DefaultConstructibleViewDefaultConstructibleView23   constexpr DefaultConstructibleView() : begin_(buff), end_(buff + 8) { }
beginDefaultConstructibleView24   constexpr int const* begin() const { return begin_; }
endDefaultConstructibleView25   constexpr int const* end() const { return end_; }
26 private:
27   int const* begin_;
28   int const* end_;
29 };
30 
31 struct DefaultConstructiblePredicate {
32   DefaultConstructiblePredicate() = default;
operator ()DefaultConstructiblePredicate33   constexpr bool operator()(int i) const { return i % 2 == 0; }
34 };
35 
36 struct NoDefaultView : std::ranges::view_base {
37   NoDefaultView() = delete;
38   int* begin() const;
39   int* end() const;
40 };
41 
42 struct NoDefaultPredicate {
43   NoDefaultPredicate() = delete;
44   constexpr bool operator()(int) const;
45 };
46 
47 struct NoexceptView : std::ranges::view_base {
48   NoexceptView() noexcept;
49   int const* begin() const;
50   int const* end() const;
51 };
52 
53 struct NoexceptPredicate {
54   NoexceptPredicate() noexcept;
55   bool operator()(int) const;
56 };
57 
test()58 constexpr bool test() {
59   {
60     using View = std::ranges::filter_view<DefaultConstructibleView, DefaultConstructiblePredicate>;
61     View view;
62     auto it = view.begin(), end = view.end();
63     assert(*it++ == 2);
64     assert(*it++ == 4);
65     assert(*it++ == 6);
66     assert(*it++ == 8);
67     assert(it == end);
68   }
69 
70   {
71     using View = std::ranges::filter_view<DefaultConstructibleView, DefaultConstructiblePredicate>;
72     View view = {};
73     auto it = view.begin(), end = view.end();
74     assert(*it++ == 2);
75     assert(*it++ == 4);
76     assert(*it++ == 6);
77     assert(*it++ == 8);
78     assert(it == end);
79   }
80 
81   // Check cases where the default constructor isn't provided
82   {
83     static_assert(!std::is_default_constructible_v<std::ranges::filter_view<NoDefaultView,            DefaultConstructiblePredicate>>);
84     static_assert(!std::is_default_constructible_v<std::ranges::filter_view<DefaultConstructibleView, NoDefaultPredicate>>);
85     static_assert(!std::is_default_constructible_v<std::ranges::filter_view<NoDefaultView,            NoDefaultPredicate>>);
86   }
87 
88   // Check noexcept-ness
89   {
90     {
91       using View = std::ranges::filter_view<DefaultConstructibleView, DefaultConstructiblePredicate>;
92       static_assert(!noexcept(View()));
93     }
94     {
95       using View = std::ranges::filter_view<NoexceptView, NoexceptPredicate>;
96       static_assert(noexcept(View()));
97     }
98   }
99 
100   return true;
101 }
102 
main(int,char **)103 int main(int, char**) {
104   test();
105   static_assert(test());
106 
107   return 0;
108 }
109