Debian Codegraph @

libcxx/test/std/thread/futures/futures.task/futures.task.members/get_future.pass.cpp

raw
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 // future<R> get_future();
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 int main(int, char**)
34 {
35 {
36 std::packaged_task<double(int, char)> p(A(5));
37 std::future<double> f = p.get_future();
38 p(3, 97);
39 assert(f.get() == 105.0);
40 }
41 #ifndef TEST_HAS_NO_EXCEPTIONS
42 {
43 std::packaged_task<double(int, char)> p(A(5));
44 std::future<double> f = p.get_future();
45 try
46 {
47 f = p.get_future();
48 assert(false);
49 }
50 catch (const std::future_error& e)
51 {
52 assert(e.code() == make_error_code(std::future_errc::future_already_retrieved));
53 }
54 }
55 {
56 std::packaged_task<double(int, char)> p;
57 try
58 {
59 std::future<double> f = p.get_future();
60 assert(false);
61 }
62 catch (const std::future_error& e)
63 {
64 assert(e.code() == make_error_code(std::future_errc::no_state));
65 }
66 }
67 #endif
68
69 return 0;
70 }