1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Converters in Detail</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="other_conversions.html" title="Beyond Basic Conversions"> 10<link rel="next" href="converters_detail/stream_converter.html" title="boost::cnv::stream Converter"> 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="other_conversions.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="converters_detail/stream_converter.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.converters_detail"></a><a class="link" href="converters_detail.html" title="Converters in Detail">Converters in Detail</a> 28</h2></div></div></div> 29<div class="toc"><dl class="toc"> 30<dt><span class="section"><a href="converters_detail.html#boost_convert.converters_detail.lexical_cast_converter"><span class="emphasis"><em>boost::cnv::lexical_cast</em></span> 31 Converter</a></span></dt> 32<dt><span class="section"><a href="converters_detail/stream_converter.html"><span class="emphasis"><em>boost::cnv::stream</em></span> 33 Converter</a></span></dt> 34<dd><dl> 35<dt><span class="section"><a href="converters_detail/stream_converter.html#boost_convert.converters_detail.stream_converter.formatting_support">Formatting 36 Support</a></span></dt> 37<dd><dl> 38<dt><span class="section"><a href="converters_detail/stream_converter.html#boost_convert.converters_detail.stream_converter.formatting_support.numeric_base">Numeric 39 Base</a></span></dt> 40<dt><span class="section"><a href="converters_detail/stream_converter.html#boost_convert.converters_detail.stream_converter.formatting_support.field_width__fill_character_and_adjustment">Field 41 Width, Fill Character and Adjustment</a></span></dt> 42<dt><span class="section"><a href="converters_detail/stream_converter.html#boost_convert.converters_detail.stream_converter.formatting_support.leading_whitespace_characters">Leading 43 Whitespace Characters</a></span></dt> 44<dt><span class="section"><a href="converters_detail/stream_converter.html#boost_convert.converters_detail.stream_converter.formatting_support.format_of_boolean_values">Format 45 of Boolean Values</a></span></dt> 46</dl></dd> 47<dt><span class="section"><a href="converters_detail/stream_converter/locale_support.html">Locale 48 Support</a></span></dt> 49<dt><span class="section"><a href="converters_detail/stream_converter/supported_string_types.html">Supported 50 String Types</a></span></dt> 51<dd><dl> 52<dt><span class="section"><a href="converters_detail/stream_converter/supported_string_types.html#boost_convert.converters_detail.stream_converter.supported_string_types.wide_string">Wide 53 String</a></span></dt> 54<dt><span class="section"><a href="converters_detail/stream_converter/supported_string_types/custom_string_types.html">Custom 55 String Types</a></span></dt> 56</dl></dd> 57<dt><span class="section"><a href="converters_detail/stream_converter/the___default_constructible__type_requirement.html">The 58 <span class="emphasis"><em>Default Constructible</em></span> Type Requirement</a></span></dt> 59</dl></dd> 60<dt><span class="section"><a href="converters_detail/strtol_converter.html"><span class="emphasis"><em>boost::cnv::strtol</em></span> 61 Converter</a></span></dt> 62<dd><dl> 63<dt><span class="section"><a href="converters_detail/strtol_converter.html#boost_convert.converters_detail.strtol_converter.basic_deployment">Basic 64 Deployment</a></span></dt> 65<dt><span class="section"><a href="converters_detail/strtol_converter/formatting_support.html">Formatting 66 Support</a></span></dt> 67<dd><dl> 68<dt><span class="section"><a href="converters_detail/strtol_converter/formatting_support.html#boost_convert.converters_detail.strtol_converter.formatting_support.numeric_base__bin__oct__dec__hex_">Numeric 69 Base (bin, oct, dec, hex)</a></span></dt> 70<dt><span class="section"><a href="converters_detail/strtol_converter/formatting_support/field_width__fill_character_and_adjustment.html">Field 71 Width, Fill Character and Adjustment</a></span></dt> 72<dt><span class="section"><a href="converters_detail/strtol_converter/formatting_support/leading_whitespace_characters.html">Leading 73 Whitespace Characters</a></span></dt> 74<dt><span class="section"><a href="converters_detail/strtol_converter/formatting_support/floating_point_precision.html">Floating-Point 75 Precision</a></span></dt> 76</dl></dd> 77<dt><span class="section"><a href="converters_detail/strtol_converter/supported_string_types.html">Supported 78 String Types</a></span></dt> 79<dd><dl> 80<dt><span class="section"><a href="converters_detail/strtol_converter/supported_string_types.html#boost_convert.converters_detail.strtol_converter.supported_string_types.wide_string">Wide 81 String</a></span></dt> 82<dt><span class="section"><a href="converters_detail/strtol_converter/supported_string_types/custom_string_types.html">Custom 83 String Types</a></span></dt> 84</dl></dd> 85</dl></dd> 86</dl></div> 87<div class="section"> 88<div class="titlepage"><div><div><h3 class="title"> 89<a name="boost_convert.converters_detail.lexical_cast_converter"></a><a class="link" href="converters_detail.html#boost_convert.converters_detail.lexical_cast_converter" title="boost::cnv::lexical_cast Converter"><span class="emphasis"><em>boost::cnv::lexical_cast</em></span> 90 Converter</a> 91</h3></div></div></div> 92<p> 93 The purpose of the converter is to 94 </p> 95<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 96<li class="listitem"> 97 Make use of the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span></code> 98 functionality and performance that many people have become accustomed 99 to and comfortable with; 100 </li> 101<li class="listitem"> 102 Demonstrate how existing independent conversion/transformation-related 103 facilities might be incorporated in to the <span class="emphasis"><em>Boost.Convert</em></span> 104 framework. 105 </li> 106</ul></div> 107<p> 108 The converter can easily replace <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span></code>, 109 adding flexibility and convenience: 110 </p> 111<p> 112</p> 113<pre class="programlisting"><span class="preprocessor">#include</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">hpp</span><span class="special">></span> 114<span class="preprocessor">#include</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">lexical_cast</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 115<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">detail</span><span class="special">/</span><span class="identifier">lightweight_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 116 117<span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">;</span> 118<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">convert</span><span class="special">;</span> 119<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span><span class="special">;</span> 120 121<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">lexical_cast</span> <span class="special">{};</span> 122</pre> 123<p> 124 </p> 125<p> 126</p> 127<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">i1</span> <span class="special">=</span> <span class="identifier">lexical_cast</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="comment">// Throws if the conversion fails.</span> 128<span class="keyword">int</span> <span class="identifier">i2</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">// Throws if the conversion fails.</span> 129<span class="keyword">int</span> <span class="identifier">i3</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">"uhm"</span><span class="special">).</span><span class="identifier">value_or</span><span class="special">(-</span><span class="number">1</span><span class="special">);</span> <span class="comment">// Returns -1 if the conversion fails.</span> 130<span class="identifier">string</span> <span class="identifier">s1</span> <span class="special">=</span> <span class="identifier">lexical_cast</span><span class="special"><</span><span class="identifier">string</span><span class="special">>(</span><span class="number">123</span><span class="special">);</span> 131<span class="identifier">string</span> <span class="identifier">s2</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special"><</span><span class="identifier">string</span><span class="special">>(</span><span class="number">123</span><span class="special">).</span><span class="identifier">value</span><span class="special">();</span> 132 133<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">i1</span> <span class="special">==</span> <span class="number">123</span><span class="special">);</span> 134<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">i2</span> <span class="special">==</span> <span class="number">123</span><span class="special">);</span> 135<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">i3</span> <span class="special">==</span> <span class="special">-</span><span class="number">1</span><span class="special">);</span> 136<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">s1</span> <span class="special">==</span> <span class="string">"123"</span><span class="special">);</span> 137<span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">s2</span> <span class="special">==</span> <span class="string">"123"</span><span class="special">);</span> 138</pre> 139<p> 140 </p> 141<p> 142 See the <code class="computeroutput"><a class="link" href="../boost/cnv/lexical_cast.html" title="Struct lexical_cast">boost::cnv::lexical_cast implementation</a></code> 143 for details. 144 </p> 145<p> 146 In order for a user-defined type to be integrated into the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span></code> framework and, consequently, 147 deployed with the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">lexical_cast</span></code> 148 converter: 149 </p> 150<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 151<li class="listitem"> 152 <span class="emphasis"><em>TypeOut</em></span> needs to be <a href="http://en.cppreference.com/w/cpp/named_req/CopyConstructible" target="_top"><span class="emphasis"><em>Copy 153 Constructible</em></span></a>; 154 </li> 155<li class="listitem"> 156 <span class="emphasis"><em>TypeOut</em></span> needs to be <a href="http://en.cppreference.com/w/cpp/named_req/DefaultConstructible" target="_top"><span class="emphasis"><em>Default 157 Constructible</em></span></a>; 158 </li> 159<li class="listitem"> 160 <span class="emphasis"><em>TypeOut</em></span> needs to be <span class="emphasis"><em>Input Streamable</em></span>; 161 </li> 162<li class="listitem"> 163 <span class="emphasis"><em>TypeIn</em></span> needs to be <span class="emphasis"><em>Output Streamable</em></span>. 164 </li> 165</ul></div> 166<p> 167 The first two requirements are imposed by the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span></code> 168 design and implementation and the last two requirements by the underlying 169 <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">stringstream</span></code> engine. 170 </p> 171</div> 172</div> 173<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 174<td align="left"></td> 175<td align="right"><div class="copyright-footer">Copyright © 2009-2016 Vladimir Batov<p> 176 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>. 177 </p> 178</div></td> 179</tr></table> 180<hr> 181<div class="spirit-nav"> 182<a accesskey="p" href="other_conversions.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="converters_detail/stream_converter.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 183</div> 184</body> 185</html> 186