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