nullptr_t_assign.pass.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // function& operator=(nullptr_t);
  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. f = nullptr;
  58. assert(A::count == 0);
  59. assert(new_called == 0);
  60. assert(f.target<A>() == 0);
  61. }
  62. {
  63. std::function<int(int)> f = g;
  64. assert(new_called == 0);
  65. assert(f.target<int(*)(int)>());
  66. assert(f.target<A>() == 0);
  67. f = nullptr;
  68. assert(new_called == 0);
  69. assert(f.target<int(*)(int)>() == 0);
  70. }
  71. }