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++98, c++03, c++11, c++14 10 11 // <functional> 12 13 // template<class F, class... Args> 14 // invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17 15 // noexcept(is_nothrow_invocable_v<_Fn, _Args...>); 16 17 /// C++14 [func.def] 20.9.0 18 /// (1) The following definitions apply to this Clause: 19 /// (2) A call signature is the name of a return type followed by a parenthesized 20 /// comma-separated list of zero or more argument types. 21 /// (3) A callable type is a function object type (20.9) or a pointer to member. 22 /// (4) A callable object is an object of a callable type. 23 /// (5) A call wrapper type is a type that holds a callable object and supports 24 /// a call operation that forwards to that object. 25 /// (6) A call wrapper is an object of a call wrapper type. 26 /// (7) A target object is the callable object held by a call wrapper. 27 28 /// C++14 [func.require] 20.9.1 29 /// 30 /// Define INVOKE (f, t1, t2, ..., tN) as follows: 31 /// (1.1) - (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is an object of 32 /// type T or a reference to an object of type T or a reference to an object of a type derived from T; 33 /// (1.2) - ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is not one of 34 /// the types described in the previous item; 35 /// (1.3) - t1.*f when N == 1 and f is a pointer to member data of a class T and t1 is an object of type T or a 36 /// reference to an object of type T or a reference to an object of a type derived from T; 37 /// (1.4) - (*t1).*f when N == 1 and f is a pointer to member data of a class T and t1 is not one of the types 38 /// described in the previous item; 39 /// (1.5) - f(t1, t2, ..., tN) in all other cases. 40 41 #include <functional> 42 #include <type_traits> 43 #include <utility> // for std::move 44 #include <cassert> 45 46 #include "test_macros.h" 47 48 struct NonCopyable { 49 NonCopyable() {} 50 private: 51 NonCopyable(NonCopyable const&) = delete; 52 NonCopyable& operator=(NonCopyable const&) = delete; 53 }; 54 55 struct TestClass { 56 explicit TestClass(int x) : data(x) {} 57 58 int& operator()(NonCopyable&&) & { return data; } 59 int const& operator()(NonCopyable&&) const & { return data; } 60 int volatile& operator()(NonCopyable&&) volatile & { return data; } 61 int const volatile& operator()(NonCopyable&&) const volatile & { return data; } 62 63 int&& operator()(NonCopyable&&) && { return std::move(data); } 64 int const&& operator()(NonCopyable&&) const && { return std::move(data); } 65 int volatile&& operator()(NonCopyable&&) volatile && { return std::move(data); } 66 int const volatile&& operator()(NonCopyable&&) const volatile && { return std::move(data); } 67 68 int data; 69 private: 70 TestClass(TestClass const&) = delete; 71 TestClass& operator=(TestClass const&) = delete; 72 }; 73 74 struct DerivedFromTestClass : public TestClass { 75 explicit DerivedFromTestClass(int x) : TestClass(x) {} 76 }; 77 78 int& foo(NonCopyable&&) { 79 static int data = 42; 80 return data; 81 } 82 83 template <class Signature, class Expect, class Functor> 84 void test_b12(Functor&& f) { 85 // Create the callable object. 86 typedef Signature TestClass::*ClassFunc; 87 ClassFunc func_ptr = &TestClass::operator(); 88 89 // Create the dummy arg. 90 NonCopyable arg; 91 92 // Check that the deduced return type of invoke is what is expected. 93 typedef decltype( 94 std::invoke(func_ptr, std::forward<Functor>(f), std::move(arg)) 95 ) DeducedReturnType; 96 static_assert((std::is_same<DeducedReturnType, Expect>::value), ""); 97 98 // Check that result_of_t matches Expect. 99 typedef typename std::result_of<ClassFunc&&(Functor&&, NonCopyable&&)>::type 100 ResultOfReturnType; 101 static_assert((std::is_same<ResultOfReturnType, Expect>::value), ""); 102 103 // Run invoke and check the return value. 104 DeducedReturnType ret = 105 std::invoke(func_ptr, std::forward<Functor>(f), std::move(arg)); 106 assert(ret == 42); 107 } 108 109 template <class Expect, class Functor> 110 void test_b34(Functor&& f) { 111 // Create the callable object. 112 typedef int TestClass::*ClassFunc; 113 ClassFunc func_ptr = &TestClass::data; 114 115 // Check that the deduced return type of invoke is what is expected. 116 typedef decltype( 117 std::invoke(func_ptr, std::forward<Functor>(f)) 118 ) DeducedReturnType; 119 static_assert((std::is_same<DeducedReturnType, Expect>::value), ""); 120 121 // Check that result_of_t matches Expect. 122 typedef typename std::result_of<ClassFunc&&(Functor&&)>::type 123 ResultOfReturnType; 124 static_assert((std::is_same<ResultOfReturnType, Expect>::value), ""); 125 126 // Run invoke and check the return value. 127 DeducedReturnType ret = 128 std::invoke(func_ptr, std::forward<Functor>(f)); 129 assert(ret == 42); 130 } 131 132 template <class Expect, class Functor> 133 void test_b5(Functor&& f) { 134 NonCopyable arg; 135 136 // Check that the deduced return type of invoke is what is expected. 137 typedef decltype( 138 std::invoke(std::forward<Functor>(f), std::move(arg)) 139 ) DeducedReturnType; 140 static_assert((std::is_same<DeducedReturnType, Expect>::value), ""); 141 142 // Check that result_of_t matches Expect. 143 typedef typename std::result_of<Functor&&(NonCopyable&&)>::type 144 ResultOfReturnType; 145 static_assert((std::is_same<ResultOfReturnType, Expect>::value), ""); 146 147 // Run invoke and check the return value. 148 DeducedReturnType ret = std::invoke(std::forward<Functor>(f), std::move(arg)); 149 assert(ret == 42); 150 } 151 152 void bullet_one_two_tests() { 153 { 154 TestClass cl(42); 155 test_b12<int&(NonCopyable&&) &, int&>(cl); 156 test_b12<int const&(NonCopyable&&) const &, int const&>(cl); 157 test_b12<int volatile&(NonCopyable&&) volatile &, int volatile&>(cl); 158 test_b12<int const volatile&(NonCopyable&&) const volatile &, int const volatile&>(cl); 159 160 test_b12<int&&(NonCopyable&&) &&, int&&>(std::move(cl)); 161 test_b12<int const&&(NonCopyable&&) const &&, int const&&>(std::move(cl)); 162 test_b12<int volatile&&(NonCopyable&&) volatile &&, int volatile&&>(std::move(cl)); 163 test_b12<int const volatile&&(NonCopyable&&) const volatile &&, int const volatile&&>(std::move(cl)); 164 } 165 { 166 DerivedFromTestClass cl(42); 167 test_b12<int&(NonCopyable&&) &, int&>(cl); 168 test_b12<int const&(NonCopyable&&) const &, int const&>(cl); 169 test_b12<int volatile&(NonCopyable&&) volatile &, int volatile&>(cl); 170 test_b12<int const volatile&(NonCopyable&&) const volatile &, int const volatile&>(cl); 171 172 test_b12<int&&(NonCopyable&&) &&, int&&>(std::move(cl)); 173 test_b12<int const&&(NonCopyable&&) const &&, int const&&>(std::move(cl)); 174 test_b12<int volatile&&(NonCopyable&&) volatile &&, int volatile&&>(std::move(cl)); 175 test_b12<int const volatile&&(NonCopyable&&) const volatile &&, int const volatile&&>(std::move(cl)); 176 } 177 { 178 TestClass cl_obj(42); 179 std::reference_wrapper<TestClass> cl(cl_obj); 180 test_b12<int&(NonCopyable&&) &, int&>(cl); 181 test_b12<int const&(NonCopyable&&) const &, int const&>(cl); 182 test_b12<int volatile&(NonCopyable&&) volatile &, int volatile&>(cl); 183 test_b12<int const volatile&(NonCopyable&&) const volatile &, int const volatile&>(cl); 184 185 test_b12<int&(NonCopyable&&) &, int&>(std::move(cl)); 186 test_b12<int const&(NonCopyable&&) const &, int const&>(std::move(cl)); 187 test_b12<int volatile&(NonCopyable&&) volatile &, int volatile&>(std::move(cl)); 188 test_b12<int const volatile&(NonCopyable&&) const volatile &, int const volatile&>(std::move(cl)); 189 } 190 { 191 DerivedFromTestClass cl_obj(42); 192 std::reference_wrapper<DerivedFromTestClass> cl(cl_obj); 193 test_b12<int&(NonCopyable&&) &, int&>(cl); 194 test_b12<int const&(NonCopyable&&) const &, int const&>(cl); 195 test_b12<int volatile&(NonCopyable&&) volatile &, int volatile&>(cl); 196 test_b12<int const volatile&(NonCopyable&&) const volatile &, int const volatile&>(cl); 197 198 test_b12<int&(NonCopyable&&) &, int&>(std::move(cl)); 199 test_b12<int const&(NonCopyable&&) const &, int const&>(std::move(cl)); 200 test_b12<int volatile&(NonCopyable&&) volatile &, int volatile&>(std::move(cl)); 201 test_b12<int const volatile&(NonCopyable&&) const volatile &, int const volatile&>(std::move(cl)); 202 } 203 { 204 TestClass cl_obj(42); 205 TestClass *cl = &cl_obj; 206 test_b12<int&(NonCopyable&&) &, int&>(cl); 207 test_b12<int const&(NonCopyable&&) const &, int const&>(cl); 208 test_b12<int volatile&(NonCopyable&&) volatile &, int volatile&>(cl); 209 test_b12<int const volatile&(NonCopyable&&) const volatile &, int const volatile&>(cl); 210 } 211 { 212 DerivedFromTestClass cl_obj(42); 213 DerivedFromTestClass *cl = &cl_obj; 214 test_b12<int&(NonCopyable&&) &, int&>(cl); 215 test_b12<int const&(NonCopyable&&) const &, int const&>(cl); 216 test_b12<int volatile&(NonCopyable&&) volatile &, int volatile&>(cl); 217 test_b12<int const volatile&(NonCopyable&&) const volatile &, int const volatile&>(cl); 218 } 219 } 220 221 void bullet_three_four_tests() { 222 { 223 typedef TestClass Fn; 224 Fn cl(42); 225 test_b34<int&>(cl); 226 test_b34<int const&>(static_cast<Fn const&>(cl)); 227 test_b34<int volatile&>(static_cast<Fn volatile&>(cl)); 228 test_b34<int const volatile&>(static_cast<Fn const volatile &>(cl)); 229 230 test_b34<int&&>(static_cast<Fn &&>(cl)); 231 test_b34<int const&&>(static_cast<Fn const&&>(cl)); 232 test_b34<int volatile&&>(static_cast<Fn volatile&&>(cl)); 233 test_b34<int const volatile&&>(static_cast<Fn const volatile&&>(cl)); 234 } 235 { 236 typedef DerivedFromTestClass Fn; 237 Fn cl(42); 238 test_b34<int&>(cl); 239 test_b34<int const&>(static_cast<Fn const&>(cl)); 240 test_b34<int volatile&>(static_cast<Fn volatile&>(cl)); 241 test_b34<int const volatile&>(static_cast<Fn const volatile &>(cl)); 242 243 test_b34<int&&>(static_cast<Fn &&>(cl)); 244 test_b34<int const&&>(static_cast<Fn const&&>(cl)); 245 test_b34<int volatile&&>(static_cast<Fn volatile&&>(cl)); 246 test_b34<int const volatile&&>(static_cast<Fn const volatile&&>(cl)); 247 } 248 { 249 typedef TestClass Fn; 250 Fn cl(42); 251 test_b34<int&>(std::reference_wrapper<Fn>(cl)); 252 test_b34<int const&>(std::reference_wrapper<Fn const>(cl)); 253 test_b34<int volatile&>(std::reference_wrapper<Fn volatile>(cl)); 254 test_b34<int const volatile&>(std::reference_wrapper<Fn const volatile>(cl)); 255 } 256 { 257 typedef DerivedFromTestClass Fn; 258 Fn cl(42); 259 test_b34<int&>(std::reference_wrapper<Fn>(cl)); 260 test_b34<int const&>(std::reference_wrapper<Fn const>(cl)); 261 test_b34<int volatile&>(std::reference_wrapper<Fn volatile>(cl)); 262 test_b34<int const volatile&>(std::reference_wrapper<Fn const volatile>(cl)); 263 } 264 { 265 typedef TestClass Fn; 266 Fn cl_obj(42); 267 Fn* cl = &cl_obj; 268 test_b34<int&>(cl); 269 test_b34<int const&>(static_cast<Fn const*>(cl)); 270 test_b34<int volatile&>(static_cast<Fn volatile*>(cl)); 271 test_b34<int const volatile&>(static_cast<Fn const volatile *>(cl)); 272 } 273 { 274 typedef DerivedFromTestClass Fn; 275 Fn cl_obj(42); 276 Fn* cl = &cl_obj; 277 test_b34<int&>(cl); 278 test_b34<int const&>(static_cast<Fn const*>(cl)); 279 test_b34<int volatile&>(static_cast<Fn volatile*>(cl)); 280 test_b34<int const volatile&>(static_cast<Fn const volatile *>(cl)); 281 } 282 } 283 284 void bullet_five_tests() { 285 using FooType = int&(NonCopyable&&); 286 { 287 FooType& fn = foo; 288 test_b5<int &>(fn); 289 } 290 { 291 FooType* fn = foo; 292 test_b5<int &>(fn); 293 } 294 { 295 typedef TestClass Fn; 296 Fn cl(42); 297 test_b5<int&>(cl); 298 test_b5<int const&>(static_cast<Fn const&>(cl)); 299 test_b5<int volatile&>(static_cast<Fn volatile&>(cl)); 300 test_b5<int const volatile&>(static_cast<Fn const volatile &>(cl)); 301 302 test_b5<int&&>(static_cast<Fn &&>(cl)); 303 test_b5<int const&&>(static_cast<Fn const&&>(cl)); 304 test_b5<int volatile&&>(static_cast<Fn volatile&&>(cl)); 305 test_b5<int const volatile&&>(static_cast<Fn const volatile&&>(cl)); 306 } 307 } 308 309 struct CopyThrows { 310 CopyThrows() {} 311 CopyThrows(CopyThrows const&) {} 312 CopyThrows(CopyThrows&&) noexcept {} 313 }; 314 315 struct NoThrowCallable { 316 void operator()() noexcept {} 317 void operator()(CopyThrows) noexcept {} 318 }; 319 320 struct ThrowsCallable { 321 void operator()() {} 322 }; 323 324 struct MemberObj { 325 int x; 326 }; 327 328 void noexcept_test() { 329 { 330 NoThrowCallable obj; ((void)obj); // suppress unused warning 331 CopyThrows arg; ((void)arg); // suppress unused warning 332 static_assert(noexcept(std::invoke(obj)), ""); 333 static_assert(!noexcept(std::invoke(obj, arg)), ""); 334 static_assert(noexcept(std::invoke(obj, std::move(arg))), ""); 335 } 336 { 337 ThrowsCallable obj; ((void)obj); // suppress unused warning 338 static_assert(!noexcept(std::invoke(obj)), ""); 339 } 340 { 341 MemberObj obj{42}; ((void)obj); // suppress unused warning. 342 static_assert(noexcept(std::invoke(&MemberObj::x, obj)), ""); 343 } 344 } 345 346 int main(int, char**) { 347 bullet_one_two_tests(); 348 bullet_three_four_tests(); 349 bullet_five_tests(); 350 noexcept_test(); 351 352 return 0; 353 } 354