2 / Copyright (c) 2013 Andrey Semashev
4 / Distributed under the Boost Software License, Version 1.0. (See accompanying
5 / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 [section:explicit_operator_bool explicit_operator_bool]
22 Header `<boost/core/explicit_operator_bool.hpp>` provides
23 `BOOST_EXPLICIT_OPERATOR_BOOL()`, `BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()`
24 and `BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()` compatibility helper macros
25 that expand to an explicit conversion operator to `bool`. For compilers not
26 supporting explicit conversion operators introduced in C++11 the macros expand
27 to a conversion operator that implements the
28 [@http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool safe bool idiom].
29 In case if the compiler is not able to handle safe bool idiom well the macros
30 expand to a regular conversion operator to `bool`.
38 Both macros are intended to be placed within a user's class definition. The
39 generated conversion operators will be implemented in terms of `operator!()`
40 that should be defined by user in this class. In case of
41 `BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()` the generated conversion operator
42 will be declared `constexpr` which requires the corresponding `operator!()`
43 to also be `constexpr`.
46 template< typename T >
52 BOOST_EXPLICIT_OPERATOR_BOOL()
54 bool operator!() const
61 Now `my_ptr` can be used in conditional expressions, similarly to a regular pointer:
66 std::cout << "true" << std::endl;
77 * Added new macros `BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT` and `BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL` to define `noexcept` and `constexpr` operators.
78 * The header moved to Boost.Core.
82 * The macro was extracted from Boost.Log.