copy.pass.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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(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 = f;
  59. assert(A::count == 2);
  60. assert(new_called == 2);
  61. assert(f2.target<A>());
  62. assert(f2.target<int(*)(int)>() == 0);
  63. }
  64. assert(A::count == 0);
  65. assert(new_called == 0);
  66. {
  67. std::function<int(int)> f = g;
  68. assert(new_called == 0);
  69. assert(f.target<int(*)(int)>());
  70. assert(f.target<A>() == 0);
  71. std::function<int(int)> f2 = f;
  72. assert(new_called == 0);
  73. assert(f2.target<int(*)(int)>());
  74. assert(f2.target<A>() == 0);
  75. }
  76. assert(new_called == 0);
  77. {
  78. std::function<int(int)> f;
  79. assert(new_called == 0);
  80. assert(f.target<int(*)(int)>() == 0);
  81. assert(f.target<A>() == 0);
  82. std::function<int(int)> f2 = f;
  83. assert(new_called == 0);
  84. assert(f2.target<int(*)(int)>() == 0);
  85. assert(f2.target<A>() == 0);
  86. }
  87. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  88. assert(new_called == 0);
  89. {
  90. std::function<int(int)> f = A();
  91. assert(A::count == 1);
  92. assert(new_called == 1);
  93. assert(f.target<A>());
  94. assert(f.target<int(*)(int)>() == 0);
  95. std::function<int(int)> f2 = std::move(f);
  96. assert(A::count == 1);
  97. assert(new_called == 1);
  98. assert(f2.target<A>());
  99. assert(f2.target<int(*)(int)>() == 0);
  100. assert(f.target<A>() == 0);
  101. assert(f.target<int(*)(int)>() == 0);
  102. }
  103. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  104. }