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 // ~packaged_task(); 15 16 #include <future> 17 #include <cassert> 18 19 class A 20 { 21 long data_; 22 23 public: 24 explicit A(long i) : data_(i) {} 25 26 long operator()(long i, long j) const {return data_ + i + j;} 27 }; 28 29 void func(std::packaged_task<double(int, char)> p) 30 { 31 } 32 33 void func2(std::packaged_task<double(int, char)> p) 34 { 35 p(3, 'a'); 36 } 37 38 int main() 39 { 40 { 41 std::packaged_task<double(int, char)> p(A(5)); 42 std::future<double> f = p.get_future(); 43 std::thread(func, std::move(p)).detach(); 44 try 45 { 46 double i = f.get(); 47 assert(false); 48 } 49 catch (const std::future_error& e) 50 { 51 assert(e.code() == make_error_code(std::future_errc::broken_promise)); 52 } 53 } 54 { 55 std::packaged_task<double(int, char)> p(A(5)); 56 std::future<double> f = p.get_future(); 57 std::thread(func2, std::move(p)).detach(); 58 assert(f.get() == 105.0); 59 } 60 } 61