2
0
mirror of https://github.com/boostorg/convert.git synced 2026-02-18 14:02:14 +00:00
Files
convert/doc/html/the_boost_convert_library/convert_default_constructible.html

104 lines
8.5 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>The Default Constructible Type Requirement</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="../index.html" title="Chapter&#160;1.&#160;The Boost Convert Library 2.0">
<link rel="up" href="../index.html" title="Chapter&#160;1.&#160;The Boost Convert Library 2.0">
<link rel="prev" href="__std__stringstream__based_converter/performance.html" title="Performance">
<link rel="next" href="other_conversions.html" title="Other Conversions">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="spirit-nav">
<a accesskey="p" href="__std__stringstream__based_converter/performance.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="other_conversions.html"><img src="../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="the_boost_convert_library.convert_default_constructible"></a><a class="link" href="convert_default_constructible.html" title="The Default Constructible Type Requirement">The
<span class="emphasis"><em>Default Constructible</em></span> Type Requirement</a>
</h2></div></div></div>
<p>
In order for a type to be integrated into the <span class="emphasis"><em>boost::lexical_cast</em></span>
framework that type needs to be <span class="emphasis"><em>Default Constructible</em></span>.
Deeply in the bowels of the implementation a temporary-storage instance of
the Target type is created and then populated with the conversion result. The
<span class="emphasis"><em>boost::lexical_cast</em></span> implementation chooses the default
constructor to create such an instance. For more details see <a href="http://www.boost.org/doc/libs/1_55_0/doc/html/boost_lexical_cast.html" target="_top"><span class="emphasis"><em>lexical_cast</em></span>
documentation</a>.
</p>
<p>
<span class="emphasis"><em>boost::convert</em></span> also creates a temporary-storage instance
of the Target type that the plugged-in converter then populates with the conversion
result. By default that temporary-storage is created by
</p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span>
<span class="special">{</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">TypeOut</span><span class="special">&gt;</span>
<span class="identifier">TypeOut</span>
<span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">TypeOut</span><span class="special">&gt;::</span><span class="identifier">create_storage</span><span class="special">()</span>
<span class="special">{</span>
<span class="keyword">return</span> <span class="identifier">TypeOut</span><span class="special">();</span>
<span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
and, therefore, the <span class="emphasis"><em>Default Constructible</em></span> requirement
is also the default requirement of <span class="emphasis"><em>boost::convert</em></span>.
</p>
<p>
A well-designed type (in my opinion, anyway) should only have meaningful and
unambiguous constructors... and the default constructor is not necessarily
one of them. Consider the following <span class="emphasis"><em>direction</em></span> type as
one such example. The type has only two meaningful states and is not <span class="emphasis"><em>Default
Constructible</em></span>:
</p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">direction</span>
<span class="special">{</span>
<span class="keyword">enum</span> <span class="identifier">value_type</span> <span class="special">{</span> <span class="identifier">up</span><span class="special">,</span> <span class="identifier">dn</span> <span class="special">};</span>
<span class="identifier">direction</span><span class="special">(</span><span class="identifier">value_type</span> <span class="identifier">value</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">value_</span><span class="special">(</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{}</span>
<span class="keyword">private</span><span class="special">:</span> <span class="identifier">value_type</span> <span class="identifier">value_</span><span class="special">;</span>
<span class="special">};</span>
</pre>
<p>
For such a type the call below will not compile (due to the <span class="emphasis"><em>Default
Constructible</em></span> requirement):
</p>
<pre class="programlisting"><span class="identifier">direction</span> <span class="identifier">dir1</span> <span class="special">=</span> <span class="identifier">lexical_cast</span><span class="special">&lt;</span><span class="identifier">direction</span><span class="special">&gt;(</span><span class="identifier">str</span><span class="special">);</span> <span class="comment">// Does not compile</span>
<span class="identifier">direction</span> <span class="identifier">dir2</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">direction</span><span class="special">&gt;::</span><span class="identifier">from</span><span class="special">(</span><span class="identifier">str</span><span class="special">).</span><span class="identifier">value</span><span class="special">();</span> <span class="comment">// Does not compile</span>
</pre>
<p>
However, <span class="emphasis"><em>boost::convert</em></span> will be able to handle such a
type with little help from the user. What <span class="emphasis"><em>boost::convert</em></span>
needs is the instructions <span class="emphasis"><em>how</em></span> to create that mentioned
temporary-storage:
</p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span>
<span class="special">{</span>
<span class="keyword">template</span><span class="special">&lt;&gt;</span> <span class="keyword">inline</span> <span class="identifier">direction</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">direction</span><span class="special">&gt;::</span><span class="identifier">create_storage</span><span class="special">()</span>
<span class="special">{</span>
<span class="keyword">return</span> <span class="identifier">direction</span><span class="special">(</span><span class="identifier">direction</span><span class="special">::</span><span class="identifier">up</span><span class="special">);</span>
<span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
Now the conversion code compiles:
</p>
<pre class="programlisting"><span class="identifier">direction</span> <span class="identifier">dir2</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">direction</span><span class="special">&gt;::</span><span class="identifier">from</span><span class="special">(</span><span class="identifier">str</span><span class="special">).</span><span class="identifier">value</span><span class="special">();</span> <span class="comment">// Compiles</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2009-2014 Vladimir Batov<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="__std__stringstream__based_converter/performance.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="other_conversions.html"><img src="../images/next.png" alt="Next"></a>
</div>
</body>
</html>