libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

dtor.pass.cpp (1539B)


      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 // ~packaged_task();
     18 
     19 #include <future>
     20 #include <cassert>
     21 
     22 #include "test_macros.h"
     23 
     24 class A
     25 {
     26     long data_;
     27 
     28 public:
     29     explicit A(long i) : data_(i) {}
     30 
     31     long operator()(long i, long j) const {return data_ + i + j;}
     32 };
     33 
     34 void func(std::packaged_task<double(int, char)>)
     35 {
     36 }
     37 
     38 void func2(std::packaged_task<double(int, char)> p)
     39 {
     40     p(3, 'a');
     41 }
     42 
     43 int main()
     44 {
     45 #ifndef TEST_HAS_NO_EXCEPTIONS
     46     {
     47         std::packaged_task<double(int, char)> p(A(5));
     48         std::future<double> f = p.get_future();
     49         std::thread(func, std::move(p)).detach();
     50         try
     51         {
     52             double i = f.get();
     53             ((void)i); // Prevent unused warning
     54             assert(false);
     55         }
     56         catch (const std::future_error& e)
     57         {
     58             assert(e.code() == make_error_code(std::future_errc::broken_promise));
     59         }
     60     }
     61 #endif
     62     {
     63         std::packaged_task<double(int, char)> p(A(5));
     64         std::future<double> f = p.get_future();
     65         std::thread(func2, std::move(p)).detach();
     66         assert(f.get() == 105.0);
     67     }
     68 }