alloc_F.pass.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // template<class F, class A> function(allocator_arg_t, const A&, F);
  12. #include <functional>
  13. #include <cassert>
  14. #include "test_allocator.h"
  15. class A
  16. {
  17. int data_[10];
  18. public:
  19. static int count;
  20. A()
  21. {
  22. ++count;
  23. for (int i = 0; i < 10; ++i)
  24. data_[i] = i;
  25. }
  26. A(const A&) {++count;}
  27. ~A() {--count;}
  28. int operator()(int i) const
  29. {
  30. for (int j = 0; j < 10; ++j)
  31. i += data_[j];
  32. return i;
  33. }
  34. int foo(int) const {return 1;}
  35. };
  36. int A::count = 0;
  37. int g(int) {return 0;}
  38. class Foo {
  39. public:
  40. void bar(int k) { }
  41. };
  42. int main()
  43. {
  44. {
  45. std::function<int(int)> f(std::allocator_arg, test_allocator<A>(), A());
  46. assert(A::count == 1);
  47. assert(f.target<A>());
  48. assert(f.target<int(*)(int)>() == 0);
  49. }
  50. assert(A::count == 0);
  51. {
  52. std::function<int(int)> f(std::allocator_arg, test_allocator<int(*)(int)>(), g);
  53. assert(f.target<int(*)(int)>());
  54. assert(f.target<A>() == 0);
  55. }
  56. {
  57. std::function<int(int)> f(std::allocator_arg, test_allocator<int(*)(int)>(),
  58. (int (*)(int))0);
  59. assert(!f);
  60. assert(f.target<int(*)(int)>() == 0);
  61. assert(f.target<A>() == 0);
  62. }
  63. {
  64. std::function<int(const A*, int)> f(std::allocator_arg,
  65. test_allocator<int(A::*)(int)const>(),
  66. &A::foo);
  67. assert(f);
  68. assert(f.target<int (A::*)(int) const>() != 0);
  69. }
  70. #if __cplusplus >= 201103L
  71. {
  72. Foo f;
  73. std::function<void(int)> fun = std::bind(&Foo::bar, &f, std::placeholders::_1);
  74. fun(10);
  75. }
  76. #endif
  77. {
  78. std::function<void(int)> fun(std::allocator_arg,
  79. test_allocator<int(*)(int)>(),
  80. &g);
  81. assert(fun);
  82. assert(fun.target<int(*)(int)>() != 0);
  83. fun(10);
  84. }
  85. }