| 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 |
// template <class F> |
| 17 |
// explicit packaged_task(F&& f); |
| 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 |
static int n_moves; |
| 30 |
static int n_copies; |
| 31 |
|
| 32 |
explicit A(long i) : data_(i) {} |
| 33 |
A(A&& a) : data_(a.data_) {++n_moves; a.data_ = -1;} |
| 34 |
A(const A& a) : data_(a.data_) {++n_copies;} |
| 35 |
|
| 36 |
long operator()(long i, long j) const {return data_ + i + j;} |
| 37 |
}; |
| 38 |
|
| 39 |
int A::n_moves = 0; |
| 40 |
int A::n_copies = 0; |
| 41 |
|
| 42 |
int func(int i) { return i; } |
| 43 |
|
| 44 |
int main(int, char**) |
| 45 |
{ |
| 46 |
{ |
| 47 |
std::packaged_task<double(int, char)> p(A(5)); |
| 48 |
assert(p.valid()); |
| 49 |
std::future<double> f = p.get_future(); |
| 50 |
p(3, 97); |
| 51 |
assert(f.get() == 105.0); |
| 52 |
assert(A::n_copies == 0); |
| 53 |
assert(A::n_moves > 0); |
| 54 |
} |
| 55 |
A::n_copies = 0; |
| 56 |
A::n_copies = 0; |
| 57 |
{ |
| 58 |
A a(5); |
| 59 |
std::packaged_task<double(int, char)> p(a); |
| 60 |
assert(p.valid()); |
| 61 |
std::future<double> f = p.get_future(); |
| 62 |
p(3, 97); |
| 63 |
assert(f.get() == 105.0); |
| 64 |
assert(A::n_copies > 0); |
| 65 |
assert(A::n_moves > 0); |
| 66 |
} |
| 67 |
{ |
| 68 |
std::packaged_task<int(int)> p(&func); |
| 69 |
assert(p.valid()); |
| 70 |
std::future<int> f = p.get_future(); |
| 71 |
p(4); |
| 72 |
assert(f.get() == 4); |
| 73 |
} |
| 74 |
{ |
| 75 |
std::packaged_task<int(int)> p(func); |
| 76 |
assert(p.valid()); |
| 77 |
std::future<int> f = p.get_future(); |
| 78 |
p(4); |
| 79 |
assert(f.get() == 4); |
| 80 |
} |
| 81 |
|
| 82 |
return 0; |
| 83 |
} |