terminate_on_exception.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___UTILITY_TERMINATE_ON_EXCEPTION_H
  9. #define _LIBCPP___UTILITY_TERMINATE_ON_EXCEPTION_H
  10. #include <__config>
  11. #include <__exception/terminate.h>
  12. #include <new>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. #if _LIBCPP_STD_VER >= 17
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  19. template <class _Func>
  20. _LIBCPP_HIDE_FROM_ABI auto __terminate_on_exception(_Func __func) {
  21. try {
  22. return __func();
  23. } catch (...) {
  24. std::terminate();
  25. }
  26. }
  27. # else // _LIBCPP_HAS_NO_EXCEPTIONS
  28. template <class _Func>
  29. _LIBCPP_HIDE_FROM_ABI auto __terminate_on_exception(_Func __func) {
  30. return __func();
  31. }
  32. # endif // _LIBCPP_HAS_NO_EXCEPTIONS
  33. _LIBCPP_END_NAMESPACE_STD
  34. #endif // _LIBCPP_STD_VER >= 17
  35. #endif // _LIBCPP___UTILITY_TERMINATE_ON_EXCEPTION_H