F.pass.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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(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. int foo(int) const {return 1;}
  47. };
  48. int A::count = 0;
  49. int g(int) {return 0;}
  50. int main()
  51. {
  52. assert(new_called == 0);
  53. {
  54. std::function<int(int)> f = A();
  55. assert(A::count == 1);
  56. assert(new_called == 1);
  57. assert(f.target<A>());
  58. assert(f.target<int(*)(int)>() == 0);
  59. }
  60. assert(A::count == 0);
  61. assert(new_called == 0);
  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. }
  68. assert(new_called == 0);
  69. {
  70. std::function<int(int)> f = (int (*)(int))0;
  71. assert(!f);
  72. assert(new_called == 0);
  73. assert(f.target<int(*)(int)>() == 0);
  74. assert(f.target<A>() == 0);
  75. }
  76. {
  77. std::function<int(const A*, int)> f = &A::foo;
  78. assert(f);
  79. assert(new_called == 0);
  80. assert(f.target<int (A::*)(int) const>() != 0);
  81. }
  82. }