Home | History | Annotate | Download | only in func.wrap.func.targ
      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<typename T>
     15 //   requires Callable<T, ArgTypes...> && Convertible<Callable<T, ArgTypes...>::result_type, R>
     16 //   T*
     17 //   target();
     18 // template<typename T>
     19 //   requires Callable<T, ArgTypes...> && Convertible<Callable<T, ArgTypes...>::result_type, R>
     20 //   const T*
     21 //   target() const;
     22 
     23 #include <functional>
     24 #include <new>
     25 #include <cstdlib>
     26 #include <cassert>
     27 
     28 class A
     29 {
     30     int data_[10];
     31 public:
     32     static int count;
     33 
     34     A()
     35     {
     36         ++count;
     37         for (int i = 0; i < 10; ++i)
     38             data_[i] = i;
     39     }
     40 
     41     A(const A&) {++count;}
     42 
     43     ~A() {--count;}
     44 
     45     int operator()(int i) const
     46     {
     47         for (int j = 0; j < 10; ++j)
     48             i += data_[j];
     49         return i;
     50     }
     51 
     52     int foo(int) const {return 1;}
     53 };
     54 
     55 int A::count = 0;
     56 
     57 int g(int) {return 0;}
     58 
     59 int main()
     60 {
     61     {
     62     std::function<int(int)> f = A();
     63     assert(A::count == 1);
     64     assert(f.target<A>());
     65     assert(f.target<int(*)(int)>() == 0);
     66     assert(f.target<int>() == nullptr);
     67     }
     68     assert(A::count == 0);
     69     {
     70     std::function<int(int)> f = g;
     71     assert(A::count == 0);
     72     assert(f.target<int(*)(int)>());
     73     assert(f.target<A>() == 0);
     74     assert(f.target<int>() == nullptr);
     75     }
     76     assert(A::count == 0);
     77     {
     78     const std::function<int(int)> f = A();
     79     assert(A::count == 1);
     80     assert(f.target<A>());
     81     assert(f.target<int(*)(int)>() == 0);
     82     assert(f.target<int>() == nullptr);
     83     }
     84     assert(A::count == 0);
     85     {
     86     const std::function<int(int)> f = g;
     87     assert(A::count == 0);
     88     assert(f.target<int(*)(int)>());
     89     assert(f.target<A>() == 0);
     90     assert(f.target<int>() == nullptr);
     91     }
     92     assert(A::count == 0);
     93 }
     94