mirror of
https://github.com/boostorg/optional.git
synced 2025-07-29 12:07:21 +02:00
Reverted specialization for trivial types
It caused too many problems. I left only specialiation for scalar types. I will need to devise clever type traits for reconizing trivial types with working constructor.
This commit is contained in:
@ -10,7 +10,7 @@ Technical details aside, the memory layout of `optional<T>` for a generic `T` is
|
||||
std::aligned_storage_t<sizeof(t), alignof(T)> _storage;
|
||||
};
|
||||
|
||||
Lifetime of the `T` inside `_storage` is manually controlled with placement-`new`s and pseudo-destructor calls. However, for trivial `T`s we use a different way of storage, by simply holding a `T`:
|
||||
Lifetime of the `T` inside `_storage` is manually controlled with placement-`new`s and pseudo-destructor calls. However, for scalar `T`s we use a different way of storage, by simply holding a `T`:
|
||||
|
||||
template <typename T>
|
||||
class optional
|
||||
@ -19,7 +19,7 @@ Lifetime of the `T` inside `_storage` is manually controlled with placement-`new
|
||||
T _storage;
|
||||
};
|
||||
|
||||
We call it a ['direct] storage. This makes `optional<T>` a trivially-copyable type for trivial `T`s. This only works for compilers that support defaulted functions and type traits. On compilers without defaulted functions we still use the direct storage, but `optional<T>` is no longer recognized as trivially-copyable. On compilers that do not fully support type traits, we still use the direct storage for scalar types, but we leave the programmer a way of customizing her type, so that it is reconized by `optional` as trivial, by specializing type trait `boost::opitonal_config::is_type_trivial`:
|
||||
We call it a ['direct] storage. This makes `optional<T>` a trivially-copyable type for scalar `T`s. This only works for compilers that support defaulted functions. On compilers without defaulted functions we still use the direct storage, but `optional<T>` is no longer recognized as trivially-copyable. Apart from scalar types, we leave the programmer a way of customizing her type, so that it is reconized by `optional` as candidate for optimized storage, by specializing type trait `boost::opitonal_config::optional_uses_direct_storage_for`:
|
||||
|
||||
struct X // not trivial
|
||||
{
|
||||
@ -28,7 +28,7 @@ We call it a ['direct] storage. This makes `optional<T>` a trivially-copyable ty
|
||||
|
||||
namespace boost { namespace optional_config {
|
||||
|
||||
template <> struct is_type_trivial<X> : boost::true_type {};
|
||||
template <> struct optional_uses_direct_storage_for<X> : boost::true_type {};
|
||||
|
||||
}}
|
||||
|
||||
|
@ -13,7 +13,7 @@
|
||||
|
||||
[heading Boost Release 1.66]
|
||||
|
||||
* On newer compilers `optional` is now trivially-copyable for trivial `T`s. This uses a different storage (just `T` rather than `aligned_storage`). We require the compiler to support defaulted functions and type traits. Otherwise, we still use the the plain storage for scalar types.
|
||||
* On newer compilers `optional` is now trivially-copyable for scalar `T`s. This uses a different storage (just `T` rather than `aligned_storage`). We require the compiler to support defaulted functions.
|
||||
|
||||
|
||||
[heading Boost Release 1.63]
|
||||
|
@ -33,11 +33,10 @@
|
||||
</h4>
|
||||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
|
||||
On newer compilers <code class="computeroutput"><span class="identifier">optional</span></code>
|
||||
is now trivially-copyable for trivial <code class="computeroutput"><span class="identifier">T</span></code>s.
|
||||
is now trivially-copyable for scalar <code class="computeroutput"><span class="identifier">T</span></code>s.
|
||||
This uses a different storage (just <code class="computeroutput"><span class="identifier">T</span></code>
|
||||
rather than <code class="computeroutput"><span class="identifier">aligned_storage</span></code>).
|
||||
We require the compiler to support defaulted functions and type traits.
|
||||
Otherwise, we still use the the plain storage for scalar types.
|
||||
We require the compiler to support defaulted functions.
|
||||
</li></ul></div>
|
||||
<h4>
|
||||
<a name="boost_optional.relnotes.h1"></a>
|
||||
|
@ -43,7 +43,7 @@
|
||||
Lifetime of the <code class="computeroutput"><span class="identifier">T</span></code> inside
|
||||
<code class="computeroutput"><span class="identifier">_storage</span></code> is manually controlled
|
||||
with placement-<code class="computeroutput"><span class="keyword">new</span></code>s and pseudo-destructor
|
||||
calls. However, for trivial <code class="computeroutput"><span class="identifier">T</span></code>s
|
||||
calls. However, for scalar <code class="computeroutput"><span class="identifier">T</span></code>s
|
||||
we use a different way of storage, by simply holding a <code class="computeroutput"><span class="identifier">T</span></code>:
|
||||
</p>
|
||||
<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">></span>
|
||||
@ -55,15 +55,13 @@
|
||||
</pre>
|
||||
<p>
|
||||
We call it a <span class="emphasis"><em>direct</em></span> storage. This makes <code class="computeroutput"><span class="identifier">optional</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code> a
|
||||
trivially-copyable type for trivial <code class="computeroutput"><span class="identifier">T</span></code>s.
|
||||
This only works for compilers that support defaulted functions and type traits.
|
||||
On compilers without defaulted functions we still use the direct storage,
|
||||
but <code class="computeroutput"><span class="identifier">optional</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code> is
|
||||
no longer recognized as trivially-copyable. On compilers that do not fully
|
||||
support type traits, we still use the direct storage for scalar types, but
|
||||
we leave the programmer a way of customizing her type, so that it is reconized
|
||||
by <code class="computeroutput"><span class="identifier">optional</span></code> as trivial, by
|
||||
specializing type trait <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">opitonal_config</span><span class="special">::</span><span class="identifier">is_type_trivial</span></code>:
|
||||
trivially-copyable type for scalar <code class="computeroutput"><span class="identifier">T</span></code>s.
|
||||
This only works for compilers that support defaulted functions. On compilers
|
||||
without defaulted functions we still use the direct storage, but <code class="computeroutput"><span class="identifier">optional</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code> is
|
||||
no longer recognized as trivially-copyable. Apart from scalar types, we leave
|
||||
the programmer a way of customizing her type, so that it is reconized by
|
||||
<code class="computeroutput"><span class="identifier">optional</span></code> as candidate for
|
||||
optimized storage, by specializing type trait <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">opitonal_config</span><span class="special">::</span><span class="identifier">optional_uses_direct_storage_for</span></code>:
|
||||
</p>
|
||||
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">X</span> <span class="comment">// not trivial</span>
|
||||
<span class="special">{</span>
|
||||
@ -72,7 +70,7 @@
|
||||
|
||||
<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">optional_config</span> <span class="special">{</span>
|
||||
|
||||
<span class="keyword">template</span> <span class="special"><></span> <span class="keyword">struct</span> <span class="identifier">is_type_trivial</span><span class="special"><</span><span class="identifier">X</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">true_type</span> <span class="special">{};</span>
|
||||
<span class="keyword">template</span> <span class="special"><></span> <span class="keyword">struct</span> <span class="identifier">optional_uses_direct_storage_for</span><span class="special"><</span><span class="identifier">X</span><span class="special">></span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">true_type</span> <span class="special">{};</span>
|
||||
|
||||
<span class="special">}}</span>
|
||||
</pre>
|
||||
|
@ -146,7 +146,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: October 30, 2017 at 22:34:46 GMT</small></p></td>
|
||||
<td align="left"><p><small>Last revised: November 04, 2017 at 16:36:14 GMT</small></p></td>
|
||||
<td align="right"><div class="copyright-footer"></div></td>
|
||||
</tr></table>
|
||||
<hr>
|
||||
|
Reference in New Issue
Block a user