F_assign.pass.cpp 2.8 KB

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