F_assign.pass.cpp 2.1 KB

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