Cleaner handling of explicit U to T conversions

This commit is contained in:
Andrzej Krzemienski
2014-06-20 11:38:57 +02:00
parent 4af83ecf83
commit 4cbb67e505
5 changed files with 68 additions and 83 deletions

View File

@ -326,7 +326,7 @@ __SPACE__
value is another reference to the same object referenced by `*rhs`; else
`*this` is uninitialized.
* [*Throws:] Nothing.
* [*Notes:] If `rhs` is initialized, both `*this` and `*rhs` will reefer to the
* [*Notes:] If `rhs` is initialized, both `*this` and `*rhs` will refer to the
same object (they alias).
* [*Example:]
``
@ -360,7 +360,8 @@ __SPACE__
* [*Postconditions:] If `rhs` is initialized, `*this` is initialized and
its value is move constructed from `rhs`; else `*this` is uninitialized.
* [*Throws:] Whatever `T::T( T&& )` throws.
* [*Notes:] If `rhs` is initialized, `T::T( T && )` is called. The expression inside `noexcept` is equivalent to `is_nothrow_move_constructible<T>::value`.
* [*Remarks:] The expression inside `noexcept` is equivalent to `is_nothrow_move_constructible<T>::value`.
* [*Notes:] If `rhs` is initialized, `T::T( T && )` is called.
* [*Exception Safety:] Exceptions can only be thrown during
`T::T( T&& );` in that case, `rhs` remains initialized and the value of `*rhs` is determined by exception safety of `T::T(T&&)`.
* [*Example:]
@ -390,7 +391,7 @@ __SPACE__
value is another reference to the same object referenced by `*rhs`; else
`*this` is uninitialized.
* [*Throws:] Nothing.
* [*Notes:] If `rhs` is initialized, both `*this` and `*rhs` will reefer to the
* [*Notes:] If `rhs` is initialized, both `*this` and `*rhs` will refer to the
same object (they alias).
* [*Example:]
``
@ -664,10 +665,11 @@ __SPACE__
* [*Postconditions:] If `rhs` is initialized, `*this` is initialized and
its value is moved from `*rhs`, `rhs` remains initialized; else `*this` is uninitialized.
* [*Throws:] Whatever `T::operator( T&& )` or `T::T( T && )` throws.
* [*Remarks:] The expression inside `noexcept` is equivalent to `is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value`.
* [*Notes:] If both `*this` and `rhs` are initially initialized, `T`'s
['move assignment operator] is used. If `*this` is initially initialized but `rhs` is
uninitialized, `T`'s [destructor] is called. If `*this` is initially uninitialized
but `rhs` is initialized, `T`'s ['move constructor] is called. The expression inside `noexcept` is equivalent to `is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value`.
but `rhs` is initialized, `T`'s ['move constructor] is called.
* [*Exception Safety:] In the event of an exception, the initialization state of
`*this` is unchanged and its value unspecified as far as optional is concerned
(it is up to `T`'s `operator=()`). If `*this` is initially uninitialized and
@ -932,8 +934,7 @@ __SPACE__
[: `template<class U> T optional<T>::value_or(U && v) const& ;`]
* [*Requires:] `T` is __COPY_CONSTRUCTIBLE__ and `U &&` is convertible to `T`.
* [*Returns:] `bool(*this) ? **this : static_cast<T>(std::forward<U>(v))`.
* [*Throws:] Any exception thrown by the selected constructor of `T`.
* [*Effects:] `if (*this) return **this; else return std::forward<U>(v);`.
* [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is replaced with the `const`-qualified member function. On compilers without rvalue reference support the type of `v` becomes `U const&`.
__SPACE__
@ -943,8 +944,7 @@ __SPACE__
[: `template<class U> T optional<T>::value_or(U && v) && ;`]
* [*Requires:] `T` is __MOVE_CONSTRUCTIBLE__ and `U &&` is convertible to `T`.
* [*Returns:] `bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(v))`.
* [*Throws:] Any exception thrown by the selected constructor of `T`.
* [*Effects:] `if (*this) return std::move(**this); else return std::forward<U>(v);`.
* [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is not present.
__SPACE__
@ -954,9 +954,8 @@ __SPACE__
[: `template<class F> T optional<T>::value_or_eval(F f) const& ;`]
* [*Requires:] `T` is __COPY_CONSTRUCTIBLE__ and `F` models a __SGI_GENERATOR__ whose result type is convertible to `T`.
* [*Returns:] `bool(*this) ? **this : static_cast<T>(f())`.
* [*Throws:] Any exception thrown by the selected constructor of `T` or by `f`.
* [*Notes:] Function `f` is only evaluated if `bool(*this) == false`. On compilers that do not support ref-qualifiers on member functions this overload is replaced with the `const`-qualified member function.
* [*Effects:] `if (*this) return **this; else return f();`.
* [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is replaced with the `const`-qualified member function.
* [*Example:]
``
int complain_and_0()
@ -982,9 +981,8 @@ __SPACE__
[: `template<class F> T optional<T>::value_or_eval(F f) && ;`]
* [*Requires:] `T` is __MOVE_CONSTRUCTIBLE__ and `F` models a __SGI_GENERATOR__ whose result type is convertible to `T`.
* [*Returns:] `bool(*this) ? std::move(**this) : static_cast<T>(f())`.
* [*Throws:] Any exception thrown by the selected constructor of `T` or by `f`.
* [*Notes:] Function `f` is only evaluated if `bool(*this) == false`. On compilers that do not support ref-qualifiers on member functions this overload is not present.
* [*Effects:] `if (*this) return std::move(**this); else return f();`.
* [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is not present.
__SPACE__

View File

@ -352,7 +352,7 @@
<span class="bold"><strong>Notes:</strong></span> If <code class="computeroutput"><span class="identifier">rhs</span></code>
is initialized, both <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
and <code class="computeroutput"><span class="special">*</span><span class="identifier">rhs</span></code>
will reefer to the same object (they alias).
will refer to the same object (they alias).
</li>
<li class="listitem">
<span class="bold"><strong>Example:</strong></span>
@ -402,12 +402,13 @@
<span class="bold"><strong>Throws:</strong></span> Whatever <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">T</span><span class="special">(</span> <span class="identifier">T</span><span class="special">&amp;&amp;</span> <span class="special">)</span></code>
throws.
</li>
<li class="listitem">
<span class="bold"><strong>Remarks:</strong></span> The expression inside <code class="computeroutput"><span class="keyword">noexcept</span></code> is equivalent to <code class="computeroutput"><span class="identifier">is_nothrow_move_constructible</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Notes:</strong></span> If <code class="computeroutput"><span class="identifier">rhs</span></code>
is initialized, <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">T</span><span class="special">(</span> <span class="identifier">T</span> <span class="special">&amp;&amp;</span>
<span class="special">)</span></code> is called. The expression inside
<code class="computeroutput"><span class="keyword">noexcept</span></code> is equivalent to
<code class="computeroutput"><span class="identifier">is_nothrow_move_constructible</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span></code>.
<span class="special">)</span></code> is called.
</li>
<li class="listitem">
<span class="bold"><strong>Exception Safety:</strong></span> Exceptions can only
@ -461,7 +462,7 @@
<span class="bold"><strong>Notes:</strong></span> If <code class="computeroutput"><span class="identifier">rhs</span></code>
is initialized, both <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
and <code class="computeroutput"><span class="special">*</span><span class="identifier">rhs</span></code>
will reefer to the same object (they alias).
will refer to the same object (they alias).
</li>
<li class="listitem">
<span class="bold"><strong>Example:</strong></span>
@ -914,6 +915,10 @@
or <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">T</span><span class="special">(</span> <span class="identifier">T</span> <span class="special">&amp;&amp;</span>
<span class="special">)</span></code> throws.
</li>
<li class="listitem">
<span class="bold"><strong>Remarks:</strong></span> The expression inside <code class="computeroutput"><span class="keyword">noexcept</span></code> is equivalent to <code class="computeroutput"><span class="identifier">is_nothrow_move_constructible</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span> <span class="special">&amp;&amp;</span>
<span class="identifier">is_nothrow_move_assignable</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Notes:</strong></span> If both <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> and <code class="computeroutput"><span class="identifier">rhs</span></code>
are initially initialized, <code class="computeroutput"><span class="identifier">T</span></code>'s
@ -921,10 +926,7 @@
is initially initialized but <code class="computeroutput"><span class="identifier">rhs</span></code>
is uninitialized, <code class="computeroutput"><span class="identifier">T</span></code>'s
[destructor] is called. If <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code> is initially uninitialized but <code class="computeroutput"><span class="identifier">rhs</span></code> is initialized, <code class="computeroutput"><span class="identifier">T</span></code>'s
<span class="emphasis"><em>move constructor</em></span> is called. The expression inside
<code class="computeroutput"><span class="keyword">noexcept</span></code> is equivalent to
<code class="computeroutput"><span class="identifier">is_nothrow_move_constructible</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span>
<span class="special">&amp;&amp;</span> <span class="identifier">is_nothrow_move_assignable</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span></code>.
<span class="emphasis"><em>move constructor</em></span> is called.
</li>
<li class="listitem">
<span class="bold"><strong>Exception Safety:</strong></span> In the event of an
@ -1455,11 +1457,8 @@
is convertible to <code class="computeroutput"><span class="identifier">T</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Returns:</strong></span> <code class="computeroutput"><span class="keyword">bool</span><span class="special">(*</span><span class="keyword">this</span><span class="special">)</span> <span class="special">?</span> <span class="special">**</span><span class="keyword">this</span> <span class="special">:</span> <span class="keyword">static_cast</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span><span class="identifier">U</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">))</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Throws:</strong></span> Any exception thrown by the
selected constructor of <code class="computeroutput"><span class="identifier">T</span></code>.
<span class="bold"><strong>Effects:</strong></span> <code class="computeroutput"><span class="keyword">if</span>
<span class="special">(*</span><span class="keyword">this</span><span class="special">)</span> <span class="keyword">return</span> <span class="special">**</span><span class="keyword">this</span><span class="special">;</span> <span class="keyword">else</span> <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span><span class="identifier">U</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">);</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Notes:</strong></span> On compilers that do not support
@ -1485,11 +1484,9 @@
is convertible to <code class="computeroutput"><span class="identifier">T</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Returns:</strong></span> <code class="computeroutput"><span class="keyword">bool</span><span class="special">(*</span><span class="keyword">this</span><span class="special">)</span> <span class="special">?</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(**</span><span class="keyword">this</span><span class="special">)</span> <span class="special">:</span> <span class="keyword">static_cast</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span><span class="identifier">U</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">))</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Throws:</strong></span> Any exception thrown by the
selected constructor of <code class="computeroutput"><span class="identifier">T</span></code>.
<span class="bold"><strong>Effects:</strong></span> <code class="computeroutput"><span class="keyword">if</span>
<span class="special">(*</span><span class="keyword">this</span><span class="special">)</span> <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(**</span><span class="keyword">this</span><span class="special">);</span> <span class="keyword">else</span> <span class="keyword">return</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special">&lt;</span><span class="identifier">U</span><span class="special">&gt;(</span><span class="identifier">v</span><span class="special">);</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Notes:</strong></span> On compilers that do not support
@ -1509,16 +1506,11 @@
is convertible to <code class="computeroutput"><span class="identifier">T</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Returns:</strong></span> <code class="computeroutput"><span class="keyword">bool</span><span class="special">(*</span><span class="keyword">this</span><span class="special">)</span> <span class="special">?</span> <span class="special">**</span><span class="keyword">this</span> <span class="special">:</span> <span class="keyword">static_cast</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;(</span><span class="identifier">f</span><span class="special">())</span></code>.
<span class="bold"><strong>Effects:</strong></span> <code class="computeroutput"><span class="keyword">if</span>
<span class="special">(*</span><span class="keyword">this</span><span class="special">)</span> <span class="keyword">return</span> <span class="special">**</span><span class="keyword">this</span><span class="special">;</span> <span class="keyword">else</span> <span class="keyword">return</span> <span class="identifier">f</span><span class="special">();</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Throws:</strong></span> Any exception thrown by the
selected constructor of <code class="computeroutput"><span class="identifier">T</span></code>
or by <code class="computeroutput"><span class="identifier">f</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Notes:</strong></span> Function <code class="computeroutput"><span class="identifier">f</span></code>
is only evaluated if <code class="computeroutput"><span class="keyword">bool</span><span class="special">(*</span><span class="keyword">this</span><span class="special">)</span> <span class="special">==</span> <span class="keyword">false</span></code>. On compilers that do not support
<span class="bold"><strong>Notes:</strong></span> On compilers that do not support
ref-qualifiers on member functions this overload is replaced with the
<code class="computeroutput"><span class="keyword">const</span></code>-qualified member function.
</li>
@ -1555,16 +1547,12 @@
whose result type is convertible to <code class="computeroutput"><span class="identifier">T</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Returns:</strong></span> <code class="computeroutput"><span class="keyword">bool</span><span class="special">(*</span><span class="keyword">this</span><span class="special">)</span> <span class="special">?</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(**</span><span class="keyword">this</span><span class="special">)</span> <span class="special">:</span> <span class="keyword">static_cast</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;(</span><span class="identifier">f</span><span class="special">())</span></code>.
<span class="bold"><strong>Effects:</strong></span> <code class="computeroutput"><span class="keyword">if</span>
<span class="special">(*</span><span class="keyword">this</span><span class="special">)</span> <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(**</span><span class="keyword">this</span><span class="special">);</span> <span class="keyword">else</span> <span class="keyword">return</span>
<span class="identifier">f</span><span class="special">();</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Throws:</strong></span> Any exception thrown by the
selected constructor of <code class="computeroutput"><span class="identifier">T</span></code>
or by <code class="computeroutput"><span class="identifier">f</span></code>.
</li>
<li class="listitem">
<span class="bold"><strong>Notes:</strong></span> Function <code class="computeroutput"><span class="identifier">f</span></code>
is only evaluated if <code class="computeroutput"><span class="keyword">bool</span><span class="special">(*</span><span class="keyword">this</span><span class="special">)</span> <span class="special">==</span> <span class="keyword">false</span></code>. On compilers that do not support
<span class="bold"><strong>Notes:</strong></span> On compilers that do not support
ref-qualifiers on member functions this overload is not present.
</li>
</ul></div>

View File

@ -133,7 +133,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: June 18, 2014 at 14:36:35 GMT</small></p></td>
<td align="left"><p><small>Last revised: June 20, 2014 at 09:06:52 GMT</small></p></td>
<td align="right"><div class="copyright-footer"></div></td>
</tr></table>
<hr>

View File

@ -112,28 +112,6 @@ class typed_in_place_factory_base ;
template<class T> void swap ( optional<T>& x, optional<T>& y );
namespace optional_detail {
// converts type U to type T using only implicit conversions/constructors
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
template <typename TT, typename UU>
TT convert(UU && u)
{
return forward<UU>(u);
}
#else
template <typename TT, typename UU>
TT convert(const UU& u)
{
return u;
}
template <typename TT, typename UU>
TT convert(UU& u)
{
return u;
}
#endif
// This local class is used instead of that in "aligned_storage.hpp"
// because I've found the 'official' class to ICE BCB5.5
// when some types are used with optional<>
@ -1082,25 +1060,37 @@ class optional : public optional_detail::optional_base<T>
template <class U>
value_type value_or ( U&& v ) const&
{
return this->is_initialized() ? get() : optional_detail::convert<value_type>(boost::forward<U>(v));
if (this->is_initialized())
return get();
else
return boost::forward<U>(v);
}
template <class U>
value_type value_or ( U&& v ) &&
{
return this->is_initialized() ? boost::move(get()) : optional_detail::convert<value_type>(boost::forward<U>(v));
if (this->is_initialized())
return boost::move(get());
else
return boost::forward<U>(v);
}
#elif !defined BOOST_NO_CXX11_RVALUE_REFERENCES
template <class U>
value_type value_or ( U&& v ) const
{
return this->is_initialized() ? get() : optional_detail::convert<value_type>(boost::forward<U>(v));
if (this->is_initialized())
return get();
else
return boost::forward<U>(v);
}
#else
template <class U>
value_type value_or ( U const& v ) const
{
return this->is_initialized() ? get() : optional_detail::convert<value_type>(v);
if (this->is_initialized())
return get();
else
return v;
}
#endif
@ -1109,19 +1099,28 @@ class optional : public optional_detail::optional_base<T>
template <typename F>
value_type value_or_eval ( F f ) const&
{
return this->is_initialized() ? get() : optional_detail::convert<value_type>(f());
if (this->is_initialized())
return get();
else
return f();
}
template <typename F>
value_type value_or_eval ( F f ) &&
{
return this->is_initialized() ? boost::move(get()) : optional_detail::convert<value_type>(f());
if (this->is_initialized())
return boost::move(get());
else
return f();
}
#else
template <typename F>
value_type value_or_eval ( F f ) const
{
return this->is_initialized() ? get() : optional_detail::convert<value_type>(f());
if (this->is_initialized())
return get();
else
return f();
}
#endif

View File

@ -149,7 +149,7 @@ int throw_()
throw int();
}
void test_function_value_or_call()
void test_function_value_or_eval()
{
optional<int> o1 = 1;
optional<int> oN;
@ -237,7 +237,7 @@ int test_main( int, char* [] )
{
test_function_value();
test_function_value_or();
test_function_value_or_call();
test_function_value_or_eval();
}
catch ( ... )
{