xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/robust_against_nonbool.compile.pass.cpp (revision 02540b2f6dc6c2d42a3365bef78833d7fab98491)
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 // <ranges>
12 //
13 // Range adaptors that take predicates should support predicates that return a non-boolean
14 // value as long as the returned type is implicitly convertible to bool.
15 
16 #include <ranges>
17 
18 #include "boolean_testable.h"
19 
20 template <std::ranges::view View>
use(View view)21 constexpr void use(View view) {
22   // Just use the view in a few ways. Our goal here is to trigger the instantiation
23   // of various functions related to the view and its iterators in the hopes that we
24   // instantiate functions that might have incorrect implementations w.r.t. predicates.
25   auto first = std::ranges::begin(view);
26   auto last  = std::ranges::end(view);
27   ++first;
28   --first;
29   (void)(first == last);
30   (void)(first != last);
31   (void)std::ranges::empty(view);
32 }
33 
34 using Value    = StrictComparable<int>;
35 using Iterator = StrictBooleanIterator<Value*>;
36 using Range    = std::ranges::subrange<Iterator>;
37 auto pred1     = StrictUnaryPredicate;
38 auto pred2     = StrictBinaryPredicate;
39 
f(Range in)40 void f(Range in) {
41   (void)pred1;
42   (void)pred2;
43 
44 #if TEST_STD_VER >= 23
45   {
46     auto view = std::views::chunk_by(in, pred2);
47     use(view);
48   }
49 #endif
50   {
51     auto view = std::views::drop_while(in, pred1);
52     use(view);
53   }
54   {
55     auto view = std::views::filter(in, pred1);
56     use(view);
57   }
58   {
59     auto view = std::views::take_while(in, pred1);
60     use(view);
61   }
62 }
63