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 
9 // <functional>
10 
11 // class function<R(ArgTypes...)>
12 
13 // function& operator=(nullptr_t);
14 
15 // This test runs in C++03, but we have deprecated using std::function in C++03.
16 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS -D_LIBCPP_ENABLE_CXX03_FUNCTION
17 
18 #include <functional>
19 #include <cassert>
20 
21 #include "count_new.h"
22 
23 #include "test_macros.h"
24 
25 class A
26 {
27     int data_[10];
28 public:
29     static int count;
30 
A()31     A()
32     {
33         ++count;
34         for (int i = 0; i < 10; ++i)
35             data_[i] = i;
36     }
37 
A(const A &)38     A(const A&) {++count;}
39 
~A()40     ~A() {--count;}
41 
operator ()(int i) const42     int operator()(int i) const
43     {
44         for (int j = 0; j < 10; ++j)
45             i += data_[j];
46         return i;
47     }
48 };
49 
50 int A::count = 0;
51 
g(int)52 int g(int) {return 0;}
53 
main(int,char **)54 int main(int, char**)
55 {
56     globalMemCounter.reset();
57     assert(globalMemCounter.checkOutstandingNewEq(0));
58     {
59     std::function<int(int)> f = A();
60     assert(A::count == 1);
61     assert(globalMemCounter.checkOutstandingNewEq(1));
62     RTTI_ASSERT(f.target<A>());
63     f = nullptr;
64     assert(A::count == 0);
65     assert(globalMemCounter.checkOutstandingNewEq(0));
66     RTTI_ASSERT(f.target<A>() == 0);
67     }
68     {
69     std::function<int(int)> f = g;
70     assert(globalMemCounter.checkOutstandingNewEq(0));
71     RTTI_ASSERT(f.target<int(*)(int)>());
72     RTTI_ASSERT(f.target<A>() == 0);
73     f = nullptr;
74     assert(globalMemCounter.checkOutstandingNewEq(0));
75     RTTI_ASSERT(f.target<int(*)(int)>() == 0);
76     }
77 
78   return 0;
79 }
80