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 // UNSUPPORTED: c++98, c++03
     12 
     13 // <future>
     14 
     15 // class packaged_task<R(ArgTypes...)>
     16 
     17 // template <class F>
     18 //     explicit packaged_task(F&& f);
     19 
     20 #include <future>
     21 #include <cassert>
     22 
     23 class A
     24 {
     25     long data_;
     26 
     27 public:
     28     static int n_moves;
     29     static int n_copies;
     30 
     31     explicit A(long i) : data_(i) {}
     32     A(A&& a) : data_(a.data_) {++n_moves; a.data_ = -1;}
     33     A(const A& a) : data_(a.data_) {++n_copies;}
     34 
     35     long operator()(long i, long j) const {return data_ + i + j;}
     36 };
     37 
     38 int A::n_moves = 0;
     39 int A::n_copies = 0;
     40 
     41 int func(int i) { return i; }
     42 
     43 int main()
     44 {
     45     {
     46         std::packaged_task<double(int, char)> p(A(5));
     47         assert(p.valid());
     48         std::future<double> f = p.get_future();
     49         p(3, 'a');
     50         assert(f.get() == 105.0);
     51         assert(A::n_copies == 0);
     52         assert(A::n_moves > 0);
     53     }
     54     A::n_copies = 0;
     55     A::n_copies = 0;
     56     {
     57         A a(5);
     58         std::packaged_task<double(int, char)> p(a);
     59         assert(p.valid());
     60         std::future<double> f = p.get_future();
     61         p(3, 'a');
     62         assert(f.get() == 105.0);
     63         assert(A::n_copies > 0);
     64         assert(A::n_moves > 0);
     65     }
     66     {
     67         std::packaged_task<int(int)> p(&func);
     68         assert(p.valid());
     69         std::future<int> f = p.get_future();
     70         p(4);
     71         assert(f.get() == 4);
     72     }
     73     {
     74         std::packaged_task<int(int)> p(func);
     75         assert(p.valid());
     76         std::future<int> f = p.get_future();
     77         p(4);
     78         assert(f.get() == 4);
     79     }
     80 }
     81