| 1 | // { dg-do run { target c++23 } } |
| 2 | // { dg-require-effective-target hosted } |
| 3 | |
| 4 | #include <functional> |
| 5 | #include <utility> |
| 6 | #include <testsuite_hooks.h> |
| 7 | |
| 8 | using std::move_only_function; |
| 9 | |
| 10 | using std::is_same_v; |
| 11 | using std::is_invocable_v; |
| 12 | using std::is_nothrow_invocable_v; |
| 13 | using std::invoke_result_t; |
| 14 | |
| 15 | // Check return types |
| 16 | static_assert( is_same_v<void, invoke_result_t<move_only_function<void()>>> ); |
| 17 | static_assert( is_same_v<int, invoke_result_t<move_only_function<int()>>> ); |
| 18 | static_assert( is_same_v<int&, invoke_result_t<move_only_function<int&()>>> ); |
| 19 | |
| 20 | // With const qualifier |
| 21 | static_assert( ! is_invocable_v< move_only_function<void()> const > ); |
| 22 | static_assert( ! is_invocable_v< move_only_function<void()> const &> ); |
| 23 | static_assert( is_invocable_v< move_only_function<void() const> > ); |
| 24 | static_assert( is_invocable_v< move_only_function<void() const> &> ); |
| 25 | static_assert( is_invocable_v< move_only_function<void() const> const > ); |
| 26 | static_assert( is_invocable_v< move_only_function<void() const> const &> ); |
| 27 | |
| 28 | // With no ref-qualifier |
| 29 | static_assert( is_invocable_v< move_only_function<void()> > ); |
| 30 | static_assert( is_invocable_v< move_only_function<void()> &> ); |
| 31 | static_assert( is_invocable_v< move_only_function<void() const> > ); |
| 32 | static_assert( is_invocable_v< move_only_function<void() const> &> ); |
| 33 | static_assert( is_invocable_v< move_only_function<void() const> const > ); |
| 34 | static_assert( is_invocable_v< move_only_function<void() const> const &> ); |
| 35 | |
| 36 | // With & ref-qualifier |
| 37 | static_assert( ! is_invocable_v< move_only_function<void()&> > ); |
| 38 | static_assert( is_invocable_v< move_only_function<void()&> &> ); |
| 39 | static_assert( is_invocable_v< move_only_function<void() const&> > ); |
| 40 | static_assert( is_invocable_v< move_only_function<void() const&> &> ); |
| 41 | static_assert( is_invocable_v< move_only_function<void() const&> const > ); |
| 42 | static_assert( is_invocable_v< move_only_function<void() const&> const &> ); |
| 43 | |
| 44 | // With && ref-qualifier |
| 45 | static_assert( is_invocable_v< move_only_function<void()&&> > ); |
| 46 | static_assert( ! is_invocable_v< move_only_function<void()&&> &> ); |
| 47 | static_assert( is_invocable_v< move_only_function<void() const&&> > ); |
| 48 | static_assert( ! is_invocable_v< move_only_function<void() const&&> &> ); |
| 49 | static_assert( is_invocable_v< move_only_function<void() const&&> const > ); |
| 50 | static_assert( ! is_invocable_v< move_only_function<void() const&&> const &> ); |
| 51 | |
| 52 | // With noexcept-specifier |
| 53 | static_assert( ! is_nothrow_invocable_v< move_only_function<void()> > ); |
| 54 | static_assert( ! is_nothrow_invocable_v< move_only_function<void() noexcept(false)> > ); |
| 55 | static_assert( is_nothrow_invocable_v< move_only_function<void() noexcept> > ); |
| 56 | static_assert( is_nothrow_invocable_v< move_only_function<void()& noexcept>& > ); |
| 57 | |
| 58 | void |
| 59 | test01() |
| 60 | { |
| 61 | struct F |
| 62 | { |
| 63 | int operator()() { return 0; } |
| 64 | int operator()() const { return 1; } |
| 65 | }; |
| 66 | |
| 67 | move_only_function<int()> f0{F{}}; |
| 68 | VERIFY( f0() == 0 ); |
| 69 | VERIFY( std::move(f0)() == 0 ); |
| 70 | |
| 71 | move_only_function<int() const> f1{F{}}; |
| 72 | VERIFY( f1() == 1 ); |
| 73 | VERIFY( std::as_const(f1)() == 1 ); |
| 74 | VERIFY( std::move(f1)() == 1 ); |
| 75 | VERIFY( std::move(std::as_const(f1))() == 1 ); |
| 76 | |
| 77 | move_only_function<int()&> f2{F{}}; |
| 78 | VERIFY( f2() == 0 ); |
| 79 | // Not rvalue-callable: std::move(f2)() |
| 80 | |
| 81 | move_only_function<int() const&> f3{F{}}; |
| 82 | VERIFY( f3() == 1 ); |
| 83 | VERIFY( std::as_const(f3)() == 1 ); |
| 84 | VERIFY( std::move(f3)() == 1 ); |
| 85 | VERIFY( std::move(std::as_const(f3))() == 1 ); |
| 86 | |
| 87 | move_only_function<int()&&> f4{F{}}; |
| 88 | // Not lvalue-callable: f4() |
| 89 | VERIFY( std::move(f4)() == 0 ); |
| 90 | |
| 91 | move_only_function<int() const&&> f5{F{}}; |
| 92 | // Not lvalue-callable: f5() |
| 93 | VERIFY( std::move(f5)() == 1 ); |
| 94 | VERIFY( std::move(std::as_const(f5))() == 1 ); |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | test02() |
| 99 | { |
| 100 | struct F |
| 101 | { |
| 102 | int operator()() & { return 0; } |
| 103 | int operator()() && { return 1; } |
| 104 | }; |
| 105 | |
| 106 | move_only_function<int()> f0{F{}}; |
| 107 | VERIFY( f0() == 0 ); |
| 108 | VERIFY( std::move(f0)() == 0 ); |
| 109 | |
| 110 | move_only_function<int()&&> f1{F{}}; |
| 111 | // Not lvalue callable: f1() |
| 112 | VERIFY( std::move(f1)() == 1 ); |
| 113 | |
| 114 | move_only_function<int()&> f2{F{}}; |
| 115 | VERIFY( f2() == 0 ); |
| 116 | // Not rvalue-callable: std::move(f2)() |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | test03() |
| 121 | { |
| 122 | struct F |
| 123 | { |
| 124 | int operator()() const & { return 0; } |
| 125 | int operator()() && { return 1; } |
| 126 | }; |
| 127 | |
| 128 | move_only_function<int()> f0{F{}}; |
| 129 | VERIFY( f0() == 0 ); |
| 130 | VERIFY( std::move(f0)() == 0 ); |
| 131 | |
| 132 | move_only_function<int()&&> f1{F{}}; |
| 133 | // Not lvalue callable: f1() |
| 134 | VERIFY( std::move(f1)() == 1 ); |
| 135 | |
| 136 | move_only_function<int() const> f2{F{}}; |
| 137 | VERIFY( f2() == 0 ); |
| 138 | VERIFY( std::as_const(f2)() == 0 ); |
| 139 | VERIFY( std::move(f2)() == 0 ); |
| 140 | VERIFY( std::move(std::as_const(f2))() == 0 ); |
| 141 | |
| 142 | move_only_function<int() const &&> f3{F{}}; |
| 143 | // Not lvalue callable: f3() |
| 144 | VERIFY( std::move(f3)() == 0 ); |
| 145 | VERIFY( std::move(std::as_const(f3))() == 0 ); |
| 146 | |
| 147 | move_only_function<int() const &> f4{F{}}; |
| 148 | VERIFY( f4() == 0 ); |
| 149 | VERIFY( std::as_const(f4)() == 0 ); |
| 150 | // Not rvalue-callable: std::move(f4)() |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | test04() |
| 155 | { |
| 156 | struct F |
| 157 | { |
| 158 | int operator()() & { return 0; } |
| 159 | int operator()() && { return 1; } |
| 160 | int operator()() const & { return 2; } |
| 161 | int operator()() const && { return 3; } |
| 162 | }; |
| 163 | |
| 164 | move_only_function<int()> f0{F{}}; |
| 165 | VERIFY( f0() == 0 ); |
| 166 | VERIFY( std::move(f0)() == 0 ); |
| 167 | |
| 168 | move_only_function<int()&> f1{F{}}; |
| 169 | VERIFY( f1() == 0 ); |
| 170 | // Not rvalue-callable: std::move(f1)() |
| 171 | |
| 172 | move_only_function<int()&&> f2{F{}}; |
| 173 | // Not lvalue callable: f2() |
| 174 | VERIFY( std::move(f2)() == 1 ); |
| 175 | |
| 176 | move_only_function<int() const> f3{F{}}; |
| 177 | VERIFY( f3() == 2 ); |
| 178 | VERIFY( std::as_const(f3)() == 2 ); |
| 179 | VERIFY( std::move(f3)() == 2 ); |
| 180 | VERIFY( std::move(std::as_const(f3))() == 2 ); |
| 181 | |
| 182 | move_only_function<int() const &> f4{F{}}; |
| 183 | VERIFY( f4() == 2 ); |
| 184 | VERIFY( std::as_const(f4)() == 2 ); |
| 185 | // Not rvalue-callable: std::move(f4)() |
| 186 | |
| 187 | move_only_function<int() const &&> f5{F{}}; |
| 188 | // Not lvalue callable: f5() |
| 189 | VERIFY( std::move(f5)() == 3 ); |
| 190 | VERIFY( std::move(std::as_const(f5))() == 3 ); |
| 191 | } |
| 192 | |
| 193 | struct Incomplete; |
| 194 | |
| 195 | void |
| 196 | test_params() |
| 197 | { |
| 198 | std::move_only_function<void(Incomplete)> f1; |
| 199 | std::move_only_function<void(Incomplete&)> f2; |
| 200 | std::move_only_function<void(Incomplete&&)> f3; |
| 201 | } |
| 202 | |
| 203 | int main() |
| 204 | { |
| 205 | test01(); |
| 206 | test02(); |
| 207 | test03(); |
| 208 | test04(); |
| 209 | test_params(); |
| 210 | } |