Howard Hinnant | ed2c291 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | ed2c291 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 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: |
| 24 | explicit A(int data) : data_(data) {} |
| 25 | |
| 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: |
| 34 | explicit B(int data) : data_(data) {} |
| 35 | |
| 36 | friend bool operator==(const B& x, const B& y) {return x.data_ == y.data_;} |
| 37 | }; |
| 38 | |
Marshall Clow | aa8a52c | 2015-03-16 15:10:28 +0000 | [diff] [blame] | 39 | #if __cplusplus > 201103L |
| 40 | struct Final final {}; |
| 41 | #endif |
| 42 | |
Howard Hinnant | ed2c291 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 43 | int 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 | } |
Marshall Clow | aa8a52c | 2015-03-16 15:10:28 +0000 | [diff] [blame] | 107 | #if __cplusplus > 201103L |
| 108 | { |
| 109 | try |
| 110 | { |
| 111 | std::throw_with_nested(Final()); |
| 112 | assert(false); |
| 113 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 114 | catch (const Final &f) |
Marshall Clow | aa8a52c | 2015-03-16 15:10:28 +0000 | [diff] [blame] | 115 | { |
| 116 | } |
| 117 | } |
| 118 | #endif |
Howard Hinnant | ed2c291 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 119 | } |