alloc_F.pass.cpp revision b64f8b07c104c6cc986570ac8ee0ed16a9f23976
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, class A> function(allocator_arg_t, const A&, F); 15 16#include <functional> 17#include <cassert> 18 19#include "../test_allocator.h" 20 21class A 22{ 23 int data_[10]; 24public: 25 static int count; 26 27 A() 28 { 29 ++count; 30 for (int i = 0; i < 10; ++i) 31 data_[i] = i; 32 } 33 34 A(const A&) {++count;} 35 36 ~A() {--count;} 37 38 int operator()(int i) const 39 { 40 for (int j = 0; j < 10; ++j) 41 i += data_[j]; 42 return i; 43 } 44 45 int foo(int) const {return 1;} 46}; 47 48int A::count = 0; 49 50int g(int) {return 0;} 51 52int main() 53{ 54 { 55 std::function<int(int)> f(std::allocator_arg, test_allocator<A>(), A()); 56 assert(A::count == 1); 57 assert(f.target<A>()); 58 assert(f.target<int(*)(int)>() == 0); 59 } 60 assert(A::count == 0); 61 { 62 std::function<int(int)> f(std::allocator_arg, test_allocator<int(*)(int)>(), g); 63 assert(f.target<int(*)(int)>()); 64 assert(f.target<A>() == 0); 65 } 66 { 67 std::function<int(int)> f(std::allocator_arg, test_allocator<int(*)(int)>(), 68 (int (*)(int))0); 69 assert(!f); 70 assert(f.target<int(*)(int)>() == 0); 71 assert(f.target<A>() == 0); 72 } 73 { 74 std::function<int(const A*, int)> f(std::allocator_arg, 75 test_allocator<int(A::*)(int)const>(), 76 &A::foo); 77 assert(f); 78 assert(f.target<int (A::*)(int) const>() != 0); 79 } 80} 81