1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // UNSUPPORTED: libcpp-has-no-threads 10 // UNSUPPORTED: c++03 11 12 // <future> 13 14 // class packaged_task<R(ArgTypes...)> 15 16 // ~packaged_task(); 17 18 #include <future> 19 #include <cassert> 20 21 #include "test_macros.h" 22 23 class A 24 { 25 long data_; 26 27 public: 28 explicit A(long i) : data_(i) {} 29 30 long operator()(long i, long j) const {return data_ + i + j;} 31 }; 32 33 void func(std::packaged_task<double(int, char)>) 34 { 35 } 36 37 void func2(std::packaged_task<double(int, char)> p) 38 { 39 p(3, 'a'); 40 } 41 42 int main(int, char**) 43 { 44 #ifndef TEST_HAS_NO_EXCEPTIONS 45 { 46 std::packaged_task<double(int, char)> p(A(5)); 47 std::future<double> f = p.get_future(); 48 std::thread(func, std::move(p)).detach(); 49 try 50 { 51 double i = f.get(); 52 ((void)i); // Prevent unused warning 53 assert(false); 54 } 55 catch (const std::future_error& e) 56 { 57 assert(e.code() == make_error_code(std::future_errc::broken_promise)); 58 } 59 } 60 #endif 61 { 62 std::packaged_task<double(int, char)> p(A(5)); 63 std::future<double> f = p.get_future(); 64 std::thread(func2, std::move(p)).detach(); 65 assert(f.get() == 105.0); 66 } 67 68 return 0; 69 } 70