
Summary: This patch marks *most* tests for `std::promise`, `std::future` and `std::shared_future` as unsupported in C++03. These tests fail in C++03 mode because they attempt to copy a `std::future` even though it is a `MoveOnly` type. AFAIK the missing move-semantics in `std::future` is the only reason these tests fail but without move semantics these classes are useless. For example even though `std::promise::set_value` and `std::promise::set_exception(...)` work in C++03 `std::promise` is still useless because we cannot call `std::promise::get_future(...)`. It might be possible to hack `std::move(...)` like we do for `std::unique_ptr` to make the move semantics work but I don't think it is worth the effort. Instead I think we should leave the `<future>` header as-is and mark the failing tests as `UNSUPPORTED`. I don't believe there are any users of `std::future` or `std::promise` in C++03 because they are so unusable. Therefore I am not concerned about losing test coverage and possibly breaking users. However because there are still parts of `<future>` that work in C++03 it would be wrong to `#ifdef` out the entire header. @mclow.lists Should we take further steps to prevent the use of `std::promise`, `std::future` and `std::shared_future` in C++03? Note: This patch also cleans up the tests and converts them to use `support/test_allocator.h` instead of a duplicate class in `test/std/futures/test_allocator.h`. Reviewers: mclow.lists Subscribers: vsk, mclow.lists, cfe-commits Differential Revision: http://reviews.llvm.org/D12135 git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@246271 91177308-0d34-0410-b5e6-96231b3b80d8
147 lines
3.5 KiB
C++
147 lines
3.5 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is dual licensed under the MIT and the University of Illinois Open
|
|
// Source Licenses. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// UNSUPPORTED: libcpp-has-no-threads
|
|
// UNSUPPORTED: c++98, c++03
|
|
|
|
// <future>
|
|
|
|
// class shared_future<R>
|
|
|
|
// const R& shared_future::get();
|
|
// R& shared_future<R&>::get();
|
|
// void shared_future<void>::get();
|
|
|
|
#include <future>
|
|
#include <cassert>
|
|
|
|
void func1(std::promise<int> p)
|
|
{
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
p.set_value(3);
|
|
}
|
|
|
|
void func2(std::promise<int> p)
|
|
{
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
p.set_exception(std::make_exception_ptr(3));
|
|
}
|
|
|
|
int j = 0;
|
|
|
|
void func3(std::promise<int&> p)
|
|
{
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
j = 5;
|
|
p.set_value(j);
|
|
}
|
|
|
|
void func4(std::promise<int&> p)
|
|
{
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
p.set_exception(std::make_exception_ptr(3.5));
|
|
}
|
|
|
|
void func5(std::promise<void> p)
|
|
{
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
p.set_value();
|
|
}
|
|
|
|
void func6(std::promise<void> p)
|
|
{
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
p.set_exception(std::make_exception_ptr('c'));
|
|
}
|
|
|
|
int main()
|
|
{
|
|
{
|
|
typedef int T;
|
|
{
|
|
std::promise<T> p;
|
|
std::shared_future<T> f = p.get_future();
|
|
std::thread(func1, std::move(p)).detach();
|
|
assert(f.valid());
|
|
assert(f.get() == 3);
|
|
assert(f.valid());
|
|
}
|
|
{
|
|
std::promise<T> p;
|
|
std::shared_future<T> f = p.get_future();
|
|
std::thread(func2, std::move(p)).detach();
|
|
try
|
|
{
|
|
assert(f.valid());
|
|
assert(f.get() == 3);
|
|
assert(false);
|
|
}
|
|
catch (int i)
|
|
{
|
|
assert(i == 3);
|
|
}
|
|
assert(f.valid());
|
|
}
|
|
}
|
|
{
|
|
typedef int& T;
|
|
{
|
|
std::promise<T> p;
|
|
std::shared_future<T> f = p.get_future();
|
|
std::thread(func3, std::move(p)).detach();
|
|
assert(f.valid());
|
|
assert(f.get() == 5);
|
|
assert(f.valid());
|
|
}
|
|
{
|
|
std::promise<T> p;
|
|
std::shared_future<T> f = p.get_future();
|
|
std::thread(func4, std::move(p)).detach();
|
|
try
|
|
{
|
|
assert(f.valid());
|
|
assert(f.get() == 3);
|
|
assert(false);
|
|
}
|
|
catch (double i)
|
|
{
|
|
assert(i == 3.5);
|
|
}
|
|
assert(f.valid());
|
|
}
|
|
}
|
|
{
|
|
typedef void T;
|
|
{
|
|
std::promise<T> p;
|
|
std::shared_future<T> f = p.get_future();
|
|
std::thread(func5, std::move(p)).detach();
|
|
assert(f.valid());
|
|
f.get();
|
|
assert(f.valid());
|
|
}
|
|
{
|
|
std::promise<T> p;
|
|
std::shared_future<T> f = p.get_future();
|
|
std::thread(func6, std::move(p)).detach();
|
|
try
|
|
{
|
|
assert(f.valid());
|
|
f.get();
|
|
assert(false);
|
|
}
|
|
catch (char i)
|
|
{
|
|
assert(i == 'c');
|
|
}
|
|
assert(f.valid());
|
|
}
|
|
}
|
|
}
|