blob: dad0df24807626e6a0f9e0a4bd4467fdd8a518cd [file] [log] [blame]
Howard Hinnanted2c2912010-05-27 17:06:52 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnanted2c2912010-05-27 17:06:52 +00007//
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
20class A
21{
22 int data_;
23public:
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
29class B
30 : public std::nested_exception
31{
32 int data_;
33public:
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 Clowaa8a52c2015-03-16 15:10:28 +000039#if __cplusplus > 201103L
40struct Final final {};
41#endif
42
Howard Hinnanted2c2912010-05-27 17:06:52 +000043int 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 Clowaa8a52c2015-03-16 15:10:28 +0000107#if __cplusplus > 201103L
108 {
109 try
110 {
111 std::throw_with_nested(Final());
112 assert(false);
113 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700114 catch (const Final &f)
Marshall Clowaa8a52c2015-03-16 15:10:28 +0000115 {
116 }
117 }
118#endif
Howard Hinnanted2c2912010-05-27 17:06:52 +0000119}