target.pass.cpp revision c52f43e72dfcea03037729649da84c23b3beb04a
1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. 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
28class A
29{
30    int data_[10];
31public:
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
55int A::count = 0;
56
57int g(int) {return 0;}
58
59int 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    }
67    assert(A::count == 0);
68    {
69    std::function<int(int)> f = g;
70    assert(A::count == 0);
71    assert(f.target<int(*)(int)>());
72    assert(f.target<A>() == 0);
73    }
74    assert(A::count == 0);
75    {
76    const std::function<int(int)> f = A();
77    assert(A::count == 1);
78    assert(f.target<A>());
79    assert(f.target<int(*)(int)>() == 0);
80    }
81    assert(A::count == 0);
82    {
83    const std::function<int(int)> f = g;
84    assert(A::count == 0);
85    assert(f.target<int(*)(int)>());
86    assert(f.target<A>() == 0);
87    }
88    assert(A::count == 0);
89}
90