Corrected rethrow_if_nested

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@104943 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant 2010-05-28 13:35:41 +00:00
parent 13c98cc20d
commit 6bb9f58bc8
2 changed files with 33 additions and 17 deletions
include
test/language.support/support.exception/except.nested

@ -211,19 +211,19 @@ template <class _E>
inline inline
void void
rethrow_if_nested(const _E& __e, typename enable_if< rethrow_if_nested(const _E& __e, typename enable_if<
!is_same<_E, nested_exception>::value && is_polymorphic<_E>::value
is_convertible<_E*, nested_exception*>::value
>::type* = 0) >::type* = 0)
{ {
static_cast<const nested_exception&>(__e).rethrow_nested(); const nested_exception* __nep = dynamic_cast<const nested_exception*>(&__e);
if (__nep)
__nep->rethrow_nested();
} }
template <class _E> template <class _E>
inline inline
void void
rethrow_if_nested(const _E& __e, typename enable_if< rethrow_if_nested(const _E& __e, typename enable_if<
is_same<_E, nested_exception>::value || !is_polymorphic<_E>::value
!is_convertible<_E*, nested_exception*>::value
>::type* = 0) >::type* = 0)
{ {
} }

@ -22,19 +22,18 @@ class A
int data_; int data_;
public: public:
explicit A(int data) : data_(data) {} explicit A(int data) : data_(data) {}
virtual ~A() {}
friend bool operator==(const A& x, const A& y) {return x.data_ == y.data_;} friend bool operator==(const A& x, const A& y) {return x.data_ == y.data_;}
}; };
class B class B
: public std::nested_exception : public std::nested_exception,
public A
{ {
int data_;
public: public:
explicit B(int data) : data_(data) {} explicit B(int data) : A(data) {}
B(const B& b) : data_(b.data_) {} B(const B& b) : A(b) {}
friend bool operator==(const B& x, const B& y) {return x.data_ == y.data_;}
}; };
int main() int main()
@ -56,18 +55,35 @@ int main()
{ {
throw B(5); throw B(5);
} }
catch (const B& b0) catch (const B& b)
{ {
try try
{ {
B b = b0; throw b;
std::rethrow_if_nested(b);
assert(false);
} }
catch (const B& b) catch (const A& a)
{ {
assert(b == B(5)); try
{
std::rethrow_if_nested(a);
assert(false);
}
catch (const B& b)
{
assert(b == B(5));
}
} }
} }
} }
{
try
{
std::rethrow_if_nested(1);
assert(true);
}
catch (...)
{
assert(false);
}
}
} }