1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-threads
11 // packaged_task allocator support was removed in C++17 (LWG 2921)
12 // REQUIRES: c++11 || c++14
16 // class packaged_task<R(ArgTypes...)>
18 // template <class F, class Allocator>
19 // explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
24 #include "test_macros.h"
25 #include "test_allocator.h"
26 #include "min_allocator.h"
36 explicit A(long i
) : data_(i
) {}
37 A(A
&& a
) : data_(a
.data_
) {++n_moves
; a
.data_
= -1;}
38 A(const A
& a
) : data_(a
.data_
) {++n_copies
;}
40 long operator()(long i
, long j
) const {return data_
+ i
+ j
;}
46 int func(int i
) { return i
; }
50 test_allocator_statistics alloc_stats
;
52 std::packaged_task
<double(int, char)> p(std::allocator_arg
,
53 test_allocator
<A
>(&alloc_stats
), A(5));
54 assert(alloc_stats
.alloc_count
> 0);
56 std::future
<double> f
= p
.get_future();
58 assert(f
.get() == 105.0);
59 assert(A::n_copies
== 0);
60 assert(A::n_moves
> 0);
62 assert(alloc_stats
.alloc_count
== 0);
67 std::packaged_task
<double(int, char)> p(std::allocator_arg
,
68 test_allocator
<A
>(&alloc_stats
), a
);
69 assert(alloc_stats
.alloc_count
> 0);
71 std::future
<double> f
= p
.get_future();
73 assert(f
.get() == 105.0);
74 assert(A::n_copies
> 0);
75 assert(A::n_moves
>= 0);
77 assert(alloc_stats
.alloc_count
== 0);
82 std::packaged_task
<int(int)> p(std::allocator_arg
, test_allocator
<A
>(&alloc_stats
), &func
);
83 assert(alloc_stats
.alloc_count
> 0);
85 std::future
<int> f
= p
.get_future();
89 assert(alloc_stats
.alloc_count
== 0);
94 std::packaged_task
<int(int)> p(std::allocator_arg
, test_allocator
<A
>(&alloc_stats
), func
);
95 assert(alloc_stats
.alloc_count
> 0);
97 std::future
<int> f
= p
.get_future();
101 assert(alloc_stats
.alloc_count
== 0);
105 std::packaged_task
<double(int, char)> p(std::allocator_arg
,
106 bare_allocator
<void>(), A(5));
108 std::future
<double> f
= p
.get_future();
110 assert(f
.get() == 105.0);
111 assert(A::n_copies
== 0);
112 assert(A::n_moves
> 0);
117 std::packaged_task
<double(int, char)> p(std::allocator_arg
,
118 min_allocator
<void>(), A(5));
120 std::future
<double> f
= p
.get_future();
122 assert(f
.get() == 105.0);
123 assert(A::n_copies
== 0);
124 assert(A::n_moves
> 0);