rethrow_if_nested.pass.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // class nested_exception;
  12. // template <class E> void rethrow_if_nested(const E& e);
  13. #include <exception>
  14. #include <cstdlib>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. class A
  18. {
  19. int data_;
  20. public:
  21. explicit A(int data) : data_(data) {}
  22. virtual ~A() TEST_NOEXCEPT {}
  23. friend bool operator==(const A& x, const A& y) {return x.data_ == y.data_;}
  24. };
  25. class B
  26. : public std::nested_exception,
  27. public A
  28. {
  29. public:
  30. explicit B(int data) : A(data) {}
  31. B(const B& b) : A(b) {}
  32. };
  33. class C
  34. {
  35. public:
  36. virtual ~C() {}
  37. C * operator&() const { assert(false); } // should not be called
  38. };
  39. int main()
  40. {
  41. {
  42. try
  43. {
  44. A a(3);
  45. std::rethrow_if_nested(a);
  46. assert(true);
  47. }
  48. catch (...)
  49. {
  50. assert(false);
  51. }
  52. }
  53. {
  54. try
  55. {
  56. throw B(5);
  57. }
  58. catch (const B& b)
  59. {
  60. try
  61. {
  62. throw b;
  63. }
  64. catch (const A& a)
  65. {
  66. try
  67. {
  68. std::rethrow_if_nested(a);
  69. assert(false);
  70. }
  71. catch (const B& b2)
  72. {
  73. assert(b2 == B(5));
  74. }
  75. }
  76. }
  77. }
  78. {
  79. try
  80. {
  81. std::rethrow_if_nested(C());
  82. assert(true);
  83. }
  84. catch (...)
  85. {
  86. assert(false);
  87. }
  88. }
  89. }