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 // UNSUPPORTED: libcpp-has-no-threads
11 // UNSUPPORTED: c++98, c++03
12 
13 // <future>
14 
15 // class packaged_task<R(ArgTypes...)>
16 
17 // ~packaged_task();
18 
19 #include <future>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 
24 class A
25 {
26     long data_;
27 
28 public:
29     explicit A(long i) : data_(i) {}
30 
31     long operator()(long i, long j) const {return data_ + i + j;}
32 };
33 
34 void func(std::packaged_task<double(int, char)> p)
35 {
36 }
37 
38 void func2(std::packaged_task<double(int, char)> p)
39 {
40     p(3, 'a');
41 }
42 
43 int main()
44 {
45 #ifndef TEST_HAS_NO_EXCEPTIONS
46     {
47         std::packaged_task<double(int, char)> p(A(5));
48         std::future<double> f = p.get_future();
49         std::thread(func, std::move(p)).detach();
50         try
51         {
52             double i = f.get();
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