| 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 | // template<class F, class I> |
| 12 | // concept indirect_unary_predicate; |
| 13 | |
| 14 | #include <iterator> |
| 15 | #include <type_traits> |
| 16 | |
| 17 | #include "indirectly_readable.h" |
| 18 | #include "test_macros.h" |
| 19 | |
| 20 | using It = IndirectlyReadable<struct Token>; |
| 21 | |
| 22 | template <class I> |
| 23 | struct GoodPredicate { |
| 24 | bool operator()(std::iter_reference_t<I>) const; |
| 25 | bool operator()(std::iter_value_t<I>&) const; |
| 26 | bool operator()(std::iter_common_reference_t<I>) const; |
| 27 | }; |
| 28 | |
| 29 | // Should work when all constraints are satisfied |
| 30 | static_assert(std::indirect_unary_predicate<GoodPredicate<It>, It>); |
| 31 | static_assert(std::indirect_unary_predicate<bool(*)(int), int*>); |
| 32 | [[maybe_unused]] auto lambda = [](int i) { return i % 2 == 0; }; |
| 33 | static_assert(std::indirect_unary_predicate<decltype(lambda), int*>); |
| 34 | |
| 35 | // Should fail when the iterator is not indirectly_readable |
| 36 | struct NotIndirectlyReadable { }; |
| 37 | static_assert(!std::indirect_unary_predicate<GoodPredicate<NotIndirectlyReadable>, NotIndirectlyReadable>); |
| 38 | |
| 39 | // Should fail when the predicate is not copy constructible |
| 40 | struct BadPredicate1 { |
| 41 | BadPredicate1(BadPredicate1 const&) = delete; |
| 42 | template <class T> bool operator()(T const&) const; |
| 43 | }; |
| 44 | static_assert(!std::indirect_unary_predicate<BadPredicate1, It>); |
| 45 | |
| 46 | // Should fail when the predicate can't be called with std::iter_value_t<It>& |
| 47 | struct BadPredicate2 { |
| 48 | template <class T> bool operator()(T const&) const; |
| 49 | bool operator()(std::iter_value_t<It>&) const = delete; |
| 50 | }; |
| 51 | static_assert(!std::indirect_unary_predicate<BadPredicate2, It>); |
| 52 | |
| 53 | // Should fail when the predicate can't be called with std::iter_reference_t<It> |
| 54 | struct BadPredicate3 { |
| 55 | template <class T> bool operator()(T const&) const; |
| 56 | bool operator()(std::iter_reference_t<It>) const = delete; |
| 57 | }; |
| 58 | static_assert(!std::indirect_unary_predicate<BadPredicate3, It>); |
| 59 | |
| 60 | // This case was made valid by P2997R1. |
| 61 | struct GoodPredicate4 { |
| 62 | template <class T> |
| 63 | bool operator()(T const&) const; |
| 64 | bool operator()(std::iter_common_reference_t<It>) const = delete; |
| 65 | }; |
| 66 | static_assert(std::indirect_unary_predicate<GoodPredicate4, It>); |
| 67 | |
| 68 | // Test ADL-proofing (P2538R1) |
| 69 | #if TEST_STD_VER >= 26 || defined(_LIBCPP_VERSION) |
| 70 | struct Incomplete; |
| 71 | template<class T> struct Holder { T t; }; |
| 72 | struct HolderIncompletePred { bool operator()(Holder<Incomplete>*) const; }; |
| 73 | static_assert(std::indirect_unary_predicate<HolderIncompletePred, Holder<Incomplete>**>); |
| 74 | static_assert(!std::indirect_unary_predicate<Holder<Incomplete>*, Holder<Incomplete>**>); |
| 75 | #endif |
| 76 | |