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 10 // XFAIL: libcpp-no-exceptions 11 // <exception> 12 13 // class nested_exception; 14 15 // template<class T> void throw_with_nested [[noreturn]] (T&& t); 16 17 #include <exception> 18 #include <cstdlib> 19 #include <cassert> 20 21 class A 22 { 23 int data_; 24 public: 25 explicit A(int data) : data_(data) {} 26 27 friend bool operator==(const A& x, const A& y) {return x.data_ == y.data_;} 28 }; 29 30 class B 31 : public std::nested_exception 32 { 33 int data_; 34 public: 35 explicit B(int data) : data_(data) {} 36 37 friend bool operator==(const B& x, const B& y) {return x.data_ == y.data_;} 38 }; 39 40 #if __cplusplus > 201103L 41 struct Final final {}; 42 #endif 43 44 int main() 45 { 46 { 47 try 48 { 49 A a(3); 50 std::throw_with_nested(a); 51 assert(false); 52 } 53 catch (const A& a) 54 { 55 assert(a == A(3)); 56 } 57 } 58 { 59 try 60 { 61 A a(4); 62 std::throw_with_nested(a); 63 assert(false); 64 } 65 catch (const std::nested_exception& e) 66 { 67 assert(e.nested_ptr() == nullptr); 68 } 69 } 70 { 71 try 72 { 73 B b(5); 74 std::throw_with_nested(b); 75 assert(false); 76 } 77 catch (const B& b) 78 { 79 assert(b == B(5)); 80 } 81 } 82 { 83 try 84 { 85 B b(6); 86 std::throw_with_nested(b); 87 assert(false); 88 } 89 catch (const std::nested_exception& e) 90 { 91 assert(e.nested_ptr() == nullptr); 92 const B& b = dynamic_cast<const B&>(e); 93 assert(b == B(6)); 94 } 95 } 96 { 97 try 98 { 99 int i = 7; 100 std::throw_with_nested(i); 101 assert(false); 102 } 103 catch (int i) 104 { 105 assert(i == 7); 106 } 107 } 108 #if __cplusplus > 201103L 109 { 110 try 111 { 112 std::throw_with_nested(Final()); 113 assert(false); 114 } 115 catch (const Final &) 116 { 117 } 118 } 119 #endif 120 } 121