nullptr_t_assign.pass.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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& operator=(nullptr_t);
  11. #include <functional>
  12. #include <cassert>
  13. #include "count_new.hpp"
  14. #include "test_macros.h"
  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. };
  35. int A::count = 0;
  36. int g(int) {return 0;}
  37. int main(int, char**)
  38. {
  39. assert(globalMemCounter.checkOutstandingNewEq(0));
  40. {
  41. std::function<int(int)> f = A();
  42. assert(A::count == 1);
  43. assert(globalMemCounter.checkOutstandingNewEq(1));
  44. assert(f.target<A>());
  45. f = nullptr;
  46. assert(A::count == 0);
  47. assert(globalMemCounter.checkOutstandingNewEq(0));
  48. assert(f.target<A>() == 0);
  49. }
  50. {
  51. std::function<int(int)> f = g;
  52. assert(globalMemCounter.checkOutstandingNewEq(0));
  53. assert(f.target<int(*)(int)>());
  54. assert(f.target<A>() == 0);
  55. f = nullptr;
  56. assert(globalMemCounter.checkOutstandingNewEq(0));
  57. assert(f.target<int(*)(int)>() == 0);
  58. }
  59. return 0;
  60. }