invoke_rvalue.pass.cpp 5.6 KB

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