alloc_F.pass.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. 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. int main()
  39. {
  40. {
  41. std::function<int(int)> f(std::allocator_arg, test_allocator<A>(), A());
  42. assert(A::count == 1);
  43. assert(f.target<A>());
  44. assert(f.target<int(*)(int)>() == 0);
  45. }
  46. assert(A::count == 0);
  47. {
  48. std::function<int(int)> f(std::allocator_arg, test_allocator<int(*)(int)>(), g);
  49. assert(f.target<int(*)(int)>());
  50. assert(f.target<A>() == 0);
  51. }
  52. {
  53. std::function<int(int)> f(std::allocator_arg, test_allocator<int(*)(int)>(),
  54. (int (*)(int))0);
  55. assert(!f);
  56. assert(f.target<int(*)(int)>() == 0);
  57. assert(f.target<A>() == 0);
  58. }
  59. {
  60. std::function<int(const A*, int)> f(std::allocator_arg,
  61. test_allocator<int(A::*)(int)const>(),
  62. &A::foo);
  63. assert(f);
  64. assert(f.target<int (A::*)(int) const>() != 0);
  65. }
  66. }