1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Default Converter</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="../index.html" title="Chapter 1. Boost.Convert 2.0"> 9<link rel="prev" href="error_detection.html" title="Better Error Detection"> 10<link rel="next" href="alternative_interface.html" title="Alternative Interface"> 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="error_detection.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="alternative_interface.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 27<a name="boost_convert.default_converter"></a><a class="link" href="default_converter.html" title="Default Converter">Default Converter</a> 28</h2></div></div></div> 29<p> 30 The explicit converter as in 31 </p> 32<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">convert</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="string">"123"</span><span class="special">,</span> <span class="identifier">converter</span><span class="special">).</span><span class="identifier">value</span><span class="special">();</span> 33</pre> 34<p> 35 provides considerable flexibility, configurability and efficiency. However, 36 in certain contexts that might be not that important or even counter-productive 37 if, for example, an application relies on certain consistent behavior associated 38 with one particular converter type and configuration. To accommodate such a 39 scenario <span class="emphasis"><em>Boost.Convert</em></span> introduces the concept of the 40 <span class="emphasis"><em>default converter</em></span> implemented as <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">by_default</span></code>. 41 </p> 42<div class="important"><table border="0" summary="Important"> 43<tr> 44<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td> 45<th align="left">Important</th> 46</tr> 47<tr><td align="left" valign="top"><p> 48 There is no default converter set by default. 49 </p></td></tr> 50</table></div> 51<p> 52 Consequently, without additional configuration steps the following call will 53 fail to compile: 54 </p> 55<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">convert</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="string">"123"</span><span class="special">).</span><span class="identifier">value</span><span class="special">();</span> <span class="comment">// No converter provided</span> 56</pre> 57<p> 58 However, after <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">by_default</span></code> 59 is defined simply as: 60 </p> 61<p> 62</p> 63<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">by_default</span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">cstream</span> <span class="special">{};</span> 64</pre> 65<p> 66 </p> 67<p> 68 or potentially configured with additional formatting: 69 </p> 70<p> 71</p> 72<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">by_default</span> <span class="special">:</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">cstream</span> 73<span class="special">{</span> 74 <span class="identifier">by_default</span><span class="special">()</span> <span class="special">{</span> <span class="special">(*</span><span class="keyword">this</span><span class="special">)(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">uppercase</span><span class="special">)(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">hex</span><span class="special">);</span> <span class="special">}</span> 75<span class="special">};</span> 76</pre> 77<p> 78 </p> 79<p> 80 the code compiles and deploys <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">cstream</span></code> when <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">convert</span><span class="special">()</span></code> is called without an explicitly supplied 81 converter: 82 </p> 83<p> 84</p> 85<pre class="programlisting"><span class="comment">// No explicit converter provided. boost::cnv::by_default is used.</span> 86<span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">convert</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="string">"F"</span><span class="special">).</span><span class="identifier">value_or</span><span class="special">(-</span><span class="number">1</span><span class="special">);</span> 87<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">s</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">convert</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">>(</span><span class="number">255</span><span class="special">).</span><span class="identifier">value_or</span><span class="special">(</span><span class="string">"bad"</span><span class="special">);</span> 88 89<span class="comment">// 'i' and 's' are converted using boost::cnv::cstream</span> 90<span class="comment">// with std::uppercase and std::hex formatting applied.</span> 91 92<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">i</span> <span class="special">==</span> <span class="number">15</span><span class="special">);</span> <span class="comment">// 15(10) = F(16)</span> 93<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">s</span> <span class="special">==</span> <span class="string">"FF"</span><span class="special">);</span> <span class="comment">// 255(10) = FF(16)</span> 94</pre> 95<p> 96 </p> 97<p> 98 The trade-off for the convenience is the rigid converter configuration (which 99 in certain contexts might be the desired behavior) and a potential performance 100 impact. When a converter is not provided explicitly, the default converter 101 is created, potentially configured, deployed and destroyed for every <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">convert</span><span class="special">()</span></code> call. Consequently, if efficiency of this 102 particular component is important, then the implementation of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">by_default</span></code> 103 will need to take that into account and to make sure those operations are cheap. 104 </p> 105</div> 106<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 107<td align="left"></td> 108<td align="right"><div class="copyright-footer">Copyright © 2009-2016 Vladimir Batov<p> 109 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>. 110 </p> 111</div></td> 112</tr></table> 113<hr> 114<div class="spirit-nav"> 115<a accesskey="p" href="error_detection.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="alternative_interface.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 116</div> 117</body> 118</html> 119