F.pass.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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(F);
  12. #include <functional>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. #include "count_new.hpp"
  16. class A
  17. {
  18. int data_[10];
  19. public:
  20. static int count;
  21. A()
  22. {
  23. ++count;
  24. for (int i = 0; i < 10; ++i)
  25. data_[i] = i;
  26. }
  27. A(const A&) {++count;}
  28. ~A() {--count;}
  29. int operator()(int i) const
  30. {
  31. for (int j = 0; j < 10; ++j)
  32. i += data_[j];
  33. return i;
  34. }
  35. int foo(int) const {return 1;}
  36. };
  37. int A::count = 0;
  38. int g(int) {return 0;}
  39. #if TEST_STD_VER >= 11
  40. struct RValueCallable {
  41. template <class ...Args>
  42. void operator()(Args&&...) && {}
  43. };
  44. struct LValueCallable {
  45. template <class ...Args>
  46. void operator()(Args&&...) & {}
  47. };
  48. #endif
  49. int main()
  50. {
  51. assert(globalMemCounter.checkOutstandingNewEq(0));
  52. {
  53. std::function<int(int)> f = A();
  54. assert(A::count == 1);
  55. assert(globalMemCounter.checkOutstandingNewEq(1));
  56. assert(f.target<A>());
  57. assert(f.target<int(*)(int)>() == 0);
  58. }
  59. assert(A::count == 0);
  60. assert(globalMemCounter.checkOutstandingNewEq(0));
  61. {
  62. std::function<int(int)> f = g;
  63. assert(globalMemCounter.checkOutstandingNewEq(0));
  64. assert(f.target<int(*)(int)>());
  65. assert(f.target<A>() == 0);
  66. }
  67. assert(globalMemCounter.checkOutstandingNewEq(0));
  68. {
  69. std::function<int(int)> f = (int (*)(int))0;
  70. assert(!f);
  71. assert(globalMemCounter.checkOutstandingNewEq(0));
  72. assert(f.target<int(*)(int)>() == 0);
  73. assert(f.target<A>() == 0);
  74. }
  75. {
  76. std::function<int(const A*, int)> f = &A::foo;
  77. assert(f);
  78. assert(globalMemCounter.checkOutstandingNewEq(0));
  79. assert(f.target<int (A::*)(int) const>() != 0);
  80. }
  81. {
  82. std::function<void(int)> f(&g);
  83. assert(f);
  84. assert(f.target<int(*)(int)>() != 0);
  85. f(1);
  86. }
  87. {
  88. std::function <void()> f(static_cast<void (*)()>(0));
  89. assert(!f);
  90. }
  91. #if TEST_STD_VER >= 11
  92. {
  93. using Fn = std::function<void(int, int, int)>;
  94. static_assert(std::is_constructible<Fn, LValueCallable&>::value, "");
  95. static_assert(std::is_constructible<Fn, LValueCallable>::value, "");
  96. static_assert(!std::is_constructible<Fn, RValueCallable&>::value, "");
  97. static_assert(!std::is_constructible<Fn, RValueCallable>::value, "");
  98. }
  99. #endif
  100. }