15
15
#include < atomic>
16
16
#include < cassert>
17
17
#include < exception>
18
+ #include < functional>
18
19
#include < initializer_list>
19
20
#include < memory>
20
21
#include < mutex>
@@ -567,34 +568,28 @@ class packaged_task {
567
568
packaged_task () = default ;
568
569
569
570
~packaged_task () {
570
- auto p = _p.lock ();
571
- if (p) p->remove_promise ();
571
+ if (auto p = _p.lock ()) p->remove_promise ();
572
572
}
573
573
574
574
packaged_task (const packaged_task& x) : _p(x._p) {
575
- auto p = _p.lock ();
576
- if (p) p->add_promise ();
575
+ if (auto p = _p.lock ()) p->add_promise ();
577
576
}
578
577
579
578
packaged_task (packaged_task&&) noexcept = default ;
580
- packaged_task& operator =(const packaged_task& x) {
581
- auto tmp = x;
582
- *this = std::move (tmp);
583
- return *this ;
584
- }
579
+
580
+ packaged_task& operator =(const packaged_task& x) { return *this = packaged_task{x}; }
581
+
585
582
packaged_task& operator =(packaged_task&& x) noexcept = default ;
586
583
587
584
template <typename ... A>
588
585
void operator ()(A&&... args) const noexcept {
589
- auto p = _p.lock ();
590
- if (p) (*p)(std::forward<A>(args)...);
586
+ if (auto p = _p.lock ()) (*p)(std::forward<A>(args)...);
591
587
}
592
588
593
589
bool canceled () const { return _p.expired (); }
594
590
595
591
void set_exception (std::exception_ptr error) const {
596
- auto p = _p.lock ();
597
- if (p) p->set_error (std::move (error));
592
+ if (auto p = _p.lock ()) p->set_error (std::move (error));
598
593
}
599
594
};
600
595
@@ -636,7 +631,7 @@ class STLAB_NODISCARD() future<T, enable_if_copyable<T>> {
636
631
637
632
template <typename F>
638
633
auto then (F&& f) const & {
639
- return recover ([_f = std::forward<F>(f)](future<result_type>&& p) {
634
+ return recover ([_f = std::forward<F>(f)](future<result_type>&& p) mutable {
640
635
return std::move (_f)(*std::move (p).get_try ());
641
636
});
642
637
}
@@ -649,7 +644,7 @@ class STLAB_NODISCARD() future<T, enable_if_copyable<T>> {
649
644
template <typename E, typename F>
650
645
auto then (E&& executor, F&& f) const & {
651
646
return recover (std::forward<E>(executor),
652
- [_f = std::forward<F>(f)](future<result_type>&& p) {
647
+ [_f = std::forward<F>(f)](future<result_type>&& p) mutable {
653
648
return std::move (_f)(*std::move (p).get_try ());
654
649
});
655
650
}
@@ -781,7 +776,7 @@ class STLAB_NODISCARD() future<void, void> {
781
776
782
777
template <typename F>
783
778
auto then (F&& f) const & {
784
- return recover ([_f = std::forward<F>(f)](future<result_type>&& p) {
779
+ return recover ([_f = std::forward<F>(f)](future<result_type>&& p) mutable {
785
780
std::move (p).get_try ();
786
781
return std::move (_f)();
787
782
});
@@ -795,7 +790,7 @@ class STLAB_NODISCARD() future<void, void> {
795
790
template <typename E, typename F>
796
791
auto then (E&& executor, F&& f) const & {
797
792
return recover (std::forward<E>(executor),
798
- [_f = std::forward<F>(f)](future<result_type>&& p) {
793
+ [_f = std::forward<F>(f)](future<result_type>&& p) mutable {
799
794
(void )std::move (p).get_try ();
800
795
return std::move (_f)();
801
796
});
@@ -1244,7 +1239,7 @@ struct make_when_any {
1244
1239
using result_t = detail::result_t <F, T, size_t >;
1245
1240
1246
1241
auto shared = std::make_shared<detail::when_any_shared<sizeof ...(Ts) + 1 , T>>();
1247
- auto p = package<result_t ()>(executor, [_f = std::move (f), _p = shared] {
1242
+ auto p = package<result_t ()>(executor, [_f = std::move (f), _p = shared]() mutable {
1248
1243
return detail::apply_when_any_arg (_f, _p);
1249
1244
});
1250
1245
shared->_f = std::move (p.first );
@@ -1264,7 +1259,7 @@ struct make_when_any<void> {
1264
1259
using result_t = detail::result_t <F, size_t >;
1265
1260
1266
1261
auto shared = std::make_shared<detail::when_any_shared<sizeof ...(Ts), void >>();
1267
- auto p = package<result_t ()>(executor, [_f = std::forward<F>(f), _p = shared] {
1262
+ auto p = package<result_t ()>(executor, [_f = std::forward<F>(f), _p = shared]() mutable {
1268
1263
return detail::apply_when_any_arg (_f, _p);
1269
1264
});
1270
1265
shared->_f = std::move (p.first );
@@ -1566,7 +1561,7 @@ struct _reduce_coroutine : std::enable_shared_from_this<_reduce_coroutine<P, R>>
1566
1561
_this->_promise .set_exception (e);
1567
1562
return ;
1568
1563
}
1569
- _this->stage_0 (std::move (a));
1564
+ _this->stage_0 (std::forward< decltype (a)> (a));
1570
1565
});
1571
1566
}
1572
1567
void stage_0 (future<future<R>>&& r) {
@@ -1672,12 +1667,13 @@ auto async(E executor, F&& f, Args&&... args)
1672
1667
using result_type = detail::result_t <std::decay_t <F>, std::decay_t <Args>...>;
1673
1668
1674
1669
auto p = package<result_type ()>(
1675
- executor, std::bind<result_type>(
1676
- [_f = std::forward<F>(f)](
1677
- unwrap_reference_t <std::decay_t <Args>>&... args) mutable -> result_type {
1678
- return _f (move_if<!is_reference_wrapper_v<std::decay_t <Args>>>(args)...);
1679
- },
1680
- std::forward<Args>(args)...));
1670
+ executor,
1671
+ std::bind<result_type>(
1672
+ [_f = std::forward<F>(f)](
1673
+ unwrap_reference_t <std::decay_t <Args>>&... args) mutable -> result_type {
1674
+ return std::move (_f)(move_if<!is_reference_wrapper_v<std::decay_t <Args>>>(args)...);
1675
+ },
1676
+ std::forward<Args>(args)...));
1681
1677
1682
1678
executor (std::move (p.first ));
1683
1679
@@ -1813,9 +1809,7 @@ struct std::coroutine_traits<stlab::future<T>, Args...> {
1813
1809
std::pair<stlab::packaged_task<T>, stlab::future<T>> _promise;
1814
1810
1815
1811
promise_type () {
1816
- _promise = stlab::package<T (T)>(stlab::immediate_executor, [](auto && x) -> decltype (x) {
1817
- return std::forward<decltype (x)>(x);
1818
- });
1812
+ _promise = stlab::package<T (T)>(stlab::immediate_executor, std::identity{});
1819
1813
}
1820
1814
1821
1815
stlab::future<T> get_return_object () { return std::move (_promise.second ); }
0 commit comments