forked from boostorg/optional
trivially-copyable optional<T> for trivial T
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
|
||||
[section Performance considerations]
|
||||
|
||||
Technical details aside, the memory layout of `optional<T>` is more-less this:
|
||||
Technical details aside, the memory layout of `optional<T>` for a generic `T` is more-less this:
|
||||
|
||||
template <typename T>
|
||||
class optional
|
||||
@ -9,8 +9,33 @@ Technical details aside, the memory layout of `optional<T>` is more-less this:
|
||||
bool _initialized;
|
||||
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`:
|
||||
|
||||
But for the purpose of this analysis, considering memory layouts, we can think of it as:
|
||||
template <typename T>
|
||||
class optional
|
||||
{
|
||||
bool _initialized;
|
||||
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`:
|
||||
|
||||
struct X // not trivial
|
||||
{
|
||||
X() {}
|
||||
};
|
||||
|
||||
namespace boost { namespace optional_config {
|
||||
|
||||
template <> struct is_type_trivial<X> : boost::true_type {};
|
||||
|
||||
}}
|
||||
|
||||
|
||||
[heading Controlling the size]
|
||||
|
||||
For the purpose of the followin analysis, considering memory layouts, we can think of it as:
|
||||
|
||||
template <typename T>
|
||||
class optional
|
||||
|
@ -11,6 +11,11 @@
|
||||
|
||||
[section:relnotes Release Notes]
|
||||
|
||||
[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.
|
||||
|
||||
|
||||
[heading Boost Release 1.63]
|
||||
* Added two new in-place constructors. They work similarly to `emplace()` functions: they initialize the contained value by perfect-forwarding the obtained arguments. One constructor always initializes the contained value, the other based on a boolean condition.
|
||||
* Syntax `o = {}` now correctly un-initializes optional, just like in `std::optional`.
|
||||
|
@ -28,6 +28,19 @@
|
||||
</h2></div></div></div>
|
||||
<h4>
|
||||
<a name="boost_optional.relnotes.h0"></a>
|
||||
<span class="phrase"><a name="boost_optional.relnotes.boost_release_1_66"></a></span><a class="link" href="relnotes.html#boost_optional.relnotes.boost_release_1_66">Boost
|
||||
Release 1.66</a>
|
||||
</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.
|
||||
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.
|
||||
</li></ul></div>
|
||||
<h4>
|
||||
<a name="boost_optional.relnotes.h1"></a>
|
||||
<span class="phrase"><a name="boost_optional.relnotes.boost_release_1_63"></a></span><a class="link" href="relnotes.html#boost_optional.relnotes.boost_release_1_63">Boost
|
||||
Release 1.63</a>
|
||||
</h4>
|
||||
@ -51,7 +64,7 @@
|
||||
</li>
|
||||
</ul></div>
|
||||
<h4>
|
||||
<a name="boost_optional.relnotes.h1"></a>
|
||||
<a name="boost_optional.relnotes.h2"></a>
|
||||
<span class="phrase"><a name="boost_optional.relnotes.boost_release_1_62"></a></span><a class="link" href="relnotes.html#boost_optional.relnotes.boost_release_1_62">Boost
|
||||
Release 1.62</a>
|
||||
</h4>
|
||||
@ -59,7 +72,7 @@
|
||||
Fixed <a href="https://svn.boost.org/trac/boost/ticket/12179" target="_top">Trac #12179</a>.
|
||||
</li></ul></div>
|
||||
<h4>
|
||||
<a name="boost_optional.relnotes.h2"></a>
|
||||
<a name="boost_optional.relnotes.h3"></a>
|
||||
<span class="phrase"><a name="boost_optional.relnotes.boost_release_1_61"></a></span><a class="link" href="relnotes.html#boost_optional.relnotes.boost_release_1_61">Boost
|
||||
Release 1.61</a>
|
||||
</h4>
|
||||
@ -102,7 +115,7 @@
|
||||
</li>
|
||||
</ul></div>
|
||||
<h4>
|
||||
<a name="boost_optional.relnotes.h3"></a>
|
||||
<a name="boost_optional.relnotes.h4"></a>
|
||||
<span class="phrase"><a name="boost_optional.relnotes.boost_release_1_60"></a></span><a class="link" href="relnotes.html#boost_optional.relnotes.boost_release_1_60">Boost
|
||||
Release 1.60</a>
|
||||
</h4>
|
||||
@ -113,7 +126,7 @@
|
||||
#11203</a>.
|
||||
</li></ul></div>
|
||||
<h4>
|
||||
<a name="boost_optional.relnotes.h4"></a>
|
||||
<a name="boost_optional.relnotes.h5"></a>
|
||||
<span class="phrase"><a name="boost_optional.relnotes.boost_release_1_59"></a></span><a class="link" href="relnotes.html#boost_optional.relnotes.boost_release_1_59">Boost
|
||||
Release 1.59</a>
|
||||
</h4>
|
||||
@ -127,7 +140,7 @@
|
||||
</li>
|
||||
</ul></div>
|
||||
<h4>
|
||||
<a name="boost_optional.relnotes.h5"></a>
|
||||
<a name="boost_optional.relnotes.h6"></a>
|
||||
<span class="phrase"><a name="boost_optional.relnotes.boost_release_1_58"></a></span><a class="link" href="relnotes.html#boost_optional.relnotes.boost_release_1_58">Boost
|
||||
Release 1.58</a>
|
||||
</h4>
|
||||
@ -163,7 +176,7 @@
|
||||
</li>
|
||||
</ul></div>
|
||||
<h4>
|
||||
<a name="boost_optional.relnotes.h6"></a>
|
||||
<a name="boost_optional.relnotes.h7"></a>
|
||||
<span class="phrase"><a name="boost_optional.relnotes.boost_release_1_57"></a></span><a class="link" href="relnotes.html#boost_optional.relnotes.boost_release_1_57">Boost
|
||||
Release 1.57</a>
|
||||
</h4>
|
||||
@ -173,7 +186,7 @@
|
||||
to fix C++03 compile error on <code class="computeroutput"><span class="identifier">logic_error</span><span class="special">(</span><span class="string">"..."</span><span class="special">)</span></code>"</em></span>.
|
||||
</li></ul></div>
|
||||
<h4>
|
||||
<a name="boost_optional.relnotes.h7"></a>
|
||||
<a name="boost_optional.relnotes.h8"></a>
|
||||
<span class="phrase"><a name="boost_optional.relnotes.boost_release_1_56"></a></span><a class="link" href="relnotes.html#boost_optional.relnotes.boost_release_1_56">Boost
|
||||
Release 1.56</a>
|
||||
</h4>
|
||||
|
@ -29,7 +29,8 @@
|
||||
</h3></div></div></div>
|
||||
<p>
|
||||
Technical details aside, the memory layout of <code class="computeroutput"><span class="identifier">optional</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code>
|
||||
is more-less this:
|
||||
for a generic <code class="computeroutput"><span class="identifier">T</span></code> is more-less
|
||||
this:
|
||||
</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>
|
||||
<span class="keyword">class</span> <span class="identifier">optional</span>
|
||||
@ -39,8 +40,50 @@
|
||||
<span class="special">};</span>
|
||||
</pre>
|
||||
<p>
|
||||
But for the purpose of this analysis, considering memory layouts, we can
|
||||
think of it as:
|
||||
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
|
||||
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>
|
||||
<span class="keyword">class</span> <span class="identifier">optional</span>
|
||||
<span class="special">{</span>
|
||||
<span class="keyword">bool</span> <span class="identifier">_initialized</span><span class="special">;</span>
|
||||
<span class="identifier">T</span> <span class="identifier">_storage</span><span class="special">;</span>
|
||||
<span class="special">};</span>
|
||||
</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>:
|
||||
</p>
|
||||
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">X</span> <span class="comment">// not trivial</span>
|
||||
<span class="special">{</span>
|
||||
<span class="identifier">X</span><span class="special">()</span> <span class="special">{}</span>
|
||||
<span class="special">};</span>
|
||||
|
||||
<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="special">}}</span>
|
||||
</pre>
|
||||
<h5>
|
||||
<a name="boost_optional.tutorial.performance_considerations.h0"></a>
|
||||
<span class="phrase"><a name="boost_optional.tutorial.performance_considerations.controlling_the_size"></a></span><a class="link" href="performance_considerations.html#boost_optional.tutorial.performance_considerations.controlling_the_size">Controlling
|
||||
the size</a>
|
||||
</h5>
|
||||
<p>
|
||||
For the purpose of the followin analysis, considering memory layouts, we
|
||||
can think of it as:
|
||||
</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>
|
||||
<span class="keyword">class</span> <span class="identifier">optional</span>
|
||||
@ -143,7 +186,7 @@
|
||||
<span class="inlinemediaobject"><img src="../../images/opt_align4.png" alt="opt_align4"></span>
|
||||
</p>
|
||||
<h5>
|
||||
<a name="boost_optional.tutorial.performance_considerations.h0"></a>
|
||||
<a name="boost_optional.tutorial.performance_considerations.h1"></a>
|
||||
<span class="phrase"><a name="boost_optional.tutorial.performance_considerations.optional_function_parameters"></a></span><a class="link" href="performance_considerations.html#boost_optional.tutorial.performance_considerations.optional_function_parameters">Optional
|
||||
function parameters</a>
|
||||
</h5>
|
||||
|
@ -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: May 17, 2017 at 23:01:39 GMT</small></p></td>
|
||||
<td align="left"><p><small>Last revised: October 28, 2017 at 14:38:14 GMT</small></p></td>
|
||||
<td align="right"><div class="copyright-footer"></div></td>
|
||||
</tr></table>
|
||||
<hr>
|
||||
|
Reference in New Issue
Block a user