invoke_rvalue.pass.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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<CopyConstructible Fn, CopyConstructible... Types>
  11. // unspecified bind(Fn, Types...);
  12. // template<Returnable R, CopyConstructible Fn, CopyConstructible... Types>
  13. // unspecified bind(Fn, Types...);
  14. #include <stdio.h>
  15. #include <functional>
  16. #include <cassert>
  17. int count = 0;
  18. // 1 arg, return void
  19. void f_void_1(int i)
  20. {
  21. count += i;
  22. }
  23. struct A_void_1
  24. {
  25. void operator()(int i)
  26. {
  27. count += i;
  28. }
  29. void mem1() {++count;}
  30. void mem2() const {count += 2;}
  31. };
  32. void
  33. test_void_1()
  34. {
  35. using namespace std::placeholders;
  36. int save_count = count;
  37. // function
  38. {
  39. std::bind(f_void_1, _1)(2);
  40. assert(count == save_count + 2);
  41. save_count = count;
  42. }
  43. {
  44. std::bind(f_void_1, 2)();
  45. assert(count == save_count + 2);
  46. save_count = count;
  47. }
  48. // function pointer
  49. {
  50. void (*fp)(int) = f_void_1;
  51. std::bind(fp, _1)(3);
  52. assert(count == save_count+3);
  53. save_count = count;
  54. }
  55. {
  56. void (*fp)(int) = f_void_1;
  57. std::bind(fp, 3)();
  58. assert(count == save_count+3);
  59. save_count = count;
  60. }
  61. // functor
  62. {
  63. A_void_1 a0;
  64. std::bind(a0, _1)(4);
  65. assert(count == save_count+4);
  66. save_count = count;
  67. }
  68. {
  69. A_void_1 a0;
  70. std::bind(a0, 4)();
  71. assert(count == save_count+4);
  72. save_count = count;
  73. }
  74. // member function pointer
  75. {
  76. void (A_void_1::*fp)() = &A_void_1::mem1;
  77. std::bind(fp, _1)(A_void_1());
  78. assert(count == save_count+1);
  79. save_count = count;
  80. A_void_1 a;
  81. std::bind(fp, _1)(&a);
  82. assert(count == save_count+1);
  83. save_count = count;
  84. }
  85. {
  86. void (A_void_1::*fp)() = &A_void_1::mem1;
  87. std::bind(fp, A_void_1())();
  88. assert(count == save_count+1);
  89. save_count = count;
  90. A_void_1 a;
  91. std::bind(fp, &a)();
  92. assert(count == save_count+1);
  93. save_count = count;
  94. }
  95. // const member function pointer
  96. {
  97. void (A_void_1::*fp)() const = &A_void_1::mem2;
  98. std::bind(fp, _1)(A_void_1());
  99. assert(count == save_count+2);
  100. save_count = count;
  101. A_void_1 a;
  102. std::bind(fp, _1)(&a);
  103. assert(count == save_count+2);
  104. save_count = count;
  105. }
  106. {
  107. void (A_void_1::*fp)() const = &A_void_1::mem2;
  108. std::bind(fp, A_void_1())();
  109. assert(count == save_count+2);
  110. save_count = count;
  111. A_void_1 a;
  112. std::bind(fp, &a)();
  113. assert(count == save_count+2);
  114. save_count = count;
  115. }
  116. }
  117. // 1 arg, return int
  118. int f_int_1(int i)
  119. {
  120. return i + 1;
  121. }
  122. struct A_int_1
  123. {
  124. A_int_1() : data_(5) {}
  125. int operator()(int i)
  126. {
  127. return i - 1;
  128. }
  129. int mem1() {return 3;}
  130. int mem2() const {return 4;}
  131. int data_;
  132. };
  133. void
  134. test_int_1()
  135. {
  136. using namespace std::placeholders;
  137. // function
  138. {
  139. assert(std::bind(f_int_1, _1)(2) == 3);
  140. assert(std::bind(f_int_1, 2)() == 3);
  141. }
  142. // function pointer
  143. {
  144. int (*fp)(int) = f_int_1;
  145. assert(std::bind(fp, _1)(3) == 4);
  146. assert(std::bind(fp, 3)() == 4);
  147. }
  148. // functor
  149. {
  150. assert(std::bind(A_int_1(), _1)(4) == 3);
  151. assert(std::bind(A_int_1(), 4)() == 3);
  152. }
  153. // member function pointer
  154. {
  155. assert(std::bind(&A_int_1::mem1, _1)(A_int_1()) == 3);
  156. assert(std::bind(&A_int_1::mem1, A_int_1())() == 3);
  157. A_int_1 a;
  158. assert(std::bind(&A_int_1::mem1, _1)(&a) == 3);
  159. assert(std::bind(&A_int_1::mem1, &a)() == 3);
  160. }
  161. // const member function pointer
  162. {
  163. assert(std::bind(&A_int_1::mem2, _1)(A_int_1()) == 4);
  164. assert(std::bind(&A_int_1::mem2, A_int_1())() == 4);
  165. A_int_1 a;
  166. assert(std::bind(&A_int_1::mem2, _1)(&a) == 4);
  167. assert(std::bind(&A_int_1::mem2, &a)() == 4);
  168. }
  169. // member data pointer
  170. {
  171. assert(std::bind(&A_int_1::data_, _1)(A_int_1()) == 5);
  172. assert(std::bind(&A_int_1::data_, A_int_1())() == 5);
  173. A_int_1 a;
  174. assert(std::bind(&A_int_1::data_, _1)(a) == 5);
  175. std::bind(&A_int_1::data_, _1)(a) = 6;
  176. assert(std::bind(&A_int_1::data_, _1)(a) == 6);
  177. assert(std::bind(&A_int_1::data_, _1)(&a) == 6);
  178. std::bind(&A_int_1::data_, _1)(&a) = 7;
  179. assert(std::bind(&A_int_1::data_, _1)(&a) == 7);
  180. }
  181. }
  182. // 2 arg, return void
  183. void f_void_2(int i, int j)
  184. {
  185. count += i+j;
  186. }
  187. struct A_void_2
  188. {
  189. void operator()(int i, int j)
  190. {
  191. count += i+j;
  192. }
  193. void mem1(int i) {count += i;}
  194. void mem2(int i) const {count += i;}
  195. };
  196. void
  197. test_void_2()
  198. {
  199. using namespace std::placeholders;
  200. int save_count = count;
  201. // function
  202. {
  203. std::bind(f_void_2, _1, _2)(2, 3);
  204. assert(count == save_count+5);
  205. save_count = count;
  206. std::bind(f_void_2, 2, _1)(3);
  207. assert(count == save_count+5);
  208. save_count = count;
  209. std::bind(f_void_2, 2, 3)();
  210. assert(count == save_count+5);
  211. save_count = count;
  212. }
  213. // member function pointer
  214. {
  215. std::bind(&A_void_2::mem1, _1, _2)(A_void_2(), 3);
  216. assert(count == save_count+3);
  217. save_count = count;
  218. std::bind(&A_void_2::mem1, _2, _1)(3, A_void_2());
  219. assert(count == save_count+3);
  220. save_count = count;
  221. }
  222. }
  223. int f_nested(int i)
  224. {
  225. return i+1;
  226. }
  227. int g_nested(int i)
  228. {
  229. return i*10;
  230. }
  231. void test_nested()
  232. {
  233. using namespace std::placeholders;
  234. assert(std::bind(f_nested, std::bind(g_nested, _1))(3) == 31);
  235. }
  236. int main()
  237. {
  238. test_void_1();
  239. test_int_1();
  240. test_void_2();
  241. test_nested();
  242. }