Howard Hinnant | 372e2f4 | 2012-01-31 23:52:20 +0000 | [diff] [blame] | 1 | //===---------------------- catch_class_02.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 <exception> |
| 11 | #include <stdlib.h> |
| 12 | #include <assert.h> |
| 13 | |
| 14 | struct B |
| 15 | { |
| 16 | static int count; |
| 17 | int id_; |
| 18 | explicit B(int id) : id_(id) {count++;} |
| 19 | B(const B& a) : id_(a.id_) {count++;} |
| 20 | ~B() {count--;} |
| 21 | }; |
| 22 | |
| 23 | int B::count = 0; |
| 24 | |
| 25 | struct A |
| 26 | : B |
| 27 | { |
| 28 | static int count; |
| 29 | int id_; |
| 30 | explicit A(int id) : B(id-1), id_(id) {count++;} |
| 31 | A(const A& a) : B(a.id_-1), id_(a.id_) {count++;} |
| 32 | ~A() {count--;} |
| 33 | }; |
| 34 | |
| 35 | int A::count = 0; |
| 36 | |
| 37 | void f1() |
| 38 | { |
| 39 | assert(A::count == 0); |
| 40 | assert(B::count == 0); |
| 41 | A a(3); |
| 42 | assert(A::count == 1); |
| 43 | assert(B::count == 1); |
| 44 | throw a; |
| 45 | assert(false); |
| 46 | } |
| 47 | |
| 48 | void f2() |
| 49 | { |
| 50 | try |
| 51 | { |
| 52 | assert(A::count == 0); |
| 53 | f1(); |
| 54 | assert(false); |
| 55 | } |
| 56 | catch (A a) |
| 57 | { |
| 58 | assert(A::count != 0); |
| 59 | assert(B::count != 0); |
| 60 | assert(a.id_ == 3); |
| 61 | throw; |
| 62 | } |
| 63 | catch (B b) |
| 64 | { |
| 65 | assert(false); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | int main() |
| 70 | { |
| 71 | try |
| 72 | { |
| 73 | f2(); |
| 74 | assert(false); |
| 75 | } |
| 76 | catch (const B& b) |
| 77 | { |
| 78 | assert(B::count != 0); |
| 79 | assert(b.id_ == 2); |
| 80 | } |
| 81 | assert(A::count == 0); |
| 82 | assert(B::count == 0); |
| 83 | } |