From 0b48bb81669c2eb28f2b8bc569550772ef6db25c Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Mon, 22 Jan 2018 21:13:02 +0200 Subject: [PATCH] Use explicit operator bool when available --- include/boost/system/error_code.hpp | 23 +++++++++++++++++++++++ test/quick.cpp | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/boost/system/error_code.hpp b/include/boost/system/error_code.hpp index 8041d5d..712d73e 100644 --- a/include/boost/system/error_code.hpp +++ b/include/boost/system/error_code.hpp @@ -2,6 +2,7 @@ // 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) @@ -387,6 +388,15 @@ namespace boost const error_category & category() const BOOST_SYSTEM_NOEXCEPT { return *m_cat; } std::string message() const { return m_cat->message(value()); } +#if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) + + explicit operator bool() const BOOST_SYSTEM_NOEXCEPT // true if error + { + return m_val != 0; + } + +#else + typedef void (*unspecified_bool_type)(); static void unspecified_bool_true() {} @@ -400,6 +410,8 @@ namespace boost return m_val == 0; } +#endif + // relationals: // the more symmetrical non-member syntax allows enum // conversions work for both rhs and lhs. @@ -486,6 +498,15 @@ namespace boost { return m_cat->default_error_condition(value()); } std::string message() const { return m_cat->message(value()); } +#if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) + + explicit operator bool() const BOOST_SYSTEM_NOEXCEPT // true if error + { + return m_val != 0; + } + +#else + typedef void (*unspecified_bool_type)(); static void unspecified_bool_true() {} @@ -499,6 +520,8 @@ namespace boost return m_val == 0; } +#endif + // relationals: inline friend bool operator==( const error_code & lhs, const error_code & rhs ) BOOST_SYSTEM_NOEXCEPT diff --git a/test/quick.cpp b/test/quick.cpp index 882b834..ca66983 100644 --- a/test/quick.cpp +++ b/test/quick.cpp @@ -30,7 +30,7 @@ int main() BOOST_TEST( bt.equivalent( ev, bn ) ); - BOOST_TEST_EQ( bc, bn ); + BOOST_TEST( bc == bn ); return boost::report_errors(); }