diff --git a/libcxx/include/future b/libcxx/include/future --- a/libcxx/include/future +++ b/libcxx/include/future @@ -44,14 +44,17 @@ const error_category& future_category() noexcept; -class future_error - : public logic_error +class future_error : public logic_error { public: future_error(error_code ec); // exposition only - explicit future_error(future_errc); // C++17 + explicit future_error(future_errc e); // C++17 + const error_code& code() const noexcept; const char* what() const noexcept; + +private: + error_code ec_; // exposition only }; template @@ -499,10 +502,15 @@ : public logic_error { error_code __ec_; +#if _LIBCPP_STD_VER <= 14 public: +#endif future_error(error_code __ec); -#if _LIBCPP_STD_VERS > 14 - explicit future_error(future_errc _Ev) : logic_error(), __ec_(make_error_code(_Ev)) {} + +public: +#if _LIBCPP_STD_VER > 14 + explicit future_error(future_errc _Ev) + : future_error(_VSTD::make_error_code(_Ev)) {} #endif _LIBCPP_INLINE_VISIBILITY const error_code& code() const _NOEXCEPT {return __ec_;} @@ -518,7 +526,7 @@ void __throw_future_error(future_errc _Ev) { #ifndef _LIBCPP_NO_EXCEPTIONS - throw future_error(make_error_code(_Ev)); + throw future_error(_Ev); #else ((void)_Ev); _VSTD::abort(); @@ -1339,7 +1347,7 @@ { if (!__state_->__has_value() && __state_->use_count() > 1) __state_->set_exception(make_exception_ptr( - future_error(make_error_code(future_errc::broken_promise)) + future_error(future_errc::broken_promise) )); __state_->__release_shared(); } @@ -1482,7 +1490,7 @@ { if (!__state_->__has_value() && __state_->use_count() > 1) __state_->set_exception(make_exception_ptr( - future_error(make_error_code(future_errc::broken_promise)) + future_error(future_errc::broken_promise) )); __state_->__release_shared(); } diff --git a/libcxx/src/future.cpp b/libcxx/src/future.cpp --- a/libcxx/src/future.cpp +++ b/libcxx/src/future.cpp @@ -207,9 +207,8 @@ { #ifndef _LIBCPP_NO_EXCEPTIONS if (!__state_->__has_value() && __state_->use_count() > 1) - __state_->set_exception(make_exception_ptr( - future_error(make_error_code(future_errc::broken_promise)) - )); + __state_->set_exception( + make_exception_ptr(future_error(future_errc::broken_promise))); #endif // _LIBCPP_NO_EXCEPTIONS __state_->__release_shared(); } diff --git a/libcxx/test/std/thread/futures/futures.future_error/code.pass.cpp b/libcxx/test/std/thread/futures/futures.future_error/code.pass.cpp --- a/libcxx/test/std/thread/futures/futures.future_error/code.pass.cpp +++ b/libcxx/test/std/thread/futures/futures.future_error/code.pass.cpp @@ -11,10 +11,10 @@ // // class future_error -// future_error(error_code __ec); // exposition only -// explicit future_error(future_errc _Ev) : __ec_(make_error_code(_Ev)) {} // C++17 +// future_error(error_code ec); // exposition only +// explicit future_error(future_errc e); // C++17 -// const error_code& code() const throw(); +// const error_code& code() const noexcept; #include #include @@ -23,6 +23,7 @@ int main(int, char**) { +#if TEST_STD_VER <= 14 { std::error_code ec = std::make_error_code(std::future_errc::broken_promise); std::future_error f(ec); @@ -43,7 +44,7 @@ std::future_error f(ec); assert(f.code() == ec); } -#if TEST_STD_VER > 14 +#else { std::future_error f(std::future_errc::broken_promise); assert(f.code() == std::make_error_code(std::future_errc::broken_promise)); diff --git a/libcxx/test/std/thread/futures/futures.future_error/ctor.fail.cpp b/libcxx/test/std/thread/futures/futures.future_error/ctor.fail.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/thread/futures/futures.future_error/ctor.fail.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++03, c++11, c++14 +// UNSUPPORTED: libcpp-has-no-threads + +// + +// class future_error +// explicit future_error(future_errc e); // C++17 + +#include + +int main(int, char**) { + auto test_explicit = [](std::future_errc e) -> std::future_error { + return {e}; + // expected-error@-1 {{chosen constructor is explicit in copy-initialization}} + }; + + std::error_code ec = std::make_error_code(std::future_errc::no_state); + std::future_error f(ec); // expected-error {{calling a private constructor}} + + return 0; +} diff --git a/libcxx/test/std/thread/futures/futures.future_error/what.pass.cpp b/libcxx/test/std/thread/futures/futures.future_error/what.pass.cpp --- a/libcxx/test/std/thread/futures/futures.future_error/what.pass.cpp +++ b/libcxx/test/std/thread/futures/futures.future_error/what.pass.cpp @@ -19,7 +19,7 @@ // class future_error -// const char* what() const throw(); +// const char* what() const noexcept; #include #include @@ -30,21 +30,21 @@ int main(int, char**) { { - std::future_error f(std::make_error_code(std::future_errc::broken_promise)); + std::future_error f(std::future_errc::broken_promise); LIBCPP_ASSERT(std::strcmp(f.what(), "The associated promise has been destructed prior " "to the associated state becoming ready.") == 0); } { - std::future_error f(std::make_error_code(std::future_errc::future_already_retrieved)); + std::future_error f(std::future_errc::future_already_retrieved); LIBCPP_ASSERT(std::strcmp(f.what(), "The future has already been retrieved from " "the promise or packaged_task.") == 0); } { - std::future_error f(std::make_error_code(std::future_errc::promise_already_satisfied)); + std::future_error f(std::future_errc::promise_already_satisfied); LIBCPP_ASSERT(std::strcmp(f.what(), "The state of the promise has already been set.") == 0); } { - std::future_error f(std::make_error_code(std::future_errc::no_state)); + std::future_error f(std::future_errc::no_state); LIBCPP_ASSERT(std::strcmp(f.what(), "Operation not permitted on an object without " "an associated state.") == 0); }