F.pass.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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(nullptr_t);
  12. #include <functional>
  13. #include <cassert>
  14. #include "count_new.hpp"
  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. assert(globalMemCounter.checkOutstandingNewEq(0));
  41. {
  42. std::function<int(int)> f = A();
  43. assert(A::count == 1);
  44. assert(globalMemCounter.checkOutstandingNewEq(1));
  45. assert(f.target<A>());
  46. assert(f.target<int(*)(int)>() == 0);
  47. }
  48. assert(A::count == 0);
  49. assert(globalMemCounter.checkOutstandingNewEq(0));
  50. {
  51. std::function<int(int)> f = g;
  52. assert(globalMemCounter.checkOutstandingNewEq(0));
  53. assert(f.target<int(*)(int)>());
  54. assert(f.target<A>() == 0);
  55. }
  56. assert(globalMemCounter.checkOutstandingNewEq(0));
  57. {
  58. std::function<int(int)> f = (int (*)(int))0;
  59. assert(!f);
  60. assert(globalMemCounter.checkOutstandingNewEq(0));
  61. assert(f.target<int(*)(int)>() == 0);
  62. assert(f.target<A>() == 0);
  63. }
  64. {
  65. std::function<int(const A*, int)> f = &A::foo;
  66. assert(f);
  67. assert(globalMemCounter.checkOutstandingNewEq(0));
  68. assert(f.target<int (A::*)(int) const>() != 0);
  69. }
  70. {
  71. std::function<void(int)> f(&g);
  72. assert(f);
  73. assert(f.target<int(*)(int)>() != 0);
  74. f(1);
  75. }
  76. }