2013-09-01 16:31:16 +00:00
[/
/ Copyright (c) 2013 Andrey Semashev
/
/ 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)
/]
2014-06-12 02:10:10 +04:00
[section:explicit_operator_bool explicit_operator_bool]
2014-06-10 19:12:54 +03:00
[/=================]
[simplesect Authors]
[/=================]
2014-06-05 10:48:21 -07:00
* Andrey Semashev
2014-06-10 19:12:54 +03:00
[endsimplesect]
2014-06-05 10:48:21 -07:00
2013-09-01 16:31:16 +00:00
[/===============]
[section Overview]
[/===============]
2014-06-05 10:48:21 -07:00
Header `<boost/core/explicit_operator_bool.hpp>` provides
`BOOST_EXPLICIT_OPERATOR_BOOL()`, `BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()`
and `BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()` compatibility helper macros
that expand to an explicit conversion operator to `bool`. For compilers not
supporting explicit conversion operators introduced in C++11 the macros expand
to a conversion operator that implements the
[@http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool safe bool idiom].
In case if the compiler is not able to handle safe bool idiom well the macros
expand to a regular conversion operator to `bool`.
2013-09-01 16:31:16 +00:00
[endsect]
[/===============]
[section Examples]
[/===============]
2014-06-05 10:48:21 -07:00
Both macros are intended to be placed within a user's class definition. The
generated conversion operators will be implemented in terms of `operator!()`
that should be defined by user in this class. In case of
`BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()` the generated conversion operator
will be declared `constexpr` which requires the corresponding `operator!()`
to also be `constexpr`.
2013-09-01 16:31:16 +00:00
2014-06-05 10:48:21 -07:00
``
template< typename T >
class my_ptr
{
T* m_p;
2013-09-01 16:31:16 +00:00
2014-06-05 10:48:21 -07:00
public:
BOOST_EXPLICIT_OPERATOR_BOOL()
2013-09-01 16:31:16 +00:00
2014-06-05 10:48:21 -07:00
bool operator!() const
{
return !m_p;
}
};
``
2013-09-01 16:31:16 +00:00
Now `my_ptr` can be used in conditional expressions, similarly to a regular pointer:
2014-06-05 10:48:21 -07:00
``
my_ptr< int > p;
if (p)
std::cout << "true" << std::endl;
``
2013-09-01 16:31:16 +00:00
[endsect]
2014-06-05 23:04:06 +04:00
[/==============]
2013-09-01 16:31:16 +00:00
[section History]
2014-06-05 23:04:06 +04:00
[/==============]
2013-09-01 16:31:16 +00:00
2014-06-05 23:04:06 +04:00
[heading boost 1.56]
2013-09-01 16:31:16 +00:00
2014-06-05 23:04:06 +04:00
* Added new macros `BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT` and `BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL` to define `noexcept` and `constexpr` operators.
* The header moved to Boost.Core.
2013-09-01 16:31:16 +00:00
2014-06-05 23:04:06 +04:00
[heading boost 1.55]
* The macro was extracted from Boost.Log.
2014-06-01 18:08:55 -07:00
[endsect]
2014-06-05 10:48:21 -07:00
[endsect]