blob: 28b13cc67091ddd7b4c378e57c1cb74301c696be [file] [log] [blame]
Howard Hinnant2d6810f2012-02-01 20:53:21 +00001//===--------------- catch_member_function_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 void foo() {}
15 void bar() const {}
16};
17
18typedef void (A::*mf1)();
19typedef void (A::*mf2)() const;
20
Eric Fiselierb6030b92015-04-06 23:03:01 +000021struct B : public A
22{
23};
24
25typedef void (B::*dmf1)();
26typedef void (B::*dmf2)() const;
27
28template <class Tp>
29bool can_convert(Tp) { return true; }
30
31template <class>
32bool can_convert(...) { return false; }
33
34
Howard Hinnant2d6810f2012-02-01 20:53:21 +000035void test1()
36{
37 try
38 {
39 throw &A::foo;
40 assert(false);
41 }
42 catch (mf2)
43 {
44 assert(false);
45 }
46 catch (mf1)
47 {
48 }
49}
50
51void test2()
52{
53 try
54 {
55 throw &A::bar;
56 assert(false);
57 }
58 catch (mf1)
59 {
60 assert(false);
61 }
62 catch (mf2)
63 {
64 }
65}
66
Eric Fiselierb6030b92015-04-06 23:03:01 +000067
68
69void test_derived()
70{
71 try
72 {
73 throw (mf1)0;
74 assert(false);
75 }
76 catch (dmf2)
77 {
78 assert(false);
79 }
80 catch (dmf1)
81 {
82 assert(false);
83 }
84 catch (mf1)
85 {
86 }
87
88 try
89 {
90 throw (mf2)0;
91 assert(false);
92 }
93 catch (dmf1)
94 {
95 assert(false);
96 }
97 catch (dmf2)
98 {
99 assert(false);
100 }
101 catch (mf2)
102 {
103 }
104
105 assert(!can_convert<mf1>((dmf1)0));
106 assert(!can_convert<mf2>((dmf1)0));
107 try
108 {
109 throw (dmf1)0;
110 assert(false);
111 }
112 catch (mf2)
113 {
114 assert(false);
115 }
116 catch (mf1)
117 {
118 assert(false);
119 }
120 catch (...)
121 {
122 }
123
124 assert(!can_convert<mf1>((dmf2)0));
125 assert(!can_convert<mf2>((dmf2)0));
126 try
127 {
128 throw (dmf2)0;
129 assert(false);
130 }
131 catch (mf2)
132 {
133 assert(false);
134 }
135 catch (mf1)
136 {
137 assert(false);
138 }
139 catch (...)
140 {
141 }
142}
143
144void test_void()
145{
146 assert(!can_convert<void*>(&A::foo));
147 try
148 {
149 throw &A::foo;
150 assert(false);
151 }
152 catch (void*)
153 {
154 assert(false);
155 }
156 catch(...)
157 {
158 }
159}
160
Howard Hinnant2d6810f2012-02-01 20:53:21 +0000161int main()
162{
163 test1();
164 test2();
Eric Fiselierb6030b92015-04-06 23:03:01 +0000165 test_derived();
166 test_void();
Howard Hinnant2d6810f2012-02-01 20:53:21 +0000167}