]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/function_objects/not_fn/1.cc
1 // Copyright (C) 2014-2023 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do run { target c++17 } }
21 #include <testsuite_hooks.h>
25 int func(int, char) { return 0; }
29 bool operator()() { return false; }
30 bool operator()() const { return true; }
31 bool operator()(int) { return false; }
37 auto f1
= not_fn(func
);
38 VERIFY( f1(1, '2') == true );
40 auto f2
= not_fn( [] { return true; } );
41 VERIFY( f2() == false );
43 auto f3
= not_fn( F
{} );
44 VERIFY( f3() == true );
45 VERIFY( f3(1) == true );
47 VERIFY( f4() == false );
50 template<typename F
, typename Arg
>
51 auto foo(F f
, Arg arg
) -> decltype(not_fn(f
)(arg
)) { return not_fn(f
)(arg
); }
53 template<typename F
, typename Arg
>
54 auto foo(F f
, Arg arg
) -> decltype(not_fn(f
)()) { return not_fn(f
)(); }
58 bool operator()(int) const { return false; }
59 void operator()() const {}
65 foo(negator
{}, 1); // PR libstdc++/66998
73 VERIFY( not_fn(&X::b
)(x
) );
79 struct abstract
{ virtual void f() = 0; };
80 struct derived
: abstract
{ void f() { } };
81 struct F
{ bool operator()(abstract
&) { return false; } };
84 VERIFY( not_fn(f
)(d
) );
90 auto nf
= std::not_fn([] { return false; });
91 auto copy(nf
); // PR libstdc++/70564
98 Boolean
operator!() noexcept(false) { return *this; }
101 Boolean
operator()() { return {}; }
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" );
113 struct NonNegatable
{ };
115 NonNegatable
operator()() { return {}; }
118 auto notf
= std::not_fn(f
);
119 using NotF
= decltype(notf
);
120 static_assert( !std::is_invocable
<NotF
>::value
, "cannot negate" );