blob: 28bf4b569b906302a95296de18cce23ad0a255aa [file] [log] [blame]
Howard Hinnantc649bde2012-02-01 20:53:21 +00001//===----------------- catch_member_data_pointer_01.cpp -------------------===//
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#include <cassert>
11
12struct A
13{
14 const int i;
15 int j;
16};
17
18typedef const int A::*md1;
19typedef int A::*md2;
20
Eric Fiselier0cb62d12015-04-02 23:26:37 +000021struct B : public A
22{
23 const int k;
24 int l;
25};
26
27typedef const int B::*der1;
28typedef int B::*der2;
29
Howard Hinnantc649bde2012-02-01 20:53:21 +000030void test1()
31{
32 try
33 {
34 throw &A::i;
35 assert(false);
36 }
37 catch (md2)
38 {
39 assert(false);
40 }
41 catch (md1)
42 {
43 }
44}
45
Eric Fiselier0cb62d12015-04-02 23:26:37 +000046// Check that cv qualified conversions are allowed.
Howard Hinnantc649bde2012-02-01 20:53:21 +000047void test2()
48{
49 try
50 {
51 throw &A::j;
Eric Fiselier0cb62d12015-04-02 23:26:37 +000052 }
53 catch (md2)
54 {
55 }
56 catch (...)
57 {
58 assert(false);
59 }
60
61 try
62 {
63 throw &A::j;
64 assert(false);
65 }
66 catch (md1)
67 {
68 }
69 catch (...)
70 {
71 assert(false);
72 }
73}
74
Eric Fiselier554d59a2015-04-06 23:03:01 +000075// Check that Base -> Derived conversions are NOT allowed.
Eric Fiselier0cb62d12015-04-02 23:26:37 +000076void test3()
77{
78 try
79 {
80 throw &A::i;
81 assert(false);
82 }
83 catch (md2)
84 {
85 assert(false);
86 }
87 catch (der2)
88 {
89 assert(false);
90 }
91 catch (der1)
92 {
Eric Fiselier554d59a2015-04-06 23:03:01 +000093 assert(false);
Eric Fiselier0cb62d12015-04-02 23:26:37 +000094 }
95 catch (md1)
96 {
Eric Fiselier0cb62d12015-04-02 23:26:37 +000097 }
98}
99
Eric Fiselier554d59a2015-04-06 23:03:01 +0000100// Check that Base -> Derived conversions NOT are allowed with different cv
Eric Fiselier0cb62d12015-04-02 23:26:37 +0000101// qualifiers.
102void test4()
103{
104 try
105 {
106 throw &A::j;
107 assert(false);
108 }
109 catch (der2)
110 {
Eric Fiselier0cb62d12015-04-02 23:26:37 +0000111 assert(false);
112 }
113 catch (der1)
114 {
Eric Fiselier554d59a2015-04-06 23:03:01 +0000115 assert(false);
116 }
117 catch (md2)
118 {
Eric Fiselier0cb62d12015-04-02 23:26:37 +0000119 }
120 catch (...)
121 {
122 assert(false);
123 }
124}
125
126// Check that no Derived -> Base conversions are allowed.
127void test5()
128{
129 try
130 {
131 throw &B::k;
Howard Hinnantc649bde2012-02-01 20:53:21 +0000132 assert(false);
133 }
134 catch (md1)
135 {
136 assert(false);
137 }
138 catch (md2)
139 {
Eric Fiselier0cb62d12015-04-02 23:26:37 +0000140 assert(false);
141 }
142 catch (der1)
143 {
144 }
145
146 try
147 {
148 throw &B::l;
149 assert(false);
150 }
151 catch (md1)
152 {
153 assert(false);
154 }
155 catch (md2)
156 {
157 assert(false);
158 }
159 catch (der2)
160 {
Howard Hinnantc649bde2012-02-01 20:53:21 +0000161 }
162}
163
164int main()
165{
166 test1();
167 test2();
Eric Fiselier0cb62d12015-04-02 23:26:37 +0000168 test3();
169 test4();
170 test5();
Howard Hinnantc649bde2012-02-01 20:53:21 +0000171}