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 // <future> 11 12 // class packaged_task<R(ArgTypes...)> 13 14 // template <class F> 15 // explicit packaged_task(F&& f); 16 17 #include <future> 18 #include <cassert> 19 20 class A 21 { 22 long data_; 23 24 public: 25 static int n_moves; 26 static int n_copies; 27 A(long i)28 explicit A(long i) : data_(i) {} A(A && a)29 A(A&& a) : data_(a.data_) {++n_moves; a.data_ = -1;} A(const A & a)30 A(const A& a) : data_(a.data_) {++n_copies;} 31 operator ()(long i,long j) const32 long operator()(long i, long j) const {return data_ + i + j;} 33 }; 34 35 int A::n_moves = 0; 36 int A::n_copies = 0; 37 func(int i)38int func(int i) { return i; } 39 main()40int main() 41 { 42 { 43 std::packaged_task<double(int, char)> p(A(5)); 44 assert(p.valid()); 45 std::future<double> f = p.get_future(); 46 p(3, 'a'); 47 assert(f.get() == 105.0); 48 assert(A::n_copies == 0); 49 assert(A::n_moves > 0); 50 } 51 A::n_copies = 0; 52 A::n_copies = 0; 53 { 54 A a(5); 55 std::packaged_task<double(int, char)> p(a); 56 assert(p.valid()); 57 std::future<double> f = p.get_future(); 58 p(3, 'a'); 59 assert(f.get() == 105.0); 60 assert(A::n_copies > 0); 61 assert(A::n_moves > 0); 62 } 63 { 64 std::packaged_task<int(int)> p(&func); 65 assert(p.valid()); 66 std::future<int> f = p.get_future(); 67 p(4); 68 assert(f.get() == 4); 69 } 70 { 71 std::packaged_task<int(int)> p(func); 72 assert(p.valid()); 73 std::future<int> f = p.get_future(); 74 p(4); 75 assert(f.get() == 4); 76 } 77 } 78