F_nullptr.pass.cpp 8.9 KB

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