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