xref: /llvm-project/libcxx/test/std/thread/futures/futures.task/futures.task.members/dtor.pass.cpp (revision a7f9895cc18995549c7facb96e72718da282a864)
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: 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 "make_test_thread.h"
22 #include "test_macros.h"
23 
24 class A
25 {
26     long data_;
27 
28 public:
A(long i)29     explicit A(long i) : data_(i) {}
30 
operator ()(long i,long j) const31     long operator()(long i, long j) const {return data_ + i + j;}
32 };
33 
func(std::packaged_task<double (int,char)>)34 void func(std::packaged_task<double(int, char)>)
35 {
36 }
37 
func2(std::packaged_task<double (int,char)> p)38 void func2(std::packaged_task<double(int, char)> p) { p(3, 97); }
39 
main(int,char **)40 int main(int, char**)
41 {
42 #ifndef TEST_HAS_NO_EXCEPTIONS
43     {
44         std::packaged_task<double(int, char)> p(A(5));
45         std::future<double> f = p.get_future();
46         support::make_test_thread(func, std::move(p)).detach();
47         try
48         {
49             double i = f.get();
50             ((void)i); // Prevent unused warning
51             assert(false);
52         }
53         catch (const std::future_error& e)
54         {
55             assert(e.code() == make_error_code(std::future_errc::broken_promise));
56         }
57     }
58 #endif
59     {
60         std::packaged_task<double(int, char)> p(A(5));
61         std::future<double> f = p.get_future();
62         support::make_test_thread(func2, std::move(p)).detach();
63         assert(f.get() == 105.0);
64     }
65 
66   return 0;
67 }
68