alloc_rfunction.pass.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // UNSUPPORTED: c++98, c++03
  9. // REQUIRES: c++11 || c++14
  10. // <functional>
  11. // class function<R(ArgTypes...)>
  12. // template<class A> function(allocator_arg_t, const A&, function&&);
  13. //
  14. // This signature was removed in C++17
  15. #include <functional>
  16. #include <memory>
  17. #include <cassert>
  18. #include "test_macros.h"
  19. #include "min_allocator.h"
  20. #include "count_new.hpp"
  21. class A
  22. {
  23. int data_[10];
  24. public:
  25. static int count;
  26. A()
  27. {
  28. ++count;
  29. for (int i = 0; i < 10; ++i)
  30. data_[i] = i;
  31. }
  32. A(const A&) {++count;}
  33. ~A() {--count;}
  34. int operator()(int i) const
  35. {
  36. for (int j = 0; j < 10; ++j)
  37. i += data_[j];
  38. return i;
  39. }
  40. };
  41. int A::count = 0;
  42. int g(int) { return 0; }
  43. int main(int, char**)
  44. {
  45. assert(globalMemCounter.checkOutstandingNewEq(0));
  46. {
  47. std::function<int(int)> f = A();
  48. assert(A::count == 1);
  49. assert(globalMemCounter.checkOutstandingNewEq(1));
  50. assert(f.target<A>());
  51. assert(f.target<int(*)(int)>() == 0);
  52. std::function<int(int)> f2(std::allocator_arg, bare_allocator<A>(), std::move(f));
  53. assert(A::count == 1);
  54. assert(globalMemCounter.checkOutstandingNewEq(1));
  55. assert(f2.target<A>());
  56. assert(f2.target<int(*)(int)>() == 0);
  57. assert(f.target<A>() == 0);
  58. assert(f.target<int(*)(int)>() == 0);
  59. }
  60. assert(globalMemCounter.checkOutstandingNewEq(0));
  61. {
  62. // Test that moving a function constructed from a reference wrapper
  63. // is done without allocating.
  64. DisableAllocationGuard g;
  65. using Ref = std::reference_wrapper<A>;
  66. A a;
  67. Ref aref(a);
  68. std::function<int(int)> f(aref);
  69. assert(A::count == 1);
  70. assert(f.target<A>() == nullptr);
  71. assert(f.target<Ref>());
  72. std::function<int(int)> f2(std::allocator_arg, std::allocator<void>{},
  73. std::move(f));
  74. assert(A::count == 1);
  75. assert(f2.target<A>() == nullptr);
  76. assert(f2.target<Ref>());
  77. assert(f.target<Ref>()); // f is unchanged because the target is small
  78. }
  79. {
  80. // Test that moving a function constructed from a function pointer
  81. // is done without allocating
  82. DisableAllocationGuard guard;
  83. using Ptr = int(*)(int);
  84. Ptr p = g;
  85. std::function<int(int)> f(p);
  86. assert(f.target<A>() == nullptr);
  87. assert(f.target<Ptr>());
  88. std::function<int(int)> f2(std::allocator_arg, std::allocator<void>(),
  89. std::move(f));
  90. assert(f2.target<A>() == nullptr);
  91. assert(f2.target<Ptr>());
  92. assert(f.target<Ptr>()); // f is unchanged because the target is small
  93. }
  94. return 0;
  95. }