Home | History | Annotate | Download | only in func.wrap.func.con
      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 
     10 // <functional>
     11 
     12 // class function<R(ArgTypes...)>
     13 
     14 // function(const function& f);
     15 
     16 #include <functional>
     17 #include <cstdlib>
     18 #include <cassert>
     19 
     20 #include "count_new.hpp"
     21 
     22 class A
     23 {
     24     int data_[10];
     25 public:
     26     static int count;
     27 
     28     A()
     29     {
     30         ++count;
     31         for (int i = 0; i < 10; ++i)
     32             data_[i] = i;
     33     }
     34 
     35     A(const A&) {++count;}
     36 
     37     ~A() {--count;}
     38 
     39     int operator()(int i) const
     40     {
     41         for (int j = 0; j < 10; ++j)
     42             i += data_[j];
     43         return i;
     44     }
     45 };
     46 
     47 int A::count = 0;
     48 
     49 int g(int) {return 0;}
     50 
     51 int main()
     52 {
     53     assert(globalMemCounter.checkOutstandingNewEq(0));
     54     {
     55     std::function<int(int)> f = A();
     56     assert(A::count == 1);
     57     assert(globalMemCounter.checkOutstandingNewEq(1));
     58     assert(f.target<A>());
     59     assert(f.target<int(*)(int)>() == 0);
     60     std::function<int(int)> f2 = f;
     61     assert(A::count == 2);
     62     assert(globalMemCounter.checkOutstandingNewEq(2));
     63     assert(f2.target<A>());
     64     assert(f2.target<int(*)(int)>() == 0);
     65     }
     66     assert(A::count == 0);
     67     assert(globalMemCounter.checkOutstandingNewEq(0));
     68     {
     69     std::function<int(int)> f = g;
     70     assert(globalMemCounter.checkOutstandingNewEq(0));
     71     assert(f.target<int(*)(int)>());
     72     assert(f.target<A>() == 0);
     73     std::function<int(int)> f2 = f;
     74     assert(globalMemCounter.checkOutstandingNewEq(0));
     75     assert(f2.target<int(*)(int)>());
     76     assert(f2.target<A>() == 0);
     77     }
     78     assert(globalMemCounter.checkOutstandingNewEq(0));
     79     {
     80     std::function<int(int)> f;
     81     assert(globalMemCounter.checkOutstandingNewEq(0));
     82     assert(f.target<int(*)(int)>() == 0);
     83     assert(f.target<A>() == 0);
     84     std::function<int(int)> f2 = f;
     85     assert(globalMemCounter.checkOutstandingNewEq(0));
     86     assert(f2.target<int(*)(int)>() == 0);
     87     assert(f2.target<A>() == 0);
     88     }
     89     {
     90     std::function<int(int)> f;
     91     assert(globalMemCounter.checkOutstandingNewEq(0));
     92     assert(f.target<int(*)(int)>() == 0);
     93     assert(f.target<A>() == 0);
     94     assert(!f);
     95     std::function<long(int)> g = f;
     96     assert(globalMemCounter.checkOutstandingNewEq(0));
     97     assert(g.target<long(*)(int)>() == 0);
     98     assert(g.target<A>() == 0);
     99     assert(!g);
    100     }
    101 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
    102     assert(globalMemCounter.checkOutstandingNewEq(0));
    103     {
    104     std::function<int(int)> f = A();
    105     assert(A::count == 1);
    106     assert(globalMemCounter.checkOutstandingNewEq(1));
    107     assert(f.target<A>());
    108     assert(f.target<int(*)(int)>() == 0);
    109     std::function<int(int)> f2 = std::move(f);
    110     assert(A::count == 1);
    111     assert(globalMemCounter.checkOutstandingNewEq(1));
    112     assert(f2.target<A>());
    113     assert(f2.target<int(*)(int)>() == 0);
    114     assert(f.target<A>() == 0);
    115     assert(f.target<int(*)(int)>() == 0);
    116     }
    117 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
    118 }
    119