target.pass.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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<typename T>
  12. // requires Callable<T, ArgTypes...> && Convertible<Callable<T, ArgTypes...>::result_type, R>
  13. // T*
  14. // target();
  15. // template<typename T>
  16. // requires Callable<T, ArgTypes...> && Convertible<Callable<T, ArgTypes...>::result_type, R>
  17. // const T*
  18. // target() const;
  19. #include <functional>
  20. #include <new>
  21. #include <cstdlib>
  22. #include <cassert>
  23. class A
  24. {
  25. int data_[10];
  26. public:
  27. static int count;
  28. A()
  29. {
  30. ++count;
  31. for (int i = 0; i < 10; ++i)
  32. data_[i] = i;
  33. }
  34. A(const A&) {++count;}
  35. ~A() {--count;}
  36. int operator()(int i) const
  37. {
  38. for (int j = 0; j < 10; ++j)
  39. i += data_[j];
  40. return i;
  41. }
  42. int foo(int) const {return 1;}
  43. };
  44. int A::count = 0;
  45. int g(int) {return 0;}
  46. int main()
  47. {
  48. {
  49. std::function<int(int)> f = A();
  50. assert(A::count == 1);
  51. assert(f.target<A>());
  52. assert(f.target<int(*)(int)>() == 0);
  53. }
  54. assert(A::count == 0);
  55. {
  56. std::function<int(int)> f = g;
  57. assert(A::count == 0);
  58. assert(f.target<int(*)(int)>());
  59. assert(f.target<A>() == 0);
  60. }
  61. assert(A::count == 0);
  62. {
  63. const std::function<int(int)> f = A();
  64. assert(A::count == 1);
  65. assert(f.target<A>());
  66. assert(f.target<int(*)(int)>() == 0);
  67. }
  68. assert(A::count == 0);
  69. {
  70. const std::function<int(int)> f = g;
  71. assert(A::count == 0);
  72. assert(f.target<int(*)(int)>());
  73. assert(f.target<A>() == 0);
  74. }
  75. assert(A::count == 0);
  76. }