65 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// UNSUPPORTED: no-threads10// UNSUPPORTED: c++0311 12// <future>13 14// class packaged_task<R(ArgTypes...)>15 16// void reset();17 18#include <future>19#include <cassert>20 21#include "test_macros.h"22 23class A24{25 long data_;26 27public:28 explicit A(long i) : data_(i) {}29 30 long operator()(long i, long j) const31 {32 return data_ + i + j;33 }34};35 36int main(int, char**)37{38 {39 std::packaged_task<double(int, char)> p(A(5));40 std::future<double> f = p.get_future();41 p(3, 97);42 assert(f.get() == 105.0);43 p.reset();44 p(4, 97);45 f = p.get_future();46 assert(f.get() == 106.0);47 }48#ifndef TEST_HAS_NO_EXCEPTIONS49 {50 std::packaged_task<double(int, char)> p;51 try52 {53 p.reset();54 assert(false);55 }56 catch (const std::future_error& e)57 {58 assert(e.code() == make_error_code(std::future_errc::no_state));59 }60 }61#endif62 63 return 0;64}65