F.pass.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <functional>
  9. // class function<R(ArgTypes...)>
  10. // function(F);
  11. #include <functional>
  12. #include <cassert>
  13. #include "test_macros.h"
  14. #include "count_new.hpp"
  15. class A
  16. {
  17. int data_[10];
  18. public:
  19. static int count;
  20. A()
  21. {
  22. ++count;
  23. for (int i = 0; i < 10; ++i)
  24. data_[i] = i;
  25. }
  26. A(const A&) {++count;}
  27. ~A() {--count;}
  28. int operator()(int i) const
  29. {
  30. for (int j = 0; j < 10; ++j)
  31. i += data_[j];
  32. return i;
  33. }
  34. int foo(int) const {return 1;}
  35. };
  36. int A::count = 0;
  37. int g(int) {return 0;}
  38. #if TEST_STD_VER >= 11
  39. struct RValueCallable {
  40. template <class ...Args>
  41. void operator()(Args&&...) && {}
  42. };
  43. struct LValueCallable {
  44. template <class ...Args>
  45. void operator()(Args&&...) & {}
  46. };
  47. #endif
  48. int main(int, char**)
  49. {
  50. assert(globalMemCounter.checkOutstandingNewEq(0));
  51. {
  52. std::function<int(int)> f = A();
  53. assert(A::count == 1);
  54. assert(globalMemCounter.checkOutstandingNewEq(1));
  55. assert(f.target<A>());
  56. assert(f.target<int(*)(int)>() == 0);
  57. }
  58. assert(A::count == 0);
  59. assert(globalMemCounter.checkOutstandingNewEq(0));
  60. {
  61. std::function<int(int)> f = g;
  62. assert(globalMemCounter.checkOutstandingNewEq(0));
  63. assert(f.target<int(*)(int)>());
  64. assert(f.target<A>() == 0);
  65. }
  66. assert(globalMemCounter.checkOutstandingNewEq(0));
  67. {
  68. std::function<int(int)> f = (int (*)(int))0;
  69. assert(!f);
  70. assert(globalMemCounter.checkOutstandingNewEq(0));
  71. assert(f.target<int(*)(int)>() == 0);
  72. assert(f.target<A>() == 0);
  73. }
  74. {
  75. std::function<int(const A*, int)> f = &A::foo;
  76. assert(f);
  77. assert(globalMemCounter.checkOutstandingNewEq(0));
  78. assert(f.target<int (A::*)(int) const>() != 0);
  79. }
  80. {
  81. std::function<void(int)> f(&g);
  82. assert(f);
  83. assert(f.target<int(*)(int)>() != 0);
  84. f(1);
  85. }
  86. {
  87. std::function <void()> f(static_cast<void (*)()>(0));
  88. assert(!f);
  89. }
  90. #if TEST_STD_VER >= 11
  91. {
  92. using Fn = std::function<void(int, int, int)>;
  93. static_assert(std::is_constructible<Fn, LValueCallable&>::value, "");
  94. static_assert(std::is_constructible<Fn, LValueCallable>::value, "");
  95. static_assert(!std::is_constructible<Fn, RValueCallable&>::value, "");
  96. static_assert(!std::is_constructible<Fn, RValueCallable>::value, "");
  97. }
  98. #endif
  99. return 0;
  100. }