forked from boostorg/conversion
https://svn.boost.org/svn/boost/trunk ........ r41679 | anthonyw | 2007-12-03 23:57:23 -0800 (Mon, 03 Dec 2007) | 1 line fixed typo in condition_variable_any::timed_wait ........ r41681 | anthonyw | 2007-12-04 01:15:37 -0800 (Tue, 04 Dec 2007) | 1 line changed boost::move to boost::detail::thread_move to fix issue #1492 ........ r41682 | anthonyw | 2007-12-04 02:04:30 -0800 (Tue, 04 Dec 2007) | 1 line split shared mutex tests in two to take less time ........ r41683 | anthonyw | 2007-12-04 03:44:25 -0800 (Tue, 04 Dec 2007) | 1 line don't dllexport/dllimport inline functions ........ r41684 | anthonyw | 2007-12-04 04:08:38 -0800 (Tue, 04 Dec 2007) | 1 line add explicit casts to remove warnings ........ r41686 | anthonyw | 2007-12-04 05:02:58 -0800 (Tue, 04 Dec 2007) | 1 line Added test for thread move constructor; implemented move on pthreads ........ r41687 | anthonyw | 2007-12-04 06:07:01 -0800 (Tue, 04 Dec 2007) | 1 line changed order of declaration to eliminate warnings ........ r41688 | nesotto | 2007-12-04 06:41:44 -0800 (Tue, 04 Dec 2007) | 1 line ticket 1488 ........ r41696 | fmhess | 2007-12-04 11:42:08 -0800 (Tue, 04 Dec 2007) | 2 lines Make sure output filenames don't include any illegal characters. ........ r41697 | fmhess | 2007-12-04 11:45:10 -0800 (Tue, 04 Dec 2007) | 4 lines Fixed printing of object name for nested classes (merge of revision 41421 from sandbox/boost_docs). ........ r41698 | fmhess | 2007-12-04 11:55:50 -0800 (Tue, 04 Dec 2007) | 17 lines Made generated ids for functions and overloaded functions human-readable which also results in (mostly) human-readable names for their .html output files. Made generate.id template properly use its node parameter (merge of parts of revision 41461 from sandbox/boost_docs). Made class name in synopsis a link to class reference page, which is useful for nested classes (merge of revision 41435 from sandbox/boost_docs). Removed spurious new line in synopisis between a nested class and the comment describing its purpose (merge of revision 41434 from sandbox/boost_docs). Added a warning if an "inherit" element has no "type" child element (partial merge of revision 41463 from sandbox/boost_docs). ........ r41701 | chris_kohlhoff | 2007-12-04 13:28:42 -0800 (Tue, 04 Dec 2007) | 2 lines Prevent deprecated function warnings for MSVC >= 8. ........ r41703 | nasonov | 2007-12-04 13:49:51 -0800 (Tue, 04 Dec 2007) | 1 line Link to html version of [Tuning] and BOOST_LEXICAL_CAST_ASSUME_C_LOCALE synopsis ........ r41704 | igaztanaga | 2007-12-04 14:05:28 -0800 (Tue, 04 Dec 2007) | 1 line Added Leopard workaround. _POSIX_THREAD_PROCESS_SHARED is defined but does not seem to work. For the moment, Mac OS will use emulation code ........ r41705 | djenkins | 2007-12-04 14:19:58 -0800 (Tue, 04 Dec 2007) | 1 line Fix msvc-9.0 code analysis problem ........ r41707 | noel_belcourt | 2007-12-04 15:18:38 -0800 (Tue, 04 Dec 2007) | 6 lines Fixes to get pgi shared libraries working, executables that don't core, and limiting the number of error messages emitted by the compiler so as to reduce the size of the log file. ........ r41727 | grafik | 2007-12-04 16:32:04 -0800 (Tue, 04 Dec 2007) | 1 line Switch FTP site for results. ........ r41734 | noel_belcourt | 2007-12-04 19:11:25 -0800 (Tue, 04 Dec 2007) | 5 lines Pgi compilers can't accept shared library with Boost version suffix appended to it. Add logic to tag rule in Jamroot to suppress appending version suffix for pgi toolset. ........ r41735 | noel_belcourt | 2007-12-04 19:20:30 -0800 (Tue, 04 Dec 2007) | 3 lines Patch pgi shared library use. ........ r41736 | grafik | 2007-12-04 20:33:36 -0800 (Tue, 04 Dec 2007) | 1 line Fix parsing of macosx version numbers from detected SDKs. ........ r41738 | anthonyw | 2007-12-05 00:27:44 -0800 (Wed, 05 Dec 2007) | 1 line added missing include of detail/config.hpp ........ r41739 | t_schwinger | 2007-12-05 02:24:21 -0800 (Wed, 05 Dec 2007) | 3 lines clarifies #error message ........ r41740 | t_schwinger | 2007-12-05 02:26:16 -0800 (Wed, 05 Dec 2007) | 2 lines updates function_types failures ........ r41741 | anthonyw | 2007-12-05 02:58:45 -0800 (Wed, 05 Dec 2007) | 1 line improved timeout checks ........ r41742 | t_schwinger | 2007-12-05 05:20:13 -0800 (Wed, 05 Dec 2007) | 3 lines attempts to fix strange problems with Pathscale compilers ........ r41762 | chris_kohlhoff | 2007-12-05 13:46:19 -0800 (Wed, 05 Dec 2007) | 2 lines Don't use deprecated function workaround when compiling for Windows CE. ........ r41770 | ramey | 2007-12-05 18:33:59 -0800 (Wed, 05 Dec 2007) | 2 lines change to fix failures with gcc 4.1+ modification to string input primitives ........ r41775 | djenkins | 2007-12-05 22:23:55 -0800 (Wed, 05 Dec 2007) | 1 line Avoid bug in Microsoft Code Analysis ........ [SVN r41776]
282 lines
14 KiB
HTML
282 lines
14 KiB
HTML
<!-- saved from url=(0022)http://internet.e-mail -->
|
|
<!doctype html public "-//W3C//DTD HTML Transitional 4.0//EN">
|
|
<html>
|
|
<head>
|
|
<title>lexical_cast</title>
|
|
<meta name="author" content="Kevlin Henney, mailto:kevlin@curbralan.com">
|
|
<meta name="generator" content="Microsoft FrontPage 5.0">
|
|
</head>
|
|
<body bgcolor="#FFFFFF" text="#000000">
|
|
<h1><img src="../../boost.png" alt="boost.png (6897 bytes)" align="center" width="277" height="86">Header
|
|
<a href="../../boost/lexical_cast.hpp">boost/lexical_cast.hpp</a></h1>
|
|
<ul type="square">
|
|
<li>
|
|
<a href="#motivation">Motivation</a></li>
|
|
<li>
|
|
<a href="#examples">Examples</a></li>
|
|
<li>
|
|
<a href="#synopsis">Synopsis</a></li>
|
|
<li>
|
|
<a href="#lexical_cast"><code>lexical_cast</code></a></li>
|
|
<li>
|
|
<a href="#bad_lexical_cast"><code>bad_lexical_cast</code></a></li>
|
|
<li>
|
|
<a href="#faq">Frequently Asked Questions</a></li>
|
|
<li>
|
|
<a href="#references">References</a></li>
|
|
<li>
|
|
<a href="#changes">Changes</a></li>
|
|
</ul>
|
|
<hr>
|
|
<h2><a name="motivation">Motivation</a></h2>
|
|
Sometimes a value must be converted to a literal text form, such as an <code>int</code>
|
|
represented as a <code>string</code>, or vice-versa, when a <code>string</code>
|
|
is interpreted as an <code>int</code>. Such examples are common when converting
|
|
between data types internal to a program and representation external to a
|
|
program, such as windows and configuration files.
|
|
<p>
|
|
The standard C and C++ libraries offer a number of facilities for performing
|
|
such conversions. However, they vary with their ease of use, extensibility, and
|
|
safety.
|
|
<p>
|
|
For instance, there are a number of limitations with the family of standard C
|
|
functions typified by <code>atoi</code>:
|
|
<ul type="square">
|
|
<li>
|
|
Conversion is supported in one direction only: from text to internal data type.
|
|
Converting the other way using the C library requires either the inconvenience
|
|
and compromised safety of the <code>sprintf</code> function, or the loss of
|
|
portability associated with non-standard functions such as <code>itoa</code>.
|
|
</li>
|
|
<li>
|
|
The range of types supported is only a subset of the built-in numeric types,
|
|
namely <code>int</code>, <code>long</code>, and <code>double</code>.
|
|
</li>
|
|
<li>
|
|
The range of types cannot be extended in a uniform manner. For instance,
|
|
conversion from string representation to <code>complex</code> or <code>rational</code>.
|
|
</li>
|
|
</ul>
|
|
The standard C functions typified by <code>strtol</code> have the same basic
|
|
limitations, but offer finer control over the conversion process. However, for
|
|
the common case such control is often either not required or not used. The <code>scanf</code>
|
|
family of functions offer even greater control, but also lack safety and ease
|
|
of use.
|
|
<p>
|
|
The standard C++ library offers <code>stringstream</code> for the kind of
|
|
in-core formatting being discussed. It offers a great deal of control over the
|
|
formatting and conversion of I/O to and from arbitrary types through text.
|
|
However, for simple conversions direct use of <code>stringstream</code> can be
|
|
either clumsy (with the introduction of extra local variables and the loss of
|
|
infix-expression convenience) or obscure (where <code>stringstream</code>
|
|
objects are created as temporary objects in an expression). Facets provide a
|
|
comprehensive concept and facility for controlling textual representation, but
|
|
their perceived complexity and high entry level requires an extreme degree of
|
|
involvement for simple conversions, and excludes all but a few programmers.
|
|
<p>
|
|
The <code>lexical_cast</code> function template offers a convenient and
|
|
consistent form for supporting common conversions to and from arbitrary types
|
|
when they are represented as text. The simplification it offers is in
|
|
expression-level convenience for such conversions. For more involved
|
|
conversions, such as where precision or formatting need tighter control than is
|
|
offered by the default behavior of <code>lexical_cast</code>, the conventional <code>
|
|
stringstream</code> approach is recommended. Where the conversions are
|
|
numeric to numeric, <code><a href="../numeric/conversion/doc/numeric_cast.html">numeric_cast</a></code>
|
|
may offer more reasonable behavior than <code>lexical_cast</code>.
|
|
<p>
|
|
For a good discussion of the options and issues involved in string-based
|
|
formatting, including comparison of <code>stringstream</code>, <code>lexical_cast</code>,
|
|
and others, see Herb Sutter's article, <a href="http://www.gotw.ca/publications/mill19.htm">
|
|
<i>The String Formatters of Manor Farm</i></a>.
|
|
<p>
|
|
<hr>
|
|
<h2><a name="examples">Examples</a></h2>
|
|
The following example treats command line arguments as a sequence of numeric
|
|
data: <blockquote>
|
|
<pre>int main(int argc, char * argv[])
|
|
{
|
|
using boost::lexical_cast;
|
|
using boost::bad_lexical_cast;
|
|
|
|
std::vector<short> args;
|
|
|
|
while(*++argv)
|
|
{
|
|
try
|
|
{
|
|
args.push_back(lexical_cast<short>(*argv));
|
|
}
|
|
catch(bad_lexical_cast &)
|
|
{
|
|
args.push_back(0);
|
|
}
|
|
}
|
|
...
|
|
}
|
|
</pre>
|
|
</blockquote>The following example uses numeric data in a string expression: <blockquote>
|
|
<pre>void log_message(const std::string &);
|
|
|
|
void log_errno(int yoko)
|
|
{
|
|
log_message("Error " + boost::lexical_cast<std::string>(yoko) + ": " + strerror(yoko));
|
|
}
|
|
</pre>
|
|
</blockquote>
|
|
<hr>
|
|
<h2><a name="synopsis">Synopsis</a></h2>
|
|
Library features defined in <a href="../../boost/lexical_cast.hpp"><code>"boost/lexical_cast.hpp"</code></a>:
|
|
<blockquote>
|
|
<pre>namespace boost
|
|
{
|
|
class <a href="#bad_lexical_cast">bad_lexical_cast</a>;
|
|
template<typename Target, typename Source>
|
|
Target <a href="#lexical_cast">lexical_cast</a>(const Source& arg);
|
|
}
|
|
</pre>
|
|
</blockquote>Unit test defined in <a href="lexical_cast_test.cpp"><code>"lexical_cast_test.cpp"</code></a>.
|
|
<p>
|
|
<hr>
|
|
<h2><a name="lexical_cast"><code>lexical_cast</code></a></h2>
|
|
<blockquote>
|
|
<pre>template<typename Target, typename Source>
|
|
Target lexical_cast(const Source& arg);
|
|
</pre>
|
|
</blockquote>Returns the result of streaming <code>arg</code> into a
|
|
standard library string-based stream and then out as a <code>Target</code> object.
|
|
Where <code>Target</code> is either <code>std::string</code>
|
|
or <code>std::wstring</code>, stream extraction takes the whole content
|
|
of the string, including spaces, rather than relying on the default
|
|
<code>operator>></code> behavior.
|
|
If the conversion is unsuccessful, a <a href="#bad_lexical_cast">
|
|
<code>bad_lexical_cast</code></a> exception is thrown.
|
|
<p>
|
|
The requirements on the argument and result types are:
|
|
<ul type="square">
|
|
<li>
|
|
<code>Source</code> is <i>OutputStreamable</i>, meaning that an <code>operator<<</code>
|
|
is defined that takes a <code>std::ostream</code> or <code>std::wostream</code> object on the
|
|
left hand side and an instance of the argument type on the right.
|
|
</li>
|
|
<li>
|
|
<code>Target</code> is <i>InputStreamable</i>, meaning that an <code>operator>></code>
|
|
is defined that takes a <code>std::istream</code> or <code>std::wistream</code> object on the left hand side
|
|
and an instance of the result type on the right.
|
|
</li>
|
|
<li>
|
|
<code>Target</code> is <i>CopyConstructible</i> [20.1.3].
|
|
</li>
|
|
<li>
|
|
<code>Target</code> is <i>DefaultConstructible</i>, meaning that it is possible
|
|
to <i>default-initialize</i> an object of that type [8.5, 20.1.4].
|
|
</li>
|
|
</ul>
|
|
The character type of the underlying stream is assumed to be <code>char</code> unless
|
|
either the <code>Source</code> or the <code>Target</code> requires wide-character
|
|
streaming, in which case the underlying stream uses <code>wchar_t</code>.
|
|
<code>Source</code> types that require wide-character streaming are <code>wchar_t</code>,
|
|
<code>wchar_t *</code>, and <code>std::wstring</code>. <code>Target</code> types that
|
|
require wide-character streaming are <code>wchar_t</code> and <code>std::wstring</code>.
|
|
<p>
|
|
Where a higher degree of control is required over conversions, <code>std::stringstream</code>
|
|
and <code>std::wstringstream</code> offer a more appropriate path. Where non-stream-based conversions are
|
|
required, <code>lexical_cast</code>
|
|
is the wrong tool for the job and is not special-cased for such scenarios.
|
|
<p>
|
|
<hr>
|
|
<h2><a name="bad_lexical_cast"><code>bad_lexical_cast</code></a></h2>
|
|
<blockquote>
|
|
<pre>class bad_lexical_cast : public std::bad_cast
|
|
{
|
|
public:
|
|
... // <i>same member function interface as</i> std::exception
|
|
};
|
|
</pre>
|
|
</blockquote>Exception used to indicate runtime <a href="#lexical_cast"><code>lexical_cast</code></a>
|
|
failure.
|
|
|
|
<hr>
|
|
<h2><a name="BOOST_LEXICAL_CAST_ASSUME_C_LOCALE"><code>BOOST_LEXICAL_CAST_ASSUME_C_LOCALE</code></a></h2>
|
|
<blockquote><pre>#define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
|
|
|
|
or,
|
|
|
|
g++ -DBOOST_LEXICAL_CAST_ASSUME_C_LOCALE ... (gcc on Linux/Unix)
|
|
cl.exe /DBOOST_LEXICAL_CAST_ASSUME_C_LOCALE ... (Visual C++ on Windows)
|
|
</pre>
|
|
Eliminate an overhead of <code>std::locale</code> if your program runs in the "C" locale. If the option is set but a program runs in other locale, <code>lexical_cast</code> result is unspecified.
|
|
<hr>
|
|
|
|
<h2><a name="faq">Frequently Asked Questions</h2>
|
|
<p> Q: Why does <code>lexical_cast<int8_t>("127")</code> throw <code>bad_lexical_cast</code>?
|
|
<br> A: The type <code>int8_t</code> is a typedef to <code>char</code> or <code>signed char</code>.
|
|
Lexical conversion to these types is simply reading a byte from source but since the source has
|
|
more than one byte, the exception is thrown.
|
|
<p>Please use other integer types such as <code>int</code> or <code>short int</code>. If bounds checking
|
|
is important, you can also call <a href="../../libs/numeric/conversion/doc/numeric_cast.html">numeric_cast</a>:
|
|
|
|
<pre><a href="../../libs/numeric/conversion/doc/numeric_cast.html">numeric_cast</a><int8_t>(lexical_cast<int>("127"));</pre>
|
|
|
|
<p> Q: What does <code>lexical_cast<std::string></code> of an <code>int8_t</code> or <code>uint8_t</code> not do what I expect?
|
|
<br> A: As above, note that <code>int8_t</code> and <code>uint8_t</code> are actually chars and are formatted as such. To avoid this, cast to an integer type first:
|
|
|
|
<pre>lexical_cast<std::string>(static_cast<int>(n));</pre>
|
|
|
|
<p> Q: The implementation always resets the <code>ios_base::skipws</code> flag of an underlying stream object. It breaks my <code>operator>></code> that works only in presence of this flag. Can you remove code that resets the flag?
|
|
<br> A: May be in a future version. There is no requirement in <a href="#n1973">[N1973]</a> to reset the flag but remember that <a href="#n1973">[N1973]</a> is not yet accepted by the committee. By the way, it's a great opportunity to make your <code>operator>></code> conform to the standard. Read a good C++ book, study <code>std::sentry</code> and <a href="../../libs/io/doc/ios_state.html">ios_state_saver</a>.
|
|
</ul>
|
|
<h2><a name="references">References</h2>
|
|
<ul type="square">
|
|
<a name="n1973"></a><li> [N1973] Kevlin Henney, Beman Dawes, Lexical Conversion Library Proposal for TR2,
|
|
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1973.html">N1973</a>.
|
|
<a name="tuning"></a><li> [Tuning] Alexander Nasonov, Fine Tuning for lexical_cast,
|
|
<a href="http://accu.org/index.php/journals/1375">Overload #74</a> (<a href="http://www.accu.org/var/uploads/journals/overload74.pdf">PDF</a>),
|
|
August 2006.</li>
|
|
</ul>
|
|
<h2><a name="changes">Changes</a></h2>
|
|
<h3>August, October 2006:</h3>
|
|
<ul type="square">
|
|
<li>Better performance for many combinations of <code>Source</code> and <code>Target</code>
|
|
types. Refer to <a href="#tuning">[Tuning]</a> for more details.
|
|
</li>
|
|
</ul>
|
|
<h3>June 2005:</h3>
|
|
<ul type="square">
|
|
<li>Call-by-const reference for the parameters. This requires partial specialization
|
|
of class templates, so it doesn't work for MSVC 6, and it uses the original
|
|
pass by value there.<br>
|
|
</li>
|
|
<li>The MSVC 6 support is deprecated, and will be removed in a future Boost
|
|
version. </li>
|
|
</ul>
|
|
<h3>Earlier:</h3>
|
|
|
|
<ul type="square">
|
|
<li>The previous version of <code>lexical_cast</code> used the default stream
|
|
precision for reading and writing floating-point numbers. For numerics that
|
|
have a corresponding specialization of <code>std::numeric_limits</code>, the
|
|
current version now chooses a precision to match. <br>
|
|
<li>The previous version of <code>lexical_cast</code> did not support conversion
|
|
to or from any wide-character-based types. For compilers with full language
|
|
and library support for wide characters, <code>lexical_cast</code> now supports
|
|
conversions from <code>wchar_t</code>, <code>wchar_t *</code>, and <code>std::wstring</code>
|
|
and to <code>wchar_t</code> and <code>std::wstring</code>. <br>
|
|
<li>The previous version of <code>lexical_cast</code> assumed that the conventional
|
|
stream extractor operators were sufficient for reading values. However, string
|
|
I/O is asymmetric, with the result that spaces play the role of I/O separators
|
|
rather than string content. The current version fixes this error for <code>std::string</code>
|
|
and, where supported, <code>std::wstring</code>: <code>lexical_cast<std::string>("Hello,
|
|
World")</code> succeeds instead of failing with a <code>bad_lexical_cast</code>
|
|
exception. <br>
|
|
<li>The previous version of <code>lexical_cast</code> allowed unsafe and meaningless
|
|
conversions to pointers. The current version now throws a <code>bad_lexical_cast</code>
|
|
for conversions to pointers: <code>lexical_cast<char *>("Goodbye, World")</code>
|
|
now throws an exception instead of causing undefined behavior.
|
|
</ul>
|
|
<p>
|
|
<hr>
|
|
|
|
<div align="right"><small><i>© Copyright Kevlin Henney, 2000–2005</i></small></div>
|
|
</body>
|
|
</html>
|