rethrow_exception.pass.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // UNSUPPORTED: libcpp-no-exceptions
  10. // <exception>
  11. // void rethrow_exception [[noreturn]] (exception_ptr p);
  12. #include <exception>
  13. #include <cassert>
  14. struct A
  15. {
  16. static int constructed;
  17. int data_;
  18. A(int data = 0) : data_(data) {++constructed;}
  19. ~A() {--constructed;}
  20. A(const A& a) : data_(a.data_) {++constructed;}
  21. };
  22. int A::constructed = 0;
  23. int main()
  24. {
  25. {
  26. std::exception_ptr p;
  27. try
  28. {
  29. throw A(3);
  30. }
  31. catch (...)
  32. {
  33. p = std::current_exception();
  34. }
  35. try
  36. {
  37. std::rethrow_exception(p);
  38. assert(false);
  39. }
  40. catch (const A& a)
  41. {
  42. assert(A::constructed == 1);
  43. assert(p != nullptr);
  44. p = nullptr;
  45. assert(p == nullptr);
  46. assert(a.data_ == 3);
  47. assert(A::constructed == 1);
  48. }
  49. assert(A::constructed == 0);
  50. }
  51. }