58 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++0310 11#include <cxxabi.h>12#include <cassert>13#include <stdlib.h>14#include <exception>15#include <typeinfo>16 17#include "test_macros.h"18 19class Base {20 virtual void foo() {};21};22 23class Derived : public Base {};24 25Derived &test_bad_cast(Base& b) {26 return dynamic_cast<Derived&>(b);27}28 29Base gB;30 31void my_terminate() { exit(0); }32 33int main ()34{35 // swap-out the terminate handler36 void (*default_handler)() = std::get_terminate();37 std::set_terminate(my_terminate);38 39#ifndef TEST_HAS_NO_EXCEPTIONS40 try {41#endif42 Derived &d = test_bad_cast(gB);43 assert(false);44 ((void)d);45#ifndef TEST_HAS_NO_EXCEPTIONS46 } catch (std::bad_cast const&) {47 // success48 return 0;49 } catch (...) {50 assert(false);51 }52#endif53 54 // failure, restore the default terminate handler and fire55 std::set_terminate(default_handler);56 std::terminate();57}58