]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/function_objects/not_fn/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / function_objects / not_fn / 1.cc
CommitLineData
99dee823 1// Copyright (C) 2014-2021 Free Software Foundation, Inc.
e6ee5bfd
JW
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-options "-std=gnu++17" }
6458742a 19// { dg-do run { target c++17 } }
e6ee5bfd
JW
20
21#include <functional>
22#include <testsuite_hooks.h>
23
24using std::not_fn;
25
26int func(int, char) { return 0; }
27
28struct F
29{
30 bool operator()() { return false; }
31 bool operator()() const { return true; }
32 bool operator()(int) { return false; }
33};
34
35void
36test01()
37{
38 auto f1 = not_fn(func);
39 VERIFY( f1(1, '2') == true );
40
41 auto f2 = not_fn( [] { return true; } );
42 VERIFY( f2() == false );
43
44 auto f3 = not_fn( F{} );
45 VERIFY( f3() == true );
46 VERIFY( f3(1) == true );
47 const auto f4 = f3;
48 VERIFY( f4() == false );
49}
50
51template<typename F, typename Arg>
52auto foo(F f, Arg arg) -> decltype(not_fn(f)(arg)) { return not_fn(f)(arg); }
53
54template<typename F, typename Arg>
55auto foo(F f, Arg arg) -> decltype(not_fn(f)()) { return not_fn(f)(); }
56
57struct negator
58{
59 bool operator()(int) const { return false; }
60 void operator()() const {}
61};
62
63void
64test02()
65{
66 foo(negator{}, 1); // PR libstdc++/66998
67}
68
69void
70test03()
71{
72 struct X { bool b; };
73 X x{ false };
74 VERIFY( not_fn(&X::b)(x) );
75}
76
77void
78test04()
79{
80 struct abstract { virtual void f() = 0; };
81 struct derived : abstract { void f() { } };
82 struct F { bool operator()(abstract&) { return false; } };
83 F f;
84 derived d;
85 VERIFY( not_fn(f)(d) );
86}
87
78ec9c15
JW
88void
89test05()
90{
aaae096a
JW
91 auto nf = std::not_fn([] { return false; });
92 auto copy(nf); // PR libstdc++/70564
78ec9c15
JW
93}
94
cc06c7f6
JW
95void
96test06()
97{
98 struct Boolean {
99 Boolean operator!() noexcept(false) { return *this; }
100 };
101 struct F {
102 Boolean operator()() { return {}; }
103 };
104 F f;
105 auto notf = std::not_fn(f);
106 using NotF = decltype(notf);
7dcc645c 107 static_assert( std::is_invocable<NotF>::value, "cannot negate" );
cc06c7f6
JW
108 static_assert( !noexcept(notf()), "conversion to bool affects noexcept" );
109}
110
111void
112test07()
113{
114 struct NonNegatable { };
115 struct F {
116 NonNegatable operator()() { return {}; }
117 };
118 F f;
119 auto notf = std::not_fn(f);
120 using NotF = decltype(notf);
7dcc645c 121 static_assert( !std::is_invocable<NotF>::value, "cannot negate" );
cc06c7f6
JW
122}
123
e6ee5bfd
JW
124int
125main()
126{
127 test01();
128 test02();
129 test03();
130 test04();
78ec9c15 131 test05();
cc06c7f6
JW
132 test06();
133 test07();
e6ee5bfd 134}