alloc_rfunction.pass.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 A> function(allocator_arg_t, const A&, function&&);
  12. #include <functional>
  13. #include <cassert>
  14. #include "../test_allocator.h"
  15. int new_called = 0;
  16. void* operator new(std::size_t s) throw(std::bad_alloc)
  17. {
  18. ++new_called;
  19. return std::malloc(s);
  20. }
  21. void operator delete(void* p) throw()
  22. {
  23. --new_called;
  24. std::free(p);
  25. }
  26. class A
  27. {
  28. int data_[10];
  29. public:
  30. static int count;
  31. A()
  32. {
  33. ++count;
  34. for (int i = 0; i < 10; ++i)
  35. data_[i] = i;
  36. }
  37. A(const A&) {++count;}
  38. ~A() {--count;}
  39. int operator()(int i) const
  40. {
  41. for (int j = 0; j < 10; ++j)
  42. i += data_[j];
  43. return i;
  44. }
  45. };
  46. int A::count = 0;
  47. int main()
  48. {
  49. #ifdef _LIBCPP_MOVE
  50. assert(new_called == 0);
  51. {
  52. std::function<int(int)> f = A();
  53. assert(A::count == 1);
  54. assert(new_called == 1);
  55. assert(f.target<A>());
  56. assert(f.target<int(*)(int)>() == 0);
  57. std::function<int(int)> f2(std::allocator_arg, test_allocator<A>(), std::move(f));
  58. assert(A::count == 1);
  59. assert(new_called == 1);
  60. assert(f2.target<A>());
  61. assert(f2.target<int(*)(int)>() == 0);
  62. assert(f.target<A>() == 0);
  63. assert(f.target<int(*)(int)>() == 0);
  64. }
  65. #endif // _LIBCPP_MOVE
  66. }