From cc49bc44570ca1299f387c689ba7810e2d3145a5 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 27 Aug 2020 03:06:14 +0300 Subject: [PATCH] Extract `throws` into its own header --- include/boost/system/detail/throws.hpp | 59 ++++++++++++++++++++++++++ include/boost/system/error_code.hpp | 35 +-------------- 2 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 include/boost/system/detail/throws.hpp diff --git a/include/boost/system/detail/throws.hpp b/include/boost/system/detail/throws.hpp new file mode 100644 index 0000000..1a22498 --- /dev/null +++ b/include/boost/system/detail/throws.hpp @@ -0,0 +1,59 @@ +#ifndef BOOST_SYSTEM_DETAIL_THROWS_HPP_INCLUDED +#define BOOST_SYSTEM_DETAIL_THROWS_HPP_INCLUDED + +// Copyright Beman Dawes 2006, 2007 +// Copyright Christoper Kohlhoff 2007 +// Copyright Peter Dimov 2017, 2018 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +// See library home page at http://www.boost.org/libs/system + +namespace boost +{ + +namespace system +{ + +class error_code; + +} // namespace system + +// boost::throws() + +namespace detail +{ + +// Misuse of the error_code object is turned into a noisy failure by +// poisoning the reference. This particular implementation doesn't +// produce warnings or errors from popular compilers, is very efficient +// (as determined by inspecting generated code), and does not suffer +// from order of initialization problems. In practice, it also seems +// cause user function error handling implementation errors to be detected +// very early in the development cycle. + +inline system::error_code* throws() +{ + // See github.com/boostorg/system/pull/12 by visigoth for why the return + // is poisoned with nonzero rather than (0). A test, test_throws_usage(), + // has been added to error_code_test.cpp, and as visigoth mentioned it + // fails on clang for release builds with a return of 0 but works fine + // with (1). + // Since the undefined behavior sanitizer (-fsanitize=undefined) does not + // allow a reference to be formed to the unaligned address of (1), we use + // (8) instead. + + return reinterpret_cast(8); +} + +} // namespace detail + +inline system::error_code& throws() +{ + return *detail::throws(); +} + +} // namespace boost + +#endif // #ifndef BOOST_SYSTEM_DETAIL_THROWS_HPP_INCLUDED diff --git a/include/boost/system/error_code.hpp b/include/boost/system/error_code.hpp index c7e745f..ef0fdab 100644 --- a/include/boost/system/error_code.hpp +++ b/include/boost/system/error_code.hpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -48,40 +49,6 @@ class error_condition; // portable generic values defined below, but ultimate } // namespace system -// boost::throws() - -namespace detail -{ - -// Misuse of the error_code object is turned into a noisy failure by -// poisoning the reference. This particular implementation doesn't -// produce warnings or errors from popular compilers, is very efficient -// (as determined by inspecting generated code), and does not suffer -// from order of initialization problems. In practice, it also seems -// cause user function error handling implementation errors to be detected -// very early in the development cycle. - -inline system::error_code* throws() -{ - // See github.com/boostorg/system/pull/12 by visigoth for why the return - // is poisoned with nonzero rather than (0). A test, test_throws_usage(), - // has been added to error_code_test.cpp, and as visigoth mentioned it - // fails on clang for release builds with a return of 0 but works fine - // with (1). - // Since the undefined behavior sanitizer (-fsanitize=undefined) does not - // allow a reference to be formed to the unaligned address of (1), we use - // (8) instead. - - return reinterpret_cast(8); -} - -} // namespace detail - -inline system::error_code& throws() -{ - return *detail::throws(); -} - // non-member functions of error_code and error_condition namespace system