Skip to content

Commit

Permalink
Fix the signatures of std::rethrow_if_nested
Browse files Browse the repository at this point in the history
  • Loading branch information
frederick-vs-ja committed Jul 1, 2024
1 parent d9e659c commit bb1e930
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
10 changes: 4 additions & 6 deletions libcxx/include/__exception/nested_exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,15 @@ struct __can_dynamic_cast
: _BoolConstant< is_polymorphic<_From>::value &&
(!is_base_of<_To, _From>::value || is_convertible<const _From*, const _To*>::value)> {};

template <class _Ep>
inline _LIBCPP_HIDE_FROM_ABI void
rethrow_if_nested(const _Ep& __e, __enable_if_t< __can_dynamic_cast<_Ep, nested_exception>::value>* = 0) {
template <class _Ep, __enable_if_t< __can_dynamic_cast<_Ep, nested_exception>::value, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI void rethrow_if_nested(const _Ep& __e) {
const nested_exception* __nep = dynamic_cast<const nested_exception*>(std::addressof(__e));
if (__nep)
__nep->rethrow_nested();
}

template <class _Ep>
inline _LIBCPP_HIDE_FROM_ABI void
rethrow_if_nested(const _Ep&, __enable_if_t<!__can_dynamic_cast<_Ep, nested_exception>::value>* = 0) {}
template <class _Ep, __enable_if_t<!__can_dynamic_cast<_Ep, nested_exception>::value, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI void rethrow_if_nested(const _Ep&) {}

} // namespace std

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
// template <class E> void rethrow_if_nested(const E& e);

#include <exception>
#include <cstddef>
#include <cstdlib>
#include <cassert>
#include <utility>

#include "test_macros.h"

Expand Down Expand Up @@ -58,6 +60,31 @@ class E1 : public std::nested_exception {};
class E2 : public std::nested_exception {};
class E : public E1, public E2 {};

#if TEST_STD_VER >= 11
template <class, class...>
struct can_rethrow_if_nested_impl {
static constexpr bool value = false;
};

template <class... Args>
struct can_rethrow_if_nested_impl<decltype((void)std::rethrow_if_nested(std::declval<Args>()...)), Args...> {
static constexpr bool value = true;
};

template <class... Args>
struct can_rethrow_if_nested : can_rethrow_if_nested_impl<void, Args...> {};

static_assert(!can_rethrow_if_nested<>::value, "");
static_assert(can_rethrow_if_nested<A>::value, "");
static_assert(can_rethrow_if_nested<const A&>::value, "");
static_assert(can_rethrow_if_nested<B>::value, "");
static_assert(can_rethrow_if_nested<const B&>::value, "");
static_assert(!can_rethrow_if_nested<A, int*>::value, "");
static_assert(!can_rethrow_if_nested<B, int*>::value, "");
static_assert(!can_rethrow_if_nested<A, std::nullptr_t>::value, "");
static_assert(!can_rethrow_if_nested<B, std::nullptr_t>::value, "");
#endif

int main(int, char**)
{
{
Expand Down

0 comments on commit bb1e930

Please sign in to comment.