Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<optional>: Fix exception specification for optional's transform-construction #3668

Merged
6 changes: 3 additions & 3 deletions stl/inc/optional
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct _Optional_destruct_base { // either contains a value of _Ty or is empty (
#if _HAS_CXX23
template <class _Fn, class _Ux>
constexpr _Optional_destruct_base(_Construct_from_invoke_result_tag, _Fn&& _Func, _Ux&& _Arg) noexcept(
is_nothrow_constructible_v<_Ty, invoke_result_t<_Fn, _Ux>>)
noexcept(static_cast<_Ty>(_STD invoke(_STD forward<_Fn>(_Func), _STD forward<_Ux>(_Arg)))))
: _Value(_STD invoke(_STD forward<_Fn>(_Func), _STD forward<_Ux>(_Arg))), _Has_value{true} {}
#endif // _HAS_CXX23

Expand Down Expand Up @@ -118,7 +118,7 @@ struct _Optional_destruct_base<_Ty, false> { // either contains a value of _Ty o
#if _HAS_CXX23
template <class _Fn, class _Ux>
constexpr _Optional_destruct_base(_Construct_from_invoke_result_tag, _Fn&& _Func, _Ux&& _Arg) noexcept(
is_nothrow_constructible_v<_Ty, invoke_result_t<_Fn, _Ux>>)
noexcept(static_cast<_Ty>(_STD invoke(_STD forward<_Fn>(_Func), _STD forward<_Ux>(_Arg)))))
: _Value(_STD invoke(_STD forward<_Fn>(_Func), _STD forward<_Ux>(_Arg))), _Has_value{true} {}
#endif // _HAS_CXX23

Expand Down Expand Up @@ -277,7 +277,7 @@ public:
#if _HAS_CXX23
template <class _Fn, class _Ux>
constexpr optional(_Construct_from_invoke_result_tag _Tag, _Fn&& _Func, _Ux&& _Arg) noexcept(
is_nothrow_constructible_v<_Ty, invoke_result_t<_Fn, _Ux>>)
noexcept(static_cast<_Ty>(_STD invoke(_STD forward<_Fn>(_Func), _STD forward<_Ux>(_Arg)))))
: _Mybase(_Tag, _STD forward<_Fn>(_Func), _STD forward<_Ux>(_Arg)) {}
#endif // _HAS_CXX23

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <cassert>
#include <exception>
#include <optional>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -109,7 +110,23 @@ constexpr bool test() {
return true;
}

void test_gh3667() {
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
class unique_exception : public exception {};

try {
optional<int> opt(1);
opt.transform([](int) -> int { throw unique_exception{}; });
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
} catch (const unique_exception&) {
return;
} catch (...) {
assert(false); // shouldn't terminate or reach here
}
assert(false); // shouldn't terminate or reach here
}

int main() {
test();
static_assert(test());

test_gh3667();
}