1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
/*
* Copyright Andrey Semashev 2018.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* https://www.boost.org/LICENSE_1_0.txt)
*/
/*!
* \file uncaught_exceptions.cpp
* \author Andrey Semashev
* \date 2018-11-10
*
* \brief This file contains tests for the uncaught_exceptions function.
*
* This file only contains the very basic checks of functionality that can be portably achieved
* through C++03 std::uncaught_exception.
*/
#include <boost/core/uncaught_exceptions.hpp>
#include <boost/core/lightweight_test.hpp>
#if defined(_MSC_VER)
# pragma warning(disable: 4512) // assignment operator could not be generated
#endif
struct my_exception {};
class exception_watcher
{
unsigned int& m_count;
public:
explicit exception_watcher(unsigned int& count) : m_count(count) {}
~exception_watcher() { m_count = boost::core::uncaught_exceptions(); }
};
// Tests for uncaught_exceptions when used in a destructor while an exception propagates
void test_in_destructor()
{
const unsigned int root_count = boost::core::uncaught_exceptions();
unsigned int level1_count = root_count;
try
{
exception_watcher watcher(level1_count);
throw my_exception();
}
catch (...)
{
}
BOOST_TEST_NE(root_count, level1_count);
}
int main()
{
test_in_destructor();
return boost::report_errors();
}
|