| 1 | // Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++17 } } |
| 19 | |
| 20 | #include <functional> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | using std::not_fn; |
| 24 | |
| 25 | int func(int, char) { return 0; } |
| 26 | |
| 27 | struct F |
| 28 | { |
| 29 | bool operator()() { return false; } |
| 30 | bool operator()() const { return true; } |
| 31 | bool operator()(int) { return false; } |
| 32 | }; |
| 33 | |
| 34 | void |
| 35 | test01() |
| 36 | { |
| 37 | auto f1 = not_fn(func); |
| 38 | VERIFY( f1(1, '2') == true ); |
| 39 | |
| 40 | auto f2 = not_fn( [] { return true; } ); |
| 41 | VERIFY( f2() == false ); |
| 42 | |
| 43 | auto f3 = not_fn( F{} ); |
| 44 | VERIFY( f3() == true ); |
| 45 | VERIFY( f3(1) == true ); |
| 46 | const auto f4 = f3; |
| 47 | VERIFY( f4() == false ); |
| 48 | } |
| 49 | |
| 50 | template<typename F, typename Arg> |
| 51 | auto foo(F f, Arg arg) -> decltype(not_fn(f)(arg)) { return not_fn(f)(arg); } |
| 52 | |
| 53 | template<typename F, typename Arg> |
| 54 | auto foo(F f, Arg arg) -> decltype(not_fn(f)()) { return not_fn(f)(); } |
| 55 | |
| 56 | struct negator |
| 57 | { |
| 58 | bool operator()(int) const { return false; } |
| 59 | void operator()() const {} |
| 60 | }; |
| 61 | |
| 62 | void |
| 63 | test02() |
| 64 | { |
| 65 | foo(negator{}, 1); // PR libstdc++/66998 |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | test03() |
| 70 | { |
| 71 | struct X { bool b; }; |
| 72 | X x{ false }; |
| 73 | VERIFY( not_fn(&X::b)(x) ); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | test04() |
| 78 | { |
| 79 | struct abstract { virtual void f() = 0; }; |
| 80 | struct derived : abstract { void f() { } }; |
| 81 | struct F { bool operator()(abstract&) { return false; } }; |
| 82 | F f; |
| 83 | derived d; |
| 84 | VERIFY( not_fn(f)(d) ); |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | test05() |
| 89 | { |
| 90 | auto nf = std::not_fn([] { return false; }); |
| 91 | auto copy(nf); // PR libstdc++/70564 |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | test06() |
| 96 | { |
| 97 | struct Boolean { |
| 98 | Boolean operator!() noexcept(false) { return *this; } |
| 99 | }; |
| 100 | struct F { |
| 101 | Boolean operator()() { return {}; } |
| 102 | }; |
| 103 | F f; |
| 104 | auto notf = std::not_fn(f); |
| 105 | using NotF = decltype(notf); |
| 106 | static_assert( std::is_invocable<NotF>::value, "cannot negate" ); |
| 107 | static_assert( !noexcept(notf()), "conversion to bool affects noexcept" ); |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | test07() |
| 112 | { |
| 113 | struct NonNegatable { }; |
| 114 | struct F { |
| 115 | NonNegatable operator()() { return {}; } |
| 116 | }; |
| 117 | F f; |
| 118 | auto notf = std::not_fn(f); |
| 119 | using NotF = decltype(notf); |
| 120 | static_assert( !std::is_invocable<NotF>::value, "cannot negate" ); |
| 121 | } |
| 122 | |
| 123 | int |
| 124 | main() |
| 125 | { |
| 126 | test01(); |
| 127 | test02(); |
| 128 | test03(); |
| 129 | test04(); |
| 130 | test05(); |
| 131 | test06(); |
| 132 | test07(); |
| 133 | } |