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