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