Home | History | Annotate | Download | only in futures.task.members
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // UNSUPPORTED: libcpp-has-no-threads
     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 class A
     23 {
     24     long data_;
     25 
     26 public:
     27     static int n_moves;
     28     static int n_copies;
     29 
     30     explicit A(long i) : data_(i) {}
     31     A(A&& a) : data_(a.data_) {++n_moves; a.data_ = -1;}
     32     A(const A& a) : data_(a.data_) {++n_copies;}
     33 
     34     long operator()(long i, long j) const {return data_ + i + j;}
     35 };
     36 
     37 int A::n_moves = 0;
     38 int A::n_copies = 0;
     39 
     40 int func(int i) { return i; }
     41 
     42 int main()
     43 {
     44     {
     45         std::packaged_task<double(int, char)> p(A(5));
     46         assert(p.valid());
     47         std::future<double> f = p.get_future();
     48         p(3, 'a');
     49         assert(f.get() == 105.0);
     50         assert(A::n_copies == 0);
     51         assert(A::n_moves > 0);
     52     }
     53     A::n_copies = 0;
     54     A::n_copies = 0;
     55     {
     56         A a(5);
     57         std::packaged_task<double(int, char)> p(a);
     58         assert(p.valid());
     59         std::future<double> f = p.get_future();
     60         p(3, 'a');
     61         assert(f.get() == 105.0);
     62         assert(A::n_copies > 0);
     63         assert(A::n_moves > 0);
     64     }
     65     {
     66         std::packaged_task<int(int)> p(&func);
     67         assert(p.valid());
     68         std::future<int> f = p.get_future();
     69         p(4);
     70         assert(f.get() == 4);
     71     }
     72     {
     73         std::packaged_task<int(int)> p(func);
     74         assert(p.valid());
     75         std::future<int> f = p.get_future();
     76         p(4);
     77         assert(f.get() == 4);
     78     }
     79 }
     80