F_nullptr.pass.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // <functional>
  9. // class function<R(ArgTypes...)>
  10. // function(Fp);
  11. // Ensure that __not_null works for all function types.
  12. // See https://bugs.llvm.org/show_bug.cgi?id=23589
  13. //------------------------------------------------------------------------------
  14. // TESTING std::function<...>::__not_null(Callable)
  15. //
  16. // Concerns:
  17. // 1) The call __not_null(Callable) is well formed and correct for each
  18. // possible 'Callable' type category. These categories include:
  19. // 1a) function pointers
  20. // 1b) member function pointer
  21. // 1c) member data pointer
  22. // 1d) callable class type
  23. // 1e) lambdas
  24. // Categories 1a, 1b, and 1c are 'Nullable' types. Only objects of these
  25. // types can be null. The other categories are not tested here.
  26. // 3) '__not_null(Callable)' is well formed when the call signature includes
  27. // varargs.
  28. // 4) '__not_null(Callable)' works for Callable types with all arities less
  29. // than or equal to 3 in C++03.
  30. // 5) '__not_null(Callable)' works when 'Callable' is a member function
  31. // pointer to a cv or ref qualified function type.
  32. //
  33. // Plan:
  34. // 1 For categories 1a, 1b and 1c define a set of
  35. // 'Callable' objects for this category. This set should include examples
  36. // of arity 0, 1, 2 and possible 3 including versions with varargs as the
  37. // last parameter.
  38. //
  39. // 2 For each 'Callable' object in categories 1a, 1b and 1c do the following.
  40. //
  41. // 1 Define a type 'std::function<Sig>' as 'F' where 'Sig' is compatible with
  42. // the signature of the 'Callable' object.
  43. //
  44. // 2 Create an object of type 'F' using a null pointer of type 'Callable'.
  45. // Check that 'F.target<Callable>()' is null.
  46. //
  47. // 3 Create an object of type 'F' that is not null. Check that
  48. // 'F.target<Callable>()' is not null and is equal to the original
  49. // argument.
  50. #include <functional>
  51. #include <type_traits>
  52. #include <cassert>
  53. #include "test_macros.h"
  54. ///////////////////////////////////////////////////////////////////////////////
  55. int foo() { return 42; }
  56. int foo(int) { return 42; }
  57. int foo(int, int) { return 42; }
  58. int foo(int, int, int) { return 42; }
  59. int foo(...) { return 42; }
  60. int foo(int, ...) { return 42; }
  61. int foo(int, int, ...) { return 42; }
  62. int foo(int, int, int, ...) { return 42; }
  63. ///////////////////////////////////////////////////////////////////////////////
  64. struct MemFun03 {
  65. int foo() { return 42; }
  66. int foo() const { return 42; }
  67. int foo() volatile { return 42; }
  68. int foo() const volatile { return 42; }
  69. int foo(int) { return 42; }
  70. int foo(int) const { return 42; }
  71. int foo(int) volatile { return 42; }
  72. int foo(int) const volatile { return 42; }
  73. int foo(int, int) { return 42; }
  74. int foo(int, int) const { return 42; }
  75. int foo(int, int) volatile { return 42; }
  76. int foo(int, int) const volatile { return 42; }
  77. int foo(int, int, int) { return 42; }
  78. int foo(int, int, int) const { return 42; }
  79. int foo(int, int, int) volatile { return 42; }
  80. int foo(int, int, int) const volatile { return 42; }
  81. int foo(...) { return 42; }
  82. int foo(...) const { return 42; }
  83. int foo(...) volatile { return 42; }
  84. int foo(...) const volatile { return 42; }
  85. int foo(int, ...) { return 42; }
  86. int foo(int, ...) const { return 42; }
  87. int foo(int, ...) volatile { return 42; }
  88. int foo(int, ...) const volatile { return 42; }
  89. int foo(int, int, ...) { return 42; }
  90. int foo(int, int, ...) const { return 42; }
  91. int foo(int, int, ...) volatile { return 42; }
  92. int foo(int, int, ...) const volatile { return 42; }
  93. int foo(int, int, int, ...) { return 42; }
  94. int foo(int, int, int, ...) const { return 42; }
  95. int foo(int, int, int, ...) volatile { return 42; }
  96. int foo(int, int, int, ...) const volatile { return 42; }
  97. };
  98. #if TEST_STD_VER >= 11
  99. struct MemFun11 {
  100. int foo() & { return 42; }
  101. int foo() const & { return 42; }
  102. int foo() volatile & { return 42; }
  103. int foo() const volatile & { return 42; }
  104. int foo(...) & { return 42; }
  105. int foo(...) const & { return 42; }
  106. int foo(...) volatile & { return 42; }
  107. int foo(...) const volatile & { return 42; }
  108. int foo() && { return 42; }
  109. int foo() const && { return 42; }
  110. int foo() volatile && { return 42; }
  111. int foo() const volatile && { return 42; }
  112. int foo(...) && { return 42; }
  113. int foo(...) const && { return 42; }
  114. int foo(...) volatile && { return 42; }
  115. int foo(...) const volatile && { return 42; }
  116. };
  117. #endif // TEST_STD_VER >= 11
  118. struct MemData {
  119. int foo;
  120. };
  121. // Create a non-null free function by taking the address of
  122. // &static_cast<Tp&>(foo);
  123. template <class Tp>
  124. struct Creator {
  125. static Tp create() {
  126. return &foo;
  127. }
  128. };
  129. // Create a non-null member pointer.
  130. template <class Ret, class Class>
  131. struct Creator<Ret Class::*> {
  132. typedef Ret Class::*ReturnType;
  133. static ReturnType create() {
  134. return &Class::foo;
  135. }
  136. };
  137. template <class TestFn, class Fn>
  138. void test_imp() {
  139. { // Check that the null value is detected
  140. TestFn tf = nullptr;
  141. std::function<Fn> f = tf;
  142. assert(f.template target<TestFn>() == nullptr);
  143. }
  144. { // Check that the non-null value is detected.
  145. TestFn tf = Creator<TestFn>::create();
  146. assert(tf != nullptr);
  147. std::function<Fn> f = tf;
  148. assert(f.template target<TestFn>() != nullptr);
  149. assert(*f.template target<TestFn>() == tf);
  150. }
  151. }
  152. void test_func() {
  153. test_imp<int(*)(), int()>();
  154. test_imp<int(*)(...), int()>();
  155. test_imp<int(*)(int), int(int)>();
  156. test_imp<int(*)(int, ...), int(int)>();
  157. test_imp<int(*)(int, int), int(int, int)>();
  158. test_imp<int(*)(int, int, ...), int(int, int)>();
  159. test_imp<int(*)(int, int, int), int(int, int, int)>();
  160. test_imp<int(*)(int, int, int, ...), int(int, int, int)>();
  161. }
  162. void test_mf() {
  163. test_imp<int(MemFun03::*)(), int(MemFun03&)>();
  164. test_imp<int(MemFun03::*)(...), int(MemFun03&)>();
  165. test_imp<int(MemFun03::*)() const, int(MemFun03&)>();
  166. test_imp<int(MemFun03::*)(...) const, int(MemFun03&)>();
  167. test_imp<int(MemFun03::*)() volatile, int(MemFun03&)>();
  168. test_imp<int(MemFun03::*)(...) volatile, int(MemFun03&)>();
  169. test_imp<int(MemFun03::*)() const volatile, int(MemFun03&)>();
  170. test_imp<int(MemFun03::*)(...) const volatile, int(MemFun03&)>();
  171. test_imp<int(MemFun03::*)(int), int(MemFun03&, int)>();
  172. test_imp<int(MemFun03::*)(int, ...), int(MemFun03&, int)>();
  173. test_imp<int(MemFun03::*)(int) const, int(MemFun03&, int)>();
  174. test_imp<int(MemFun03::*)(int, ...) const, int(MemFun03&, int)>();
  175. test_imp<int(MemFun03::*)(int) volatile, int(MemFun03&, int)>();
  176. test_imp<int(MemFun03::*)(int, ...) volatile, int(MemFun03&, int)>();
  177. test_imp<int(MemFun03::*)(int) const volatile, int(MemFun03&, int)>();
  178. test_imp<int(MemFun03::*)(int, ...) const volatile, int(MemFun03&, int)>();
  179. test_imp<int(MemFun03::*)(int, int), int(MemFun03&, int, int)>();
  180. test_imp<int(MemFun03::*)(int, int, ...), int(MemFun03&, int, int)>();
  181. test_imp<int(MemFun03::*)(int, int) const, int(MemFun03&, int, int)>();
  182. test_imp<int(MemFun03::*)(int, int, ...) const, int(MemFun03&, int, int)>();
  183. test_imp<int(MemFun03::*)(int, int) volatile, int(MemFun03&, int, int)>();
  184. test_imp<int(MemFun03::*)(int, int, ...) volatile, int(MemFun03&, int, int)>();
  185. test_imp<int(MemFun03::*)(int, int) const volatile, int(MemFun03&, int, int)>();
  186. test_imp<int(MemFun03::*)(int, int, ...) const volatile, int(MemFun03&, int, int)>();
  187. #if TEST_STD_VER >= 11
  188. test_imp<int(MemFun11::*)() &, int(MemFun11&)>();
  189. test_imp<int(MemFun11::*)(...) &, int(MemFun11&)>();
  190. test_imp<int(MemFun11::*)() const &, int(MemFun11&)>();
  191. test_imp<int(MemFun11::*)(...) const &, int(MemFun11&)>();
  192. test_imp<int(MemFun11::*)() volatile &, int(MemFun11&)>();
  193. test_imp<int(MemFun11::*)(...) volatile &, int(MemFun11&)>();
  194. test_imp<int(MemFun11::*)() const volatile &, int(MemFun11&)>();
  195. test_imp<int(MemFun11::*)(...) const volatile &, int(MemFun11&)>();
  196. test_imp<int(MemFun11::*)() &&, int(MemFun11&&)>();
  197. test_imp<int(MemFun11::*)(...) &&, int(MemFun11&&)>();
  198. test_imp<int(MemFun11::*)() const &&, int(MemFun11&&)>();
  199. test_imp<int(MemFun11::*)(...) const &&, int(MemFun11&&)>();
  200. test_imp<int(MemFun11::*)() volatile &&, int(MemFun11&&)>();
  201. test_imp<int(MemFun11::*)(...) volatile &&, int(MemFun11&&)>();
  202. test_imp<int(MemFun11::*)() const volatile &&, int(MemFun11&&)>();
  203. test_imp<int(MemFun11::*)(...) const volatile &&, int(MemFun11&&)>();
  204. #endif
  205. }
  206. void test_md() {
  207. test_imp<int MemData::*, int(MemData&)>();
  208. }
  209. int main(int, char**) {
  210. test_func();
  211. test_mf();
  212. test_md();
  213. return 0;
  214. }