boost/libs/convert/doc/html/boost_convert/converters_detail.html
2021-10-05 21:37:46 +02:00

186 lines
16 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Converters in Detail</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter 1. Boost.Convert 2.0">
<link rel="up" href="../index.html" title="Chapter 1. Boost.Convert 2.0">
<link rel="prev" href="other_conversions.html" title="Beyond Basic Conversions">
<link rel="next" href="converters_detail/stream_converter.html" title="boost::cnv::stream Converter">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_convert.converters_detail"></a><a class="link" href="converters_detail.html" title="Converters in Detail">Converters in Detail</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<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>
Converter</a></span></dt>
<dt><span class="section"><a href="converters_detail/stream_converter.html"><span class="emphasis"><em>boost::cnv::stream</em></span>
Converter</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="converters_detail/stream_converter.html#boost_convert.converters_detail.stream_converter.formatting_support">Formatting
Support</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="converters_detail/stream_converter.html#boost_convert.converters_detail.stream_converter.formatting_support.numeric_base">Numeric
Base</a></span></dt>
<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
Width, Fill Character and Adjustment</a></span></dt>
<dt><span class="section"><a href="converters_detail/stream_converter.html#boost_convert.converters_detail.stream_converter.formatting_support.leading_whitespace_characters">Leading
Whitespace Characters</a></span></dt>
<dt><span class="section"><a href="converters_detail/stream_converter.html#boost_convert.converters_detail.stream_converter.formatting_support.format_of_boolean_values">Format
of Boolean Values</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="converters_detail/stream_converter/locale_support.html">Locale
Support</a></span></dt>
<dt><span class="section"><a href="converters_detail/stream_converter/supported_string_types.html">Supported
String Types</a></span></dt>
<dd><dl>
<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
String</a></span></dt>
<dt><span class="section"><a href="converters_detail/stream_converter/supported_string_types/custom_string_types.html">Custom
String Types</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="converters_detail/stream_converter/the___default_constructible__type_requirement.html">The
<span class="emphasis"><em>Default Constructible</em></span> Type Requirement</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="converters_detail/strtol_converter.html"><span class="emphasis"><em>boost::cnv::strtol</em></span>
Converter</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="converters_detail/strtol_converter.html#boost_convert.converters_detail.strtol_converter.basic_deployment">Basic
Deployment</a></span></dt>
<dt><span class="section"><a href="converters_detail/strtol_converter/formatting_support.html">Formatting
Support</a></span></dt>
<dd><dl>
<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
Base (bin, oct, dec, hex)</a></span></dt>
<dt><span class="section"><a href="converters_detail/strtol_converter/formatting_support/field_width__fill_character_and_adjustment.html">Field
Width, Fill Character and Adjustment</a></span></dt>
<dt><span class="section"><a href="converters_detail/strtol_converter/formatting_support/leading_whitespace_characters.html">Leading
Whitespace Characters</a></span></dt>
<dt><span class="section"><a href="converters_detail/strtol_converter/formatting_support/floating_point_precision.html">Floating-Point
Precision</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="converters_detail/strtol_converter/supported_string_types.html">Supported
String Types</a></span></dt>
<dd><dl>
<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
String</a></span></dt>
<dt><span class="section"><a href="converters_detail/strtol_converter/supported_string_types/custom_string_types.html">Custom
String Types</a></span></dt>
</dl></dd>
</dl></dd>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<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>
Converter</a>
</h3></div></div></div>
<p>
The purpose of the converter is to
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
Make use of the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span></code>
functionality and performance that many people have become accustomed
to and comfortable with;
</li>
<li class="listitem">
Demonstrate how existing independent conversion/transformation-related
facilities might be incorporated in to the <span class="emphasis"><em>Boost.Convert</em></span>
framework.
</li>
</ul></div>
<p>
The converter can easily replace <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span></code>,
adding flexibility and convenience:
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
<span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">;</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">convert</span><span class="special">;</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span><span class="special">;</span>
<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>
</pre>
<p>
</p>
<p>
</p>
<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">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="string">"123"</span><span class="special">);</span> <span class="comment">// Throws if the conversion fails.</span>
<span class="keyword">int</span> <span class="identifier">i2</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</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>
<span class="keyword">int</span> <span class="identifier">i3</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</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>
<span class="identifier">string</span> <span class="identifier">s1</span> <span class="special">=</span> <span class="identifier">lexical_cast</span><span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;(</span><span class="number">123</span><span class="special">);</span>
<span class="identifier">string</span> <span class="identifier">s2</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;(</span><span class="number">123</span><span class="special">).</span><span class="identifier">value</span><span class="special">();</span>
<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>
<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>
<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>
<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>
<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>
</pre>
<p>
</p>
<p>
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>
for details.
</p>
<p>
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,
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>
converter:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<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
Constructible</em></span></a>;
</li>
<li class="listitem">
<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
Constructible</em></span></a>;
</li>
<li class="listitem">
<span class="emphasis"><em>TypeOut</em></span> needs to be <span class="emphasis"><em>Input Streamable</em></span>;
</li>
<li class="listitem">
<span class="emphasis"><em>TypeIn</em></span> needs to be <span class="emphasis"><em>Output Streamable</em></span>.
</li>
</ul></div>
<p>
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>
design and implementation and the last two requirements by the underlying
<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">stringstream</span></code> engine.
</p>
</div>
</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 © 2009-2020 Vladimir Batov<p>
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>.
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
</body>
</html>