invoke.pass.cpp 13 KB

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