Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // UNSUPPORTED: libcpp-has-no-threads |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 11 | |
| 12 | // <future> |
| 13 | |
| 14 | // class packaged_task<R(ArgTypes...)> |
| 15 | |
| 16 | // future<R> get_future(); |
| 17 | |
| 18 | #include <future> |
| 19 | #include <cassert> |
| 20 | |
| 21 | class A |
| 22 | { |
| 23 | long data_; |
| 24 | |
| 25 | public: |
| 26 | explicit A(long i) : data_(i) {} |
| 27 | |
| 28 | long operator()(long i, long j) const {return data_ + i + j;} |
| 29 | }; |
| 30 | |
| 31 | int main() |
| 32 | { |
| 33 | { |
| 34 | std::packaged_task<double(int, char)> p(A(5)); |
| 35 | std::future<double> f = p.get_future(); |
| 36 | p(3, 'a'); |
| 37 | assert(f.get() == 105.0); |
| 38 | } |
| 39 | { |
| 40 | std::packaged_task<double(int, char)> p(A(5)); |
| 41 | std::future<double> f = p.get_future(); |
| 42 | try |
| 43 | { |
| 44 | f = p.get_future(); |
| 45 | assert(false); |
| 46 | } |
| 47 | catch (const std::future_error& e) |
| 48 | { |
| 49 | assert(e.code() == make_error_code(std::future_errc::future_already_retrieved)); |
| 50 | } |
| 51 | } |
| 52 | { |
| 53 | std::packaged_task<double(int, char)> p; |
| 54 | try |
| 55 | { |
| 56 | std::future<double> f = p.get_future(); |
| 57 | assert(false); |
| 58 | } |
| 59 | catch (const std::future_error& e) |
| 60 | { |
| 61 | assert(e.code() == make_error_code(std::future_errc::no_state)); |
| 62 | } |
| 63 | } |
| 64 | } |