Use BOOST_EXPLICIT_OPERATOR_BOOL for optional

I often have the problem that when I change a std::wstring to boost::optional<std::wstring> and the variable is used as a parameter with Boost.Format, the result silently changes from the string contents to "1".

This change prevents implicit conversion to bool if the compiler supports explicit conversion operators.
This commit is contained in:
Marcel Raad
2014-04-24 10:00:43 +02:00
committed by Andrzej Krzemienski
parent b4738ac07e
commit c7cf80e5df
8 changed files with 46 additions and 23 deletions

View File

@ -981,16 +981,19 @@
<span class="inlinemediaobject"><img src="../images/space.png" alt="space"></span>
</p>
<a name="reference_optional_operator_bool"></a><div class="blockquote"><blockquote class="blockquote"><p>
<code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="keyword">operator</span> </code><span class="emphasis"><em>unspecified-bool-type</em></span><code class="computeroutput"><span class="special">()</span> <span class="keyword">const</span> <span class="special">;</span></code>
<code class="computeroutput"><span class="keyword">explicit</span> <span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="keyword">operator</span> <span class="keyword">bool</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">;</span></code>
</p></blockquote></div>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<span class="bold"><strong>Returns:</strong></span> An unspecified value which if
used on a boolean context is equivalent to (<code class="computeroutput"><span class="identifier">get_ptr</span><span class="special">()</span> <span class="special">!=</span> <span class="number">0</span></code>)
<span class="bold"><strong>Returns:</strong></span> <code class="computeroutput"><span class="identifier">get_ptr</span><span class="special">()</span> <span class="special">!=</span> <span class="number">0</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Throws:</strong></span> Nothing.
</li>
<li class="listitem">
<span class="bold"><strong>Notes:</strong></span> On compilers that do not support
explicit conversion operators this falls back to safe-bool idiom.
</li>
<li class="listitem">
<span class="bold"><strong>Example:</strong></span>
<pre class="programlisting"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">def</span> <span class="special">;</span>

View File

@ -81,7 +81,7 @@
<span class="identifier">T</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">get_ptr</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">;</span> <a class="link" href="detailed_semantics.html#reference_optional_get_ptr"><span class="inlinemediaobject"><img src="../images/callouts/R.png" alt="R"></span></a>
<span class="identifier">T</span><span class="special">*</span> <span class="identifier">get_ptr</span><span class="special">()</span> <span class="special">;</span> <a class="link" href="detailed_semantics.html#reference_optional_get_ptr"><span class="inlinemediaobject"><img src="../images/callouts/R.png" alt="R"></span></a>
<span class="keyword">operator</span> <span class="identifier">unspecified</span><span class="special">-</span><span class="keyword">bool</span><span class="special">-</span><span class="identifier">type</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">;</span> <a class="link" href="detailed_semantics.html#reference_optional_operator_bool"><span class="inlinemediaobject"><img src="../images/callouts/R.png" alt="R"></span></a>
<span class="keyword">explicit</span> <span class="keyword">operator</span> <span class="keyword">bool</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">;</span> <a class="link" href="detailed_semantics.html#reference_optional_operator_bool"><span class="inlinemediaobject"><img src="../images/callouts/R.png" alt="R"></span></a>
<span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">!()</span> <span class="keyword">const</span> <span class="special">;</span> <a class="link" href="detailed_semantics.html#reference_optional_operator_not"><span class="inlinemediaobject"><img src="../images/callouts/R.png" alt="R"></span></a>

View File

@ -163,7 +163,7 @@
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"><p><small>Last revised: April 11, 2014 at 13:25:26 GMT</small></p></td>
<td align="left"><p><small>Last revised: April 26, 2014 at 08:58:33 GMT</small></p></td>
<td align="right"><div class="copyright-footer"></div></td>
</tr></table>
<hr>

View File

