deduct_F.pass.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // <functional>
  10. // template<class F>
  11. // function(F) -> function<see-below>;
  12. // UNSUPPORTED: c++98, c++03, c++11, c++14
  13. // UNSUPPORTED: libcpp-no-deduction-guides
  14. #include <functional>
  15. #include <type_traits>
  16. #include <utility>
  17. #include "test_macros.h"
  18. struct R { };
  19. struct A1 { };
  20. struct A2 { };
  21. struct A3 { };
  22. #define DECLARE_FUNCTIONS_WITH_QUALS(N, ...) \
  23. struct f0_##N { R operator()() __VA_ARGS__ { return {}; } }; \
  24. struct f1_##N { R operator()(A1) __VA_ARGS__ { return {}; } }; \
  25. struct f2_##N { R operator()(A1, A2) __VA_ARGS__ { return {}; } }; \
  26. struct f3_##N { R operator()(A1, A2, A3) __VA_ARGS__ { return {}; } } \
  27. /**/
  28. DECLARE_FUNCTIONS_WITH_QUALS(0, /* nothing */);
  29. DECLARE_FUNCTIONS_WITH_QUALS(1, const);
  30. DECLARE_FUNCTIONS_WITH_QUALS(2, volatile);
  31. DECLARE_FUNCTIONS_WITH_QUALS(3, const volatile);
  32. DECLARE_FUNCTIONS_WITH_QUALS(4, &);
  33. DECLARE_FUNCTIONS_WITH_QUALS(5 , const &);
  34. DECLARE_FUNCTIONS_WITH_QUALS(6 , volatile &);
  35. DECLARE_FUNCTIONS_WITH_QUALS(7 , const volatile &);
  36. DECLARE_FUNCTIONS_WITH_QUALS(8 , noexcept);
  37. DECLARE_FUNCTIONS_WITH_QUALS(9 , const noexcept);
  38. DECLARE_FUNCTIONS_WITH_QUALS(10, volatile noexcept);
  39. DECLARE_FUNCTIONS_WITH_QUALS(11, const volatile noexcept);
  40. DECLARE_FUNCTIONS_WITH_QUALS(12, & noexcept);
  41. DECLARE_FUNCTIONS_WITH_QUALS(13, const & noexcept);
  42. DECLARE_FUNCTIONS_WITH_QUALS(14, volatile & noexcept);
  43. DECLARE_FUNCTIONS_WITH_QUALS(15, const volatile & noexcept);
  44. int main() {
  45. #define CHECK_FUNCTIONS(N) \
  46. do { \
  47. /* implicit */ \
  48. std::function g0 = f0_##N{}; \
  49. ASSERT_SAME_TYPE(decltype(g0), std::function<R()>); \
  50. \
  51. std::function g1 = f1_##N{}; \
  52. ASSERT_SAME_TYPE(decltype(g1), std::function<R(A1)>); \
  53. \
  54. std::function g2 = f2_##N{}; \
  55. ASSERT_SAME_TYPE(decltype(g2), std::function<R(A1, A2)>); \
  56. \
  57. std::function g3 = f3_##N{}; \
  58. ASSERT_SAME_TYPE(decltype(g3), std::function<R(A1, A2, A3)>); \
  59. \
  60. /* explicit */ \
  61. std::function g4{f0_##N{}}; \
  62. ASSERT_SAME_TYPE(decltype(g4), std::function<R()>); \
  63. \
  64. std::function g5{f1_##N{}}; \
  65. ASSERT_SAME_TYPE(decltype(g5), std::function<R(A1)>); \
  66. \
  67. std::function g6{f2_##N{}}; \
  68. ASSERT_SAME_TYPE(decltype(g6), std::function<R(A1, A2)>); \
  69. \
  70. std::function g7{f3_##N{}}; \
  71. ASSERT_SAME_TYPE(decltype(g7), std::function<R(A1, A2, A3)>); \
  72. \
  73. /* from std::function */ \
  74. std::function<R(A1)> unary; \
  75. std::function g8 = unary; \
  76. ASSERT_SAME_TYPE(decltype(g8), std::function<R(A1)>); \
  77. \
  78. std::function g9 = std::move(unary); \
  79. ASSERT_SAME_TYPE(decltype(g9), std::function<R(A1)>); \
  80. \
  81. std::function<R(A1&&)> unary_ref; \
  82. std::function g10 = unary_ref; \
  83. ASSERT_SAME_TYPE(decltype(g10), std::function<R(A1&&)>); \
  84. \
  85. std::function g11 = std::move(unary_ref); \
  86. ASSERT_SAME_TYPE(decltype(g11), std::function<R(A1&&)>); \
  87. } while (false) \
  88. /**/
  89. // Make sure we can deduce from function objects with valid call operators
  90. CHECK_FUNCTIONS(0);
  91. CHECK_FUNCTIONS(1);
  92. CHECK_FUNCTIONS(2);
  93. CHECK_FUNCTIONS(3);
  94. CHECK_FUNCTIONS(4);
  95. CHECK_FUNCTIONS(5);
  96. CHECK_FUNCTIONS(6);
  97. CHECK_FUNCTIONS(7);
  98. CHECK_FUNCTIONS(8);
  99. CHECK_FUNCTIONS(9);
  100. CHECK_FUNCTIONS(10);
  101. CHECK_FUNCTIONS(11);
  102. CHECK_FUNCTIONS(12);
  103. CHECK_FUNCTIONS(13);
  104. CHECK_FUNCTIONS(14);
  105. CHECK_FUNCTIONS(15);
  106. }
  107. // Make sure we fail in a SFINAE-friendly manner when we try to deduce
  108. // from a type without a valid call operator.
  109. template <typename F, typename = decltype(std::function{std::declval<F>()})>
  110. constexpr bool can_deduce() { return true; }
  111. template <typename F>
  112. constexpr bool can_deduce(...) { return false; }
  113. struct invalid1 { };
  114. struct invalid2 {
  115. template <typename ...Args>
  116. void operator()(Args ...);
  117. };
  118. struct invalid3 {
  119. void operator()(int);
  120. void operator()(long);
  121. };
  122. static_assert(!can_deduce<invalid1>());
  123. static_assert(!can_deduce<invalid2>());
  124. static_assert(!can_deduce<invalid3>());