copy_assign.pass.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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& operator=(const function& f);
  12. #include <functional>
  13. #include <new>
  14. #include <cstdlib>
  15. #include <cassert>
  16. int new_called = 0;
  17. void* operator new(std::size_t s) throw(std::bad_alloc)
  18. {
  19. ++new_called;
  20. return std::malloc(s);
  21. }
  22. void operator delete(void* p) throw()
  23. {
  24. --new_called;
  25. std::free(p);
  26. }
  27. class A
  28. {
  29. int data_[10];
  30. public:
  31. static int count;
  32. A()
  33. {
  34. ++count;
  35. for (int i = 0; i < 10; ++i)
  36. data_[i] = i;
  37. }
  38. A(const A&) {++count;}
  39. ~A() {--count;}
  40. int operator()(int i) const
  41. {
  42. for (int j = 0; j < 10; ++j)
  43. i += data_[j];
  44. return i;
  45. }
  46. };
  47. int A::count = 0;
  48. int g(int) {return 0;}
  49. int main()
  50. {
  51. assert(new_called == 0);
  52. {
  53. std::function<int(int)> f = A();
  54. assert(A::count == 1);
  55. assert(new_called == 1);
  56. assert(f.target<A>());
  57. assert(f.target<int(*)(int)>() == 0);
  58. std::function<int(int)> f2;
  59. f2 = f;
  60. assert(A::count == 2);
  61. assert(new_called == 2);
  62. assert(f2.target<A>());
  63. assert(f2.target<int(*)(int)>() == 0);
  64. }
  65. assert(A::count == 0);
  66. assert(new_called == 0);
  67. {
  68. std::function<int(int)> f = g;
  69. assert(new_called == 0);
  70. assert(f.target<int(*)(int)>());
  71. assert(f.target<A>() == 0);
  72. std::function<int(int)> f2;
  73. f2 = f;
  74. assert(new_called == 0);
  75. assert(f2.target<int(*)(int)>());
  76. assert(f2.target<A>() == 0);
  77. }
  78. assert(new_called == 0);
  79. {
  80. std::function<int(int)> f;
  81. assert(new_called == 0);
  82. assert(f.target<int(*)(int)>() == 0);
  83. assert(f.target<A>() == 0);
  84. std::function<int(int)> f2;
  85. f2 = f;
  86. assert(new_called == 0);
  87. assert(f2.target<int(*)(int)>() == 0);
  88. assert(f2.target<A>() == 0);
  89. }
  90. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  91. assert(new_called == 0);
  92. {
  93. std::function<int(int)> f = A();
  94. assert(A::count == 1);
  95. assert(new_called == 1);
  96. assert(f.target<A>());
  97. assert(f.target<int(*)(int)>() == 0);
  98. std::function<int(int)> f2;
  99. f2 = std::move(f);
  100. assert(A::count == 1);
  101. assert(new_called == 1);
  102. assert(f2.target<A>());
  103. assert(f2.target<int(*)(int)>() == 0);
  104. assert(f.target<A>() == 0);
  105. assert(f.target<int(*)(int)>() == 0);
  106. }
  107. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  108. }