throws.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #ifndef BOOST_SYSTEM_DETAIL_THROWS_HPP_INCLUDED
  2. #define BOOST_SYSTEM_DETAIL_THROWS_HPP_INCLUDED
  3. // Copyright Beman Dawes 2006, 2007
  4. // Copyright Christoper Kohlhoff 2007
  5. // Copyright Peter Dimov 2017, 2018
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See library home page at http://www.boost.org/libs/system
  11. namespace boost
  12. {
  13. namespace system
  14. {
  15. class error_code;
  16. } // namespace system
  17. // boost::throws()
  18. namespace detail
  19. {
  20. // Misuse of the error_code object is turned into a noisy failure by
  21. // poisoning the reference. This particular implementation doesn't
  22. // produce warnings or errors from popular compilers, is very efficient
  23. // (as determined by inspecting generated code), and does not suffer
  24. // from order of initialization problems. In practice, it also seems
  25. // cause user function error handling implementation errors to be detected
  26. // very early in the development cycle.
  27. inline system::error_code* throws()
  28. {
  29. // See github.com/boostorg/system/pull/12 by visigoth for why the return
  30. // is poisoned with nonzero rather than (0). A test, test_throws_usage(),
  31. // has been added to error_code_test.cpp, and as visigoth mentioned it
  32. // fails on clang for release builds with a return of 0 but works fine
  33. // with (1).
  34. // Since the undefined behavior sanitizer (-fsanitize=undefined) does not
  35. // allow a reference to be formed to the unaligned address of (1), we use
  36. // (8) instead.
  37. return reinterpret_cast<system::error_code*>(8);
  38. }
  39. } // namespace detail
  40. inline system::error_code& throws()
  41. {
  42. return *detail::throws();
  43. }
  44. } // namespace boost
  45. #endif // #ifndef BOOST_SYSTEM_DETAIL_THROWS_HPP_INCLUDED