1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Converter Signature</title> 5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Convert 2.0"> 8<link rel="up" href="../design_notes.html" title="Design Notes"> 9<link rel="prev" href="../design_notes.html" title="Design Notes"> 10<link rel="next" href="user_interface_signature.html" title="User Interface Signature"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="../design_notes.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../design_notes.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="user_interface_signature.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="boost_convert.design_notes.converter_signature"></a><a class="link" href="converter_signature.html" title="Converter Signature">Converter 28 Signature</a> 29</h3></div></div></div> 30<p> 31 The following converter signatures have been considered: 32 </p> 33<pre class="programlisting"><span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">TypeIn</span> <span class="keyword">const</span><span class="special">&,</span> <span class="identifier">TypeOut</span><span class="special">&);</span> <span class="comment">//#1</span> 34<span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">TypeIn</span> <span class="keyword">const</span><span class="special">&,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special"><</span><span class="identifier">TypeOut</span><span class="special">>&);</span> <span class="comment">//#2</span> 35<span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special"><</span><span class="identifier">TypeOut</span><span class="special">></span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">TypeIn</span> <span class="keyword">const</span><span class="special">&);</span> <span class="comment">//#3</span> 36</pre> 37<p> 38 From the design perspective the signature #1 has the advantage of providing 39 the best <span class="emphasis"><em>separation of concerns</em></span>. Namely, it leaves the 40 respective converter with only one task -- the actual task of conversion. 41 In practice though that can result in unnecessary performance overhead. Namely, 42 given an instance of <span class="emphasis"><em>TypeOut</em></span> type is supplied from outside, 43 a storage for that instance needs to be allocated and, most importantly, 44 initialized. That initialization phase (which can be expensive) is an unnecessary 45 overhead as, if the conversion operation succeeds, the initial value is overridden 46 with the actual result, if it fails, then the value of the <span class="emphasis"><em>TypeOut</em></span> 47 instance is either meaningless or worse misleading. 48 </p> 49<p> 50 The signature #2 avoids the initialization overhead by deploying <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span></code>'s ability to allocate storage 51 <span class="emphasis"><em>without initializing it</em></span>. Now the storage for <span class="emphasis"><em>TypeOut</em></span> 52 is still allocated outside but it is not initialized. It is now converter's 53 responsibility to know <span class="emphasis"><em>how</em></span> to initialize the <span class="emphasis"><em>TypeOut</em></span> 54 instance and, <span class="emphasis"><em>when</em></span> needed, to actually initialize it. 55 In practice it is usually easier than it might sound. For example, <code class="computeroutput"><span class="identifier">strtol</span><span class="special">()</span></code>-based 56 converter might have something along the following lines: 57 </p> 58<pre class="programlisting"><span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">str_in</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special"><</span><span class="keyword">int</span><span class="special">>&</span> <span class="identifier">result_out</span><span class="special">)</span> <span class="keyword">const</span> 59<span class="special">{</span> 60 <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">str_end</span> <span class="special">=</span> <span class="identifier">str_in</span> <span class="special">+</span> <span class="identifier">strlen</span><span class="special">(</span><span class="identifier">str_in</span><span class="special">);</span> 61 <span class="keyword">char</span><span class="special">*</span> <span class="identifier">cnv_end</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> 62 <span class="keyword">long</span> <span class="keyword">int</span> <span class="identifier">result</span> <span class="special">=</span> <span class="special">::</span><span class="identifier">strtol</span><span class="special">(</span><span class="identifier">str_in</span><span class="special">,</span> <span class="special">&</span><span class="identifier">cnv_end</span><span class="special">,</span> <span class="identifier">base_</span><span class="special">);</span> 63 64 <span class="keyword">if</span> <span class="special">(</span><span class="identifier">INT_MIN</span> <span class="special"><=</span> <span class="identifier">result</span> <span class="special">&&</span> <span class="identifier">result</span> <span class="special"><=</span> <span class="identifier">INT_MAX</span> <span class="special">&&</span> <span class="identifier">cnv_end</span> <span class="special">==</span> <span class="identifier">str_end</span><span class="special">)</span> 65 <span class="identifier">result_out</span> <span class="special">=</span> <span class="keyword">int</span><span class="special">(</span><span class="identifier">result</span><span class="special">);</span> 66<span class="special">}</span> 67</pre> 68<p> 69 The signature #3 has been briefly considered as aesthetically advantageous 70 and more idiomatic. Unfortunately, it lacked automatic deduction of the 71 <span class="emphasis"><em>TypeOut</em></span> which, consequently, had to be specified explicitly. 72 For different types of supported converters (class-based, plain old functions, 73 lambdas) that complicated considerably the implementation of the <span class="emphasis"><em>Boost.Convert</em></span> 74 infrastructure and restricted implementation of the respective converters. 75 </p> 76</div> 77<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 78<td align="left"></td> 79<td align="right"><div class="copyright-footer">Copyright © 2009-2016 Vladimir Batov<p> 80 Distributed under the Boost Software License, Version 1.0. See copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>. 81 </p> 82</div></td> 83</tr></table> 84<hr> 85<div class="spirit-nav"> 86<a accesskey="p" href="../design_notes.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../design_notes.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="user_interface_signature.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 87</div> 88</body> 89</html> 90