CLHEP VERSION Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // Explore single- and double-throwing techniques 00002 00003 00004 #include <iostream> 00005 using std::cerr; 00006 00007 00008 struct B { 00009 00010 B() { cerr << "make B\n"; } 00011 B( B const & b ) { cerr << "copy B\n"; } 00012 virtual char type() const { return 'B'; }; 00013 00014 }; // struct B 00015 00016 00017 struct D : public B { 00018 00019 D() { cerr << "make D\n"; } 00020 D( const D & d ) : B( d ) { cerr << "copy D\n"; } 00021 virtual char type() const { return 'D'; }; 00022 00023 }; // struct D 00024 00025 00026 #define single( obj ) \ 00027 cerr << "\nsingle( " #obj " )\n"; \ 00028 const B & ref = obj; \ 00029 throw ref; 00030 00031 00032 #define double( obj ) \ 00033 cerr << "\ndouble( " #obj " )\n"; \ 00034 try { throw obj; } \ 00035 catch ( const B & x ) { throw; } 00036 00037 00038 void f( void g() ) { 00039 00040 try { g(); } 00041 //catch( D const & x ) { cerr << "Caught D is " << x.type() << '\n'; } 00042 catch( B const & x ) { cerr << "Caught B is " << x.type() << '\n'; } 00043 00044 } 00045 00046 00047 void test1() { double( B() ); } 00048 void test2() { double( D() ); } 00049 void test3() { single( B() ); } 00050 void test4() { single( D() ); } 00051 00052 00053 int main() { 00054 00055 cerr << "\nTesting double throws:\n"; 00056 f( test1 ); 00057 f( test2 ); 00058 00059 cerr << "\nTesting single throws:\n"; 00060 f( test3 ); 00061 f( test4 ); 00062 00063 }