F_assign.pass.cpp 2.8 KB

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