| 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 indirectly_unary_invocable; |
| 13 | |
| 14 | #include <iterator> |
| 15 | #include <concepts> |
| 16 | |
| 17 | #include "indirectly_readable.h" |
| 18 | #include "test_macros.h" |
| 19 | |
| 20 | using It = IndirectlyReadable<struct Token>; |
| 21 | using R1 = T1<struct ReturnToken>; |
| 22 | using R2 = T2<struct ReturnToken>; |
| 23 | |
| 24 | template <class I> |
| 25 | struct GoodInvocable { |
| 26 | R1 operator()(std::iter_value_t<I>&) const; |
| 27 | R2 operator()(std::iter_reference_t<I>) const; |
| 28 | R2 operator()(std::iter_common_reference_t<I>) const; |
| 29 | }; |
| 30 | |
| 31 | // Should work when all constraints are satisfied |
| 32 | static_assert(std::indirectly_unary_invocable<GoodInvocable<It>, It>); |
| 33 | |
| 34 | // Should fail when the iterator is not indirectly_readable |
| 35 | struct NotIndirectlyReadable { }; |
| 36 | static_assert(!std::indirectly_unary_invocable<GoodInvocable<NotIndirectlyReadable>, NotIndirectlyReadable>); |
| 37 | |
| 38 | // Should fail when the invocable is not copy constructible |
| 39 | struct BadInvocable1 { |
| 40 | BadInvocable1(BadInvocable1 const&) = delete; |
| 41 | template <class T> R1 operator()(T const&) const; |
| 42 | }; |
| 43 | static_assert(!std::indirectly_unary_invocable<BadInvocable1, It>); |
| 44 | |
| 45 | // Should fail when the invocable can't be called with (iter_value_t&) |
| 46 | struct BadInvocable2 { |
| 47 | template <class T> R1 operator()(T const&) const; |
| 48 | R1 operator()(std::iter_value_t<It>&) const = delete; |
| 49 | }; |
| 50 | static_assert(!std::indirectly_unary_invocable<BadInvocable2, It>); |
| 51 | |
| 52 | // Should fail when the invocable can't be called with (iter_reference_t) |
| 53 | struct BadInvocable3 { |
| 54 | template <class T> R1 operator()(T const&) const; |
| 55 | R1 operator()(std::iter_reference_t<It>) const = delete; |
| 56 | }; |
| 57 | static_assert(!std::indirectly_unary_invocable<BadInvocable3, It>); |
| 58 | |
| 59 | // This case was made valid by P2997R1. |
| 60 | struct GoodInvocable4 { |
| 61 | template <class T> |
| 62 | R1 operator()(T const&) const; |
| 63 | R1 operator()(std::iter_common_reference_t<It>) const = delete; |
| 64 | }; |
| 65 | static_assert(std::indirectly_unary_invocable<GoodInvocable4, It>); |
| 66 | |
| 67 | // Should fail when the invocable doesn't have a common reference between its return types |
| 68 | struct BadInvocable5 { |
| 69 | R1 operator()(std::iter_value_t<It>&) const; |
| 70 | struct Unrelated { }; |
| 71 | Unrelated operator()(std::iter_reference_t<It>) const; |
| 72 | R1 operator()(std::iter_common_reference_t<It>) const; |
| 73 | }; |
| 74 | static_assert(!std::indirectly_unary_invocable<BadInvocable5, It>); |
| 75 | |
| 76 | // Various tests with callables |
| 77 | struct S; |
| 78 | static_assert(std::indirectly_unary_invocable<int (*)(int), int*>); |
| 79 | static_assert(std::indirectly_unary_invocable<int (&)(int), int*>); |
| 80 | static_assert(std::indirectly_unary_invocable<int S::*, S*>); |
| 81 | static_assert(std::indirectly_unary_invocable<int (S::*)(), S*>); |
| 82 | static_assert(std::indirectly_unary_invocable<int (S::*)() const, S*>); |
| 83 | static_assert(std::indirectly_unary_invocable<void(*)(int), int*>); |
| 84 | |
| 85 | static_assert(!std::indirectly_unary_invocable<int(int), int*>); // not move constructible |
| 86 | static_assert(!std::indirectly_unary_invocable<int (*)(int*, int*), int*>); |
| 87 | static_assert(!std::indirectly_unary_invocable<int (&)(int*, int*), int*>); |
| 88 | static_assert(!std::indirectly_unary_invocable<int (S::*)(int*), S*>); |
| 89 | static_assert(!std::indirectly_unary_invocable<int (S::*)(int*) const, S*>); |
| 90 | |
| 91 | // Test ADL-proofing (P2538R1) |
| 92 | #if TEST_STD_VER >= 26 || defined(_LIBCPP_VERSION) |
| 93 | struct Incomplete; |
| 94 | template<class T> struct Holder { T t; }; |
| 95 | struct HolderIncompletePred { bool operator()(Holder<Incomplete>*) const; }; |
| 96 | static_assert(std::indirectly_unary_invocable<HolderIncompletePred, Holder<Incomplete>**>); |
| 97 | static_assert(!std::indirectly_unary_invocable<Holder<Incomplete>*, Holder<Incomplete>**>); |
| 98 | #endif |
| 99 | |