blob: e0f42682b5792a4d99a67dc0b42d38aab00a0be5 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_EXCEPTION
11#define _LIBCPP_EXCEPTION
12
13/*
14 exception synopsis
15
16namespace std
17{
18
19class exception
20{
21public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000022 exception() noexcept;
23 exception(const exception&) noexcept;
24 exception& operator=(const exception&) noexcept;
25 virtual ~exception() noexcept;
26 virtual const char* what() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000027};
28
29class bad_exception
30 : public exception
31{
32public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000033 bad_exception() noexcept;
34 bad_exception(const bad_exception&) noexcept;
35 bad_exception& operator=(const bad_exception&) noexcept;
36 virtual ~bad_exception() noexcept;
37 virtual const char* what() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000038};
39
40typedef void (*unexpected_handler)();
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000041unexpected_handler set_unexpected(unexpected_handler f ) noexcept;
42unexpected_handler get_unexpected() noexcept;
43[[noreturn]] void unexpected();
Howard Hinnantc51e1022010-05-11 19:42:16 +000044
45typedef void (*terminate_handler)();
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000046terminate_handler set_terminate(terminate_handler f ) noexcept;
47terminate_handler get_terminate() noexcept;
48[[noreturn]] void terminate() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000049
Marshall Clow209dacc2015-06-02 15:33:38 +000050bool uncaught_exception() noexcept;
51int uncaught_exceptions() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000052
53typedef unspecified exception_ptr;
54
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000055exception_ptr current_exception() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000056void rethrow_exception [[noreturn]] (exception_ptr p);
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000057template<class E> exception_ptr make_exception_ptr(E e) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000058
59class nested_exception
60{
61public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000062 nested_exception() noexcept;
63 nested_exception(const nested_exception&) noexcept = default;
64 nested_exception& operator=(const nested_exception&) noexcept = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +000065 virtual ~nested_exception() = default;
66
67 // access functions
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000068 [[noreturn]] void rethrow_nested() const;
69 exception_ptr nested_ptr() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000070};
71
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000072template <class T> [[noreturn]] void throw_with_nested(T&& t);
Howard Hinnantc51e1022010-05-11 19:42:16 +000073template <class E> void rethrow_if_nested(const E& e);
74
75} // std
76
77*/
78
Louis Dionneb4fce352022-03-25 12:55:36 -040079#include <__assert> // all public C++ headers provide the assertion handler
Louis Dionne73912b22020-11-04 15:01:25 -050080#include <__availability>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040081#include <__config>
Louis Dionne735bc462021-04-14 13:59:03 -040082#include <__memory/addressof.h>
Nikolas Klauserca7ac532022-12-21 00:07:17 +010083#include <__type_traits/decay.h>
84#include <__type_traits/is_base_of.h>
85#include <__type_traits/is_class.h>
86#include <__type_traits/is_convertible.h>
87#include <__type_traits/is_copy_constructible.h>
88#include <__type_traits/is_final.h>
89#include <__type_traits/is_polymorphic.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +000090#include <cstddef>
Eric Fiselieraae1ccf2016-12-03 03:22:11 +000091#include <cstdlib>
Marshall Clow0a1e7502018-09-12 19:41:40 +000092#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +000093
Paul Kirthdb722162022-08-17 20:57:59 +000094// <vcruntime_exception.h> defines its own std::exception and std::bad_exception types,
95// which we use in order to be ABI-compatible with other STLs on Windows.
Eric Fiselier85f66332019-03-05 01:57:01 +000096#if defined(_LIBCPP_ABI_VCRUNTIME)
Paul Kirthdb722162022-08-17 20:57:59 +000097# include <vcruntime_exception.h>
Eric Fiselierec3a1672017-02-10 08:57:35 +000098#endif
99
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000100#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500101# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000102#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103
104namespace std // purposefully not using versioning namespace
105{
106
Paul Kirthdb722162022-08-17 20:57:59 +0000107#if defined(_LIBCPP_ABI_VCRUNTIME) && (!defined(_HAS_EXCEPTIONS) || _HAS_EXCEPTIONS != 0)
108// The std::exception class was already included above, but we're explicit about this condition here for clarity.
Dimitry Andric47269ce2020-03-13 19:36:26 +0100109
Paul Kirthdb722162022-08-17 20:57:59 +0000110#elif defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
111// However, <vcruntime_exception.h> does not define std::exception and std::bad_exception
112// when _HAS_EXCEPTIONS == 0.
113//
114// Since libc++ still wants to provide the std::exception hierarchy even when _HAS_EXCEPTIONS == 0
115// (after all those are simply types like any other), we define an ABI-compatible version
116// of the VCRuntime std::exception and std::bad_exception types in that mode.
117
118struct __std_exception_data {
119 char const* _What;
120 bool _DoFree;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121};
122
Paul Kirthdb722162022-08-17 20:57:59 +0000123class exception { // base of all library exceptions
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124public:
Nikolas Klauser79567e32022-09-02 16:19:07 +0200125 exception() _NOEXCEPT : __data_() {}
Paul Kirthdb722162022-08-17 20:57:59 +0000126
Nikolas Klauser79567e32022-09-02 16:19:07 +0200127 explicit exception(char const* __message) _NOEXCEPT : __data_() {
128 __data_._What = __message;
129 __data_._DoFree = true;
Paul Kirthdb722162022-08-17 20:57:59 +0000130 }
131
132 exception(exception const&) _NOEXCEPT {}
133
134 exception& operator=(exception const&) _NOEXCEPT { return *this; }
135
136 virtual ~exception() _NOEXCEPT {}
137
Nikolas Klauser79567e32022-09-02 16:19:07 +0200138 virtual char const* what() const _NOEXCEPT { return __data_._What ? __data_._What : "Unknown exception"; }
Paul Kirthdb722162022-08-17 20:57:59 +0000139
140private:
Nikolas Klauser79567e32022-09-02 16:19:07 +0200141 __std_exception_data __data_;
Paul Kirthdb722162022-08-17 20:57:59 +0000142};
143
144class bad_exception : public exception {
145public:
146 bad_exception() _NOEXCEPT : exception("bad exception") {}
147};
148
149#else // !defined(_LIBCPP_ABI_VCRUNTIME)
150// On all other platforms, we define our own std::exception and std::bad_exception types
151// regardless of whether exceptions are turned on as a language feature.
152
153class _LIBCPP_EXCEPTION_ABI exception {
154public:
155 _LIBCPP_INLINE_VISIBILITY exception() _NOEXCEPT {}
156 _LIBCPP_INLINE_VISIBILITY exception(const exception&) _NOEXCEPT = default;
157
158 virtual ~exception() _NOEXCEPT;
159 virtual const char* what() const _NOEXCEPT;
160};
161
162class _LIBCPP_EXCEPTION_ABI bad_exception : public exception {
163public:
164 _LIBCPP_INLINE_VISIBILITY bad_exception() _NOEXCEPT {}
Nikolas Klauseraa3a6cd2022-08-24 02:14:29 +0200165 ~bad_exception() _NOEXCEPT override;
166 const char* what() const _NOEXCEPT override;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167};
Eric Fiselier85f66332019-03-05 01:57:01 +0000168#endif // !_LIBCPP_ABI_VCRUNTIME
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
Eric Fiselierddd77792017-02-17 03:25:08 +0000170#if _LIBCPP_STD_VER <= 14 \
171 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS) \
172 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000173typedef void (*unexpected_handler)();
Howard Hinnant8331b762013-03-06 23:30:19 +0000174_LIBCPP_FUNC_VIS unexpected_handler set_unexpected(unexpected_handler) _NOEXCEPT;
175_LIBCPP_FUNC_VIS unexpected_handler get_unexpected() _NOEXCEPT;
176_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void unexpected();
Eric Fiselierddd77792017-02-17 03:25:08 +0000177#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178
179typedef void (*terminate_handler)();
Howard Hinnant8331b762013-03-06 23:30:19 +0000180_LIBCPP_FUNC_VIS terminate_handler set_terminate(terminate_handler) _NOEXCEPT;
181_LIBCPP_FUNC_VIS terminate_handler get_terminate() _NOEXCEPT;
182_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void terminate() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183
Howard Hinnant8331b762013-03-06 23:30:19 +0000184_LIBCPP_FUNC_VIS bool uncaught_exception() _NOEXCEPT;
Mehdi Amini228053d2017-05-04 17:08:54 +0000185_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS int uncaught_exceptions() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186
Howard Hinnant8331b762013-03-06 23:30:19 +0000187class _LIBCPP_TYPE_VIS exception_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000188
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000189_LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
190_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000192#ifndef _LIBCPP_ABI_MICROSOFT
193
Howard Hinnant8331b762013-03-06 23:30:19 +0000194class _LIBCPP_TYPE_VIS exception_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000195{
196 void* __ptr_;
197public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000198 _LIBCPP_INLINE_VISIBILITY exception_ptr() _NOEXCEPT : __ptr_() {}
199 _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000200
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000201 exception_ptr(const exception_ptr&) _NOEXCEPT;
202 exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
203 ~exception_ptr() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204
Arthur O'Dwyer6c9c9a72021-06-15 12:57:54 -0400205 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const _NOEXCEPT
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000206 {return __ptr_ != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000208 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000209 bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210 {return __x.__ptr_ == __y.__ptr_;}
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000211
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000212 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000213 bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214 {return !(__x == __y);}
215
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000216 friend _LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
217 friend _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218};
219
Howard Hinnantc834c512011-11-29 18:15:50 +0000220template<class _Ep>
Louis Dionnecdc41112018-11-21 17:00:52 +0000221_LIBCPP_INLINE_VISIBILITY exception_ptr
Howard Hinnantc834c512011-11-29 18:15:50 +0000222make_exception_ptr(_Ep __e) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223{
Howard Hinnant72f73582010-08-11 17:04:31 +0000224#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225 try
226 {
227 throw __e;
228 }
229 catch (...)
230 {
231 return current_exception();
232 }
Eric Fiselieraae1ccf2016-12-03 03:22:11 +0000233#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000234 ((void)__e);
Eric Fiselieraae1ccf2016-12-03 03:22:11 +0000235 _VSTD::abort();
236#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237}
238
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000239#else // _LIBCPP_ABI_MICROSOFT
240
241class _LIBCPP_TYPE_VIS exception_ptr
242{
Nikolas Klauser41c59762022-02-14 18:52:28 +0100243_LIBCPP_DIAGNOSTIC_PUSH
244_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wunused-private-field")
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000245 void* __ptr1_;
246 void* __ptr2_;
Nikolas Klauser41c59762022-02-14 18:52:28 +0100247_LIBCPP_DIAGNOSTIC_POP
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000248public:
249 exception_ptr() _NOEXCEPT;
250 exception_ptr(nullptr_t) _NOEXCEPT;
251 exception_ptr(const exception_ptr& __other) _NOEXCEPT;
252 exception_ptr& operator=(const exception_ptr& __other) _NOEXCEPT;
253 exception_ptr& operator=(nullptr_t) _NOEXCEPT;
254 ~exception_ptr() _NOEXCEPT;
Arthur O'Dwyer6c9c9a72021-06-15 12:57:54 -0400255 explicit operator bool() const _NOEXCEPT;
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000256};
257
258_LIBCPP_FUNC_VIS
259bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT;
260
261inline _LIBCPP_INLINE_VISIBILITY
262bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
263 {return !(__x == __y);}
264
265_LIBCPP_FUNC_VIS void swap(exception_ptr&, exception_ptr&) _NOEXCEPT;
266
267_LIBCPP_FUNC_VIS exception_ptr __copy_exception_ptr(void *__except, const void* __ptr);
268_LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
Nikolas Klauser7b8c0502022-07-08 18:17:26 +0200269_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr);
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000270
271// This is a built-in template function which automagically extracts the required
272// information.
273template <class _E> void *__GetExceptionInfo(_E);
274
275template<class _Ep>
Louis Dionnecdc41112018-11-21 17:00:52 +0000276_LIBCPP_INLINE_VISIBILITY exception_ptr
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000277make_exception_ptr(_Ep __e) _NOEXCEPT
278{
279 return __copy_exception_ptr(_VSTD::addressof(__e), __GetExceptionInfo(__e));
280}
281
282#endif // _LIBCPP_ABI_MICROSOFT
Howard Hinnante4f92722010-05-27 17:06:52 +0000283// nested_exception
284
285class _LIBCPP_EXCEPTION_ABI nested_exception
286{
287 exception_ptr __ptr_;
288public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000289 nested_exception() _NOEXCEPT;
290// nested_exception(const nested_exception&) noexcept = default;
291// nested_exception& operator=(const nested_exception&) noexcept = default;
292 virtual ~nested_exception() _NOEXCEPT;
Howard Hinnante4f92722010-05-27 17:06:52 +0000293
294 // access functions
Richard Smithcabd3922012-07-26 02:04:22 +0000295 _LIBCPP_NORETURN void rethrow_nested() const;
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000296 _LIBCPP_INLINE_VISIBILITY exception_ptr nested_ptr() const _NOEXCEPT {return __ptr_;}
Howard Hinnante4f92722010-05-27 17:06:52 +0000297};
298
299template <class _Tp>
300struct __nested
301 : public _Tp,
302 public nested_exception
303{
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000304 _LIBCPP_INLINE_VISIBILITY explicit __nested(const _Tp& __t) : _Tp(__t) {}
Howard Hinnante4f92722010-05-27 17:06:52 +0000305};
306
Howard Hinnant72f73582010-08-11 17:04:31 +0000307#ifndef _LIBCPP_NO_EXCEPTIONS
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000308template <class _Tp, class _Up, bool>
309struct __throw_with_nested;
310
311template <class _Tp, class _Up>
312struct __throw_with_nested<_Tp, _Up, true> {
Louis Dionne16fe2952018-07-11 23:14:33 +0000313 _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000314 __do_throw(_Tp&& __t)
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000315 {
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000316 throw __nested<_Up>(static_cast<_Tp&&>(__t));
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000317 }
318};
319
320template <class _Tp, class _Up>
321struct __throw_with_nested<_Tp, _Up, false> {
Louis Dionne16fe2952018-07-11 23:14:33 +0000322 _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void
Eric Fiselier58e1c912017-04-19 01:35:58 +0000323#ifndef _LIBCPP_CXX03_LANG
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000324 __do_throw(_Tp&& __t)
Eric Fiselier58e1c912017-04-19 01:35:58 +0000325#else
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000326 __do_throw (_Tp& __t)
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400327#endif // _LIBCPP_CXX03_LANG
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000328 {
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000329 throw static_cast<_Tp&&>(__t);
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000330 }
331};
Howard Hinnant72f73582010-08-11 17:04:31 +0000332#endif
Howard Hinnante4f92722010-05-27 17:06:52 +0000333
334template <class _Tp>
Nikolas Klausera9ad6702022-08-13 13:23:16 +0200335_LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000336void
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000337throw_with_nested(_Tp&& __t)
Howard Hinnante4f92722010-05-27 17:06:52 +0000338{
Howard Hinnant72f73582010-08-11 17:04:31 +0000339#ifndef _LIBCPP_NO_EXCEPTIONS
Marshall Clow94a38502017-04-13 16:57:42 +0000340 typedef typename decay<_Tp>::type _Up;
341 static_assert( is_copy_constructible<_Up>::value, "type thrown must be CopyConstructible");
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000342 __throw_with_nested<_Tp, _Up,
343 is_class<_Up>::value &&
344 !is_base_of<nested_exception, _Up>::value &&
345 !__libcpp_is_final<_Up>::value>::
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000346 __do_throw(static_cast<_Tp&&>(__t));
Eric Fiselier6003c772016-12-23 23:37:52 +0000347#else
348 ((void)__t);
349 // FIXME: Make this abort
Howard Hinnant72f73582010-08-11 17:04:31 +0000350#endif
Howard Hinnante4f92722010-05-27 17:06:52 +0000351}
352
Marshall Clowb75c89c2017-03-14 17:08:47 +0000353template <class _From, class _To>
Louis Dionne5083aef2022-03-23 17:02:07 -0400354struct __can_dynamic_cast : _BoolConstant<
Marshall Clowb75c89c2017-03-14 17:08:47 +0000355 is_polymorphic<_From>::value &&
356 (!is_base_of<_To, _From>::value ||
Louis Dionne5083aef2022-03-23 17:02:07 -0400357 is_convertible<const _From*, const _To*>::value)> {};
Marshall Clowb75c89c2017-03-14 17:08:47 +0000358
Howard Hinnantc834c512011-11-29 18:15:50 +0000359template <class _Ep>
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000360inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante4f92722010-05-27 17:06:52 +0000361void
Marshall Clowb75c89c2017-03-14 17:08:47 +0000362rethrow_if_nested(const _Ep& __e,
Nikolas Klauser7537c9c2022-07-04 01:21:44 +0200363 __enable_if_t< __can_dynamic_cast<_Ep, nested_exception>::value>* = 0)
Howard Hinnante4f92722010-05-27 17:06:52 +0000364{
Marshall Clowe993abe2015-12-14 18:01:56 +0000365 const nested_exception* __nep = dynamic_cast<const nested_exception*>(_VSTD::addressof(__e));
Howard Hinnant3615a192010-05-28 13:35:41 +0000366 if (__nep)
367 __nep->rethrow_nested();
Howard Hinnante4f92722010-05-27 17:06:52 +0000368}
369
Howard Hinnantc834c512011-11-29 18:15:50 +0000370template <class _Ep>
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000371inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante4f92722010-05-27 17:06:52 +0000372void
Marshall Clowb75c89c2017-03-14 17:08:47 +0000373rethrow_if_nested(const _Ep&,
Nikolas Klauser7537c9c2022-07-04 01:21:44 +0200374 __enable_if_t<!__can_dynamic_cast<_Ep, nested_exception>::value>* = 0)
Howard Hinnante4f92722010-05-27 17:06:52 +0000375{
376}
377
Nikolas Klauserd26407a2021-12-02 14:12:51 +0100378} // namespace std
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Nikolas Klauserca7ac532022-12-21 00:07:17 +0100380#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
381# include <type_traits>
382#endif
383
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400384#endif // _LIBCPP_EXCEPTION