apply.pass.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // <tuple>
  11. // template <class F, class T> constexpr decltype(auto) apply(F &&, T &&)
  12. // Test with different ref/ptr/cv qualified argument types.
  13. #include <tuple>
  14. #include <array>
  15. #include <utility>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #include "type_id.h"
  19. // std::array is explicitly allowed to be initialized with A a = { init-list };.
  20. // Disable the missing braces warning for this reason.
  21. #include "disable_missing_braces_warning.h"
  22. constexpr int constexpr_sum_fn() { return 0; }
  23. template <class ...Ints>
  24. constexpr int constexpr_sum_fn(int x1, Ints... rest) { return x1 + constexpr_sum_fn(rest...); }
  25. struct ConstexprSumT {
  26. constexpr ConstexprSumT() = default;
  27. template <class ...Ints>
  28. constexpr int operator()(Ints... values) const {
  29. return constexpr_sum_fn(values...);
  30. }
  31. };
  32. void test_constexpr_evaluation()
  33. {
  34. constexpr ConstexprSumT sum_obj{};
  35. {
  36. using Tup = std::tuple<>;
  37. using Fn = int(&)();
  38. constexpr Tup t;
  39. static_assert(std::apply(static_cast<Fn>(constexpr_sum_fn), t) == 0, "");
  40. static_assert(std::apply(sum_obj, t) == 0, "");
  41. }
  42. {
  43. using Tup = std::tuple<int>;
  44. using Fn = int(&)(int);
  45. constexpr Tup t(42);
  46. static_assert(std::apply(static_cast<Fn>(constexpr_sum_fn), t) == 42, "");
  47. static_assert(std::apply(sum_obj, t) == 42, "");
  48. }
  49. {
  50. using Tup = std::tuple<int, long>;
  51. using Fn = int(&)(int, int);
  52. constexpr Tup t(42, 101);
  53. static_assert(std::apply(static_cast<Fn>(constexpr_sum_fn), t) == 143, "");
  54. static_assert(std::apply(sum_obj, t) == 143, "");
  55. }
  56. {
  57. using Tup = std::pair<int, long>;
  58. using Fn = int(&)(int, int);
  59. constexpr Tup t(42, 101);
  60. static_assert(std::apply(static_cast<Fn>(constexpr_sum_fn), t) == 143, "");
  61. static_assert(std::apply(sum_obj, t) == 143, "");
  62. }
  63. {
  64. using Tup = std::tuple<int, long, int>;
  65. using Fn = int(&)(int, int, int);
  66. constexpr Tup t(42, 101, -1);
  67. static_assert(std::apply(static_cast<Fn>(constexpr_sum_fn), t) == 142, "");
  68. static_assert(std::apply(sum_obj, t) == 142, "");
  69. }
  70. {
  71. using Tup = std::array<int, 3>;
  72. using Fn = int(&)(int, int, int);
  73. constexpr Tup t = {42, 101, -1};
  74. static_assert(std::apply(static_cast<Fn>(constexpr_sum_fn), t) == 142, "");
  75. static_assert(std::apply(sum_obj, t) == 142, "");
  76. }
  77. }
  78. enum CallQuals {
  79. CQ_None,
  80. CQ_LValue,
  81. CQ_ConstLValue,
  82. CQ_RValue,
  83. CQ_ConstRValue
  84. };
  85. template <class Tuple>
  86. struct CallInfo {
  87. CallQuals quals;
  88. TypeID const* arg_types;
  89. Tuple args;
  90. template <class ...Args>
  91. CallInfo(CallQuals q, Args&&... xargs)
  92. : quals(q), arg_types(&makeArgumentID<Args&&...>()), args(std::forward<Args>(xargs)...)
  93. {}
  94. };
  95. template <class ...Args>
  96. inline CallInfo<decltype(std::forward_as_tuple(std::declval<Args>()...))>
  97. makeCallInfo(CallQuals quals, Args&&... args) {
  98. return {quals, std::forward<Args>(args)...};
  99. }
  100. struct TrackedCallable {
  101. TrackedCallable() = default;
  102. template <class ...Args> auto operator()(Args&&... xargs) &
  103. { return makeCallInfo(CQ_LValue, std::forward<Args>(xargs)...); }
  104. template <class ...Args> auto operator()(Args&&... xargs) const&
  105. { return makeCallInfo(CQ_ConstLValue, std::forward<Args>(xargs)...); }
  106. template <class ...Args> auto operator()(Args&&... xargs) &&
  107. { return makeCallInfo(CQ_RValue, std::forward<Args>(xargs)...); }
  108. template <class ...Args> auto operator()(Args&&... xargs) const&&
  109. { return makeCallInfo(CQ_ConstRValue, std::forward<Args>(xargs)...); }
  110. };
  111. template <class ...ExpectArgs, class Tuple>
  112. void check_apply_quals_and_types(Tuple&& t) {
  113. TypeID const* const expect_args = &makeArgumentID<ExpectArgs...>();
  114. TrackedCallable obj;
  115. TrackedCallable const& cobj = obj;
  116. {
  117. auto ret = std::apply(obj, std::forward<Tuple>(t));
  118. assert(ret.quals == CQ_LValue);
  119. assert(ret.arg_types == expect_args);
  120. assert(ret.args == t);
  121. }
  122. {
  123. auto ret = std::apply(cobj, std::forward<Tuple>(t));
  124. assert(ret.quals == CQ_ConstLValue);
  125. assert(ret.arg_types == expect_args);
  126. assert(ret.args == t);
  127. }
  128. {
  129. auto ret = std::apply(std::move(obj), std::forward<Tuple>(t));
  130. assert(ret.quals == CQ_RValue);
  131. assert(ret.arg_types == expect_args);
  132. assert(ret.args == t);
  133. }
  134. {
  135. auto ret = std::apply(std::move(cobj), std::forward<Tuple>(t));
  136. assert(ret.quals == CQ_ConstRValue);
  137. assert(ret.arg_types == expect_args);
  138. assert(ret.args == t);
  139. }
  140. }
  141. void test_call_quals_and_arg_types()
  142. {
  143. TrackedCallable obj;
  144. using Tup = std::tuple<int, int const&, unsigned&&>;
  145. const int x = 42;
  146. unsigned y = 101;
  147. Tup t(-1, x, std::move(y));
  148. Tup const& ct = t;
  149. check_apply_quals_and_types<int&, int const&, unsigned&>(t);
  150. check_apply_quals_and_types<int const&, int const&, unsigned&>(ct);
  151. check_apply_quals_and_types<int&&, int const&, unsigned&&>(std::move(t));
  152. check_apply_quals_and_types<int const&&, int const&, unsigned&&>(std::move(ct));
  153. }
  154. struct NothrowMoveable {
  155. NothrowMoveable() noexcept = default;
  156. NothrowMoveable(NothrowMoveable const&) noexcept(false) {}
  157. NothrowMoveable(NothrowMoveable&&) noexcept {}
  158. };
  159. template <bool IsNoexcept>
  160. struct TestNoexceptCallable {
  161. template <class ...Args>
  162. NothrowMoveable operator()(Args...) const noexcept(IsNoexcept) { return {}; }
  163. };
  164. void test_noexcept()
  165. {
  166. TestNoexceptCallable<true> nec;
  167. TestNoexceptCallable<false> tc;
  168. {
  169. // test that the functions noexcept-ness is propagated
  170. using Tup = std::tuple<int, const char*, long>;
  171. Tup t;
  172. ASSERT_NOEXCEPT(std::apply(nec, t));
  173. ASSERT_NOT_NOEXCEPT(std::apply(tc, t));
  174. }
  175. {
  176. // test that the noexcept-ness of the argument conversions is checked.
  177. using Tup = std::tuple<NothrowMoveable, int>;
  178. Tup t;
  179. ASSERT_NOT_NOEXCEPT(std::apply(nec, t));
  180. ASSERT_NOEXCEPT(std::apply(nec, std::move(t)));
  181. }
  182. }
  183. namespace ReturnTypeTest {
  184. static int my_int = 42;
  185. template <int N> struct index {};
  186. void f(index<0>) {}
  187. int f(index<1>) { return 0; }
  188. int & f(index<2>) { return static_cast<int &>(my_int); }
  189. int const & f(index<3>) { return static_cast<int const &>(my_int); }
  190. int volatile & f(index<4>) { return static_cast<int volatile &>(my_int); }
  191. int const volatile & f(index<5>) { return static_cast<int const volatile &>(my_int); }
  192. int && f(index<6>) { return static_cast<int &&>(my_int); }
  193. int const && f(index<7>) { return static_cast<int const &&>(my_int); }
  194. int volatile && f(index<8>) { return static_cast<int volatile &&>(my_int); }
  195. int const volatile && f(index<9>) { return static_cast<int const volatile &&>(my_int); }
  196. int * f(index<10>) { return static_cast<int *>(&my_int); }
  197. int const * f(index<11>) { return static_cast<int const *>(&my_int); }
  198. int volatile * f(index<12>) { return static_cast<int volatile *>(&my_int); }
  199. int const volatile * f(index<13>) { return static_cast<int const volatile *>(&my_int); }
  200. template <int Func, class Expect>
  201. void test()
  202. {
  203. using RawInvokeResult = decltype(f(index<Func>{}));
  204. static_assert(std::is_same<RawInvokeResult, Expect>::value, "");
  205. using FnType = RawInvokeResult (*) (index<Func>);
  206. FnType fn = f;
  207. std::tuple<index<Func>> t; ((void)t);
  208. using InvokeResult = decltype(std::apply(fn, t));
  209. static_assert(std::is_same<InvokeResult, Expect>::value, "");
  210. }
  211. } // end namespace ReturnTypeTest
  212. void test_return_type()
  213. {
  214. using ReturnTypeTest::test;
  215. test<0, void>();
  216. test<1, int>();
  217. test<2, int &>();
  218. test<3, int const &>();
  219. test<4, int volatile &>();
  220. test<5, int const volatile &>();
  221. test<6, int &&>();
  222. test<7, int const &&>();
  223. test<8, int volatile &&>();
  224. test<9, int const volatile &&>();
  225. test<10, int *>();
  226. test<11, int const *>();
  227. test<12, int volatile *>();
  228. test<13, int const volatile *>();
  229. }
  230. int main() {
  231. test_constexpr_evaluation();
  232. test_call_quals_and_arg_types();
  233. test_return_type();
  234. test_noexcept();
  235. }