Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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<class A> function(allocator_arg_t, const A&, function&&); |
| 15 | |
| 16 | #include <functional> |
| 17 | #include <cassert> |
| 18 | |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 19 | #include "../test_allocator.h" |
| 20 | |
| 21 | int new_called = 0; |
| 22 | |
| 23 | void* operator new(std::size_t s) throw(std::bad_alloc) |
| 24 | { |
| 25 | ++new_called; |
| 26 | return std::malloc(s); |
| 27 | } |
| 28 | |
| 29 | void operator delete(void* p) throw() |
| 30 | { |
| 31 | --new_called; |
| 32 | std::free(p); |
| 33 | } |
| 34 | |
| 35 | class A |
| 36 | { |
| 37 | int data_[10]; |
| 38 | public: |
| 39 | static int count; |
| 40 | |
| 41 | A() |
| 42 | { |
| 43 | ++count; |
| 44 | for (int i = 0; i < 10; ++i) |
| 45 | data_[i] = i; |
| 46 | } |
| 47 | |
| 48 | A(const A&) {++count;} |
| 49 | |
| 50 | ~A() {--count;} |
| 51 | |
| 52 | int operator()(int i) const |
| 53 | { |
| 54 | for (int j = 0; j < 10; ++j) |
| 55 | i += data_[j]; |
| 56 | return i; |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | int A::count = 0; |
| 61 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 62 | int main() |
| 63 | { |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 64 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 7255280 | 2010-08-20 19:36:46 +0000 | [diff] [blame] | 65 | assert(new_called == 0); |
| 66 | { |
| 67 | std::function<int(int)> f = A(); |
| 68 | assert(A::count == 1); |
| 69 | assert(new_called == 1); |
| 70 | assert(f.target<A>()); |
| 71 | assert(f.target<int(*)(int)>() == 0); |
| 72 | std::function<int(int)> f2(std::allocator_arg, test_allocator<A>(), std::move(f)); |
| 73 | assert(A::count == 1); |
| 74 | assert(new_called == 1); |
| 75 | assert(f2.target<A>()); |
| 76 | assert(f2.target<int(*)(int)>() == 0); |
| 77 | assert(f.target<A>() == 0); |
| 78 | assert(f.target<int(*)(int)>() == 0); |
| 79 | } |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 80 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 81 | } |