@ -66,7 +66,7 @@
T const* get_ptr() const ; ``[link reference_optional_get_ptr __GO_TO__]``
T* get_ptr() ; ``[link reference_optional_get_ptr __GO_TO__]``
operator unspecified-bool-type() const ; ``[link reference_optional_operator_bool __GO_TO__]``
explicit operator bool() const ; ``[link reference_optional_operator_bool __GO_TO__]``
bool operator!() const ; ``[link reference_optional_operator_not __GO_TO__]``
@ -657,11 +657,11 @@ __SPACE__
[#reference_optional_operator_bool]
[: `optional<T>::operator `['unspecified-bool-type]`() const ;`]
[: `explicit optional<T>::operator bool() const ;`]
* [*Returns:] An unspecified value which if used on a boolean context
is equivalent to (`get_ptr() != 0`)
* [*Returns:] `get_ptr() != 0`.
* [*Throws:] Nothing.
* [*Notes:] On compilers that do not support explicit conversion operators this falls back to safe-bool idiom.
* [*Example:]
``
optional<T> def ;

View File

@ -36,6 +36,7 @@
#include <boost/utility/addressof.hpp>
#include <boost/utility/compare_pointees.hpp>
#include <boost/utility/in_place_factory.hpp>
#include <boost/utility/explicit_operator_bool.hpp>
#include <boost/optional/optional_fwd.hpp>
@ -180,8 +181,6 @@ class optional_base : public optional_tag
typedef BOOST_DEDUCED_TYPENAME mpl::if_<is_reference_predicate,types_when_ref,types_when_not_ref>::type types ;
protected:
typedef bool (this_type::*unspecified_bool_type)() const;
typedef BOOST_DEDUCED_TYPENAME types::reference_type reference_type ;
typedef BOOST_DEDUCED_TYPENAME types::reference_const_type reference_const_type ;
typedef BOOST_DEDUCED_TYPENAME types::pointer_type pointer_type ;
@ -418,8 +417,6 @@ class optional_base : public optional_tag
destroy_impl(is_reference_predicate()) ;
}
unspecified_bool_type safe_bool() const { return m_initialized ? &this_type::is_initialized : 0 ; }
reference_const_type get_impl() const { return dereference(get_object(), is_reference_predicate() ) ; }
reference_type get_impl() { return dereference(get_object(), is_reference_predicate() ) ; }
@ -479,8 +476,6 @@ class optional : public optional_detail::optional_base<T>
{
typedef optional_detail::optional_base<T> base ;
typedef BOOST_DEDUCED_TYPENAME base::unspecified_bool_type unspecified_bool_type ;
public :
typedef optional<T> this_type ;
@ -620,13 +615,9 @@ class optional : public optional_detail::optional_base<T>
reference_const_type operator *() const { return this->get() ; }
reference_type operator *() { return this->get() ; }
// implicit conversion to "bool"
// No-throw
operator unspecified_bool_type() const { return this->safe_bool() ; }
// This is provided for those compilers which don't like the conversion to bool
// on some contexts.
bool operator!() const { return !this->is_initialized() ; }
BOOST_EXPLICIT_OPERATOR_BOOL()
} ;
// Returns optional<T>(v)

View File

@ -29,5 +29,6 @@ import testing ;
[ compile-fail optional_test_ref_fail4.cpp ]
[ compile-fail optional_test_inplace_fail.cpp ]
[ compile-fail optional_test_inplace_fail2.cpp ]
[ compile-fail optional_test_fail_implicit_bool_convert.cpp ]
;
}

View File

@ -1086,8 +1086,8 @@ namespace optional_swap_test
//
void swap(boost::optional<class_whose_explicit_ctor_should_be_used> & x, boost::optional<class_whose_explicit_ctor_should_be_used> & y)
{
bool hasX = x;
bool hasY = y;
bool hasX(x);
bool hasY(y);
if ( !hasX && !hasY )
return;

View File

@ -0,0 +1,28 @@
// Copyright (C) 2014, Andrzej Krzemienski.
//
// Use, modification, and distribution is subject to 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 http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// akrzemi1@gmail.com
//
#include "boost/optional.hpp"
//
// THIS TEST SHOULD FAIL TO COMPILE
//
#if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
bool test_implicit_conversion_to_bool()
{
boost::optional<int> opt;
return opt;
}
#else
# error "Test skipped: this compiler does not support explicit conversion operators."
#endif