mirror of
https://github.com/boostorg/conversion.git
synced 2026-08-06 13:34:14 +02:00
Compare commits
102 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 098d495b8a | |||
| e51cf081b3 | |||
| 51a6aacb57 | |||
| a2e4606e6c | |||
| 82abf7b54f | |||
| 7112ded1b9 | |||
| 1eda87448e | |||
| 3a62368d0e | |||
| e1caac418c | |||
| 8480a6d083 | |||
| 00f1246faf | |||
| 6c5f31e7a5 | |||
| 1f7147d24b | |||
| a385c3ec99 | |||
| 701d5f0bf0 | |||
| b5583b88fa | |||
| 5b6e39b2b8 | |||
| 04f73e5a89 | |||
| 65328d9716 | |||
| dc151d2b5a | |||
| 23949682c1 | |||
| 5055e842cf | |||
| 9e764f3e49 | |||
| a3e690da32 | |||
| fb681bb0ea | |||
| 98a67d93f3 | |||
| 0c54d6a037 | |||
| 39329bc5b9 | |||
| 2ce20c872e | |||
| 49c0b2f333 | |||
| e9a5ac6d7a | |||
| ee3f643c5e | |||
| d44a6fcf8b | |||
| ac6e41d676 | |||
| 4224fa5deb | |||
| d6eea4ef81 | |||
| 37d07617d1 | |||
| bfeb60c400 | |||
| 78f1acc5ff | |||
| abf3aa23de | |||
| 06e7d986d2 | |||
| 74fc01da7e | |||
| 80e9edcb29 | |||
| 17ca3a4493 | |||
| e5a0aec661 | |||
| 8cec2cc291 | |||
| 027a566e09 | |||
| 9a29a753e8 | |||
| cb2bd8076d | |||
| 1637edf8ca | |||
| 72d69ef6ad | |||
| 1470210523 | |||
| 0b19a0ae27 | |||
| 3ab685475c | |||
| e66d58e6de | |||
| e331fc2a21 | |||
| 2a65633517 | |||
| 7a388cb1a6 | |||
| 461dce3851 | |||
| cfe358b570 | |||
| caa875505e | |||
| 462025d8e6 | |||
| b8c373ab47 | |||
| f6b0ec3685 | |||
| a096a4b153 | |||
| bba9169401 | |||
| f11967a2c8 | |||
| c1c338990c | |||
| ec35f00bc3 | |||
| abf8ee0eb9 | |||
| 463284f46f | |||
| 9f5ec17603 | |||
| 575c477f54 | |||
| e6c52ae0f4 | |||
| 13796f3f98 | |||
| 80a6db56c1 | |||
| 25935cda40 | |||
| 386d7a67d6 | |||
| 4b3e8171fa | |||
| 498c84dd4f | |||
| 3ecac311fd | |||
| 8828af5f5b | |||
| 1937e3a12a | |||
| 545687e22b | |||
| 9c63f9fbd0 | |||
| 9c1311a712 | |||
| 51395d7b26 | |||
| 02609a4675 | |||
| 52de514faf | |||
| ae7d610cb1 | |||
| eb9d2c02ea | |||
| e2825ab729 | |||
| 4978f3a79f | |||
| 42639f120a | |||
| 0d7e048f06 | |||
| 95e0465334 | |||
| 0fba1d9f7d | |||
| 3597b25802 | |||
| ccf7efe620 | |||
| 7d3864be03 | |||
| cb5b83ef53 | |||
| 0ec92f1f03 |
@@ -1,64 +1,90 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<meta name="generator" content=
|
||||
"Microsoft FrontPage 5.0">
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=windows-1252">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||
<title>Header boost/cast.hpp Documentation</title>
|
||||
</head>
|
||||
<title>Header boost/cast.hpp Documentation</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" text="#000000">
|
||||
<body bgcolor="#FFFFFF" text="#000000">
|
||||
<h1><img src="../../boost.png" alt="boost.png (6897 bytes)" align=
|
||||
"middle" width="277" height="86">Header <a href=
|
||||
"../../boost/cast.hpp">boost/cast.hpp</a></h1>
|
||||
|
||||
<h1><img src="../../c++boost.gif" alt="c++boost.gif (8819 bytes)" align="middle" width="277" height="86">Header
|
||||
<a href="../../boost/cast.hpp">boost/cast.hpp</a></h1>
|
||||
<h2><a name="Cast Functions">Cast Functions</a></h2>
|
||||
|
||||
<h2><a name="Cast Functions">Cast Functions</a></h2>
|
||||
<p>The header <a href="../../boost/cast.hpp">boost/cast.hpp</a> provides <code>
|
||||
<a href="#Polymorphic_cast">polymorphic_cast</a> and</code> <a href=
|
||||
"#Polymorphic_cast"><code>polymorphic_downcast</code></a> function templates designed to
|
||||
complement the C++ built-in casts.</p>
|
||||
|
||||
<p>The header <a href="../../boost/cast.hpp">boost/cast.hpp</a>
|
||||
provides <a href="#Polymorphic_cast"><b>polymorphic_cast</b></a>, <a href="#Polymorphic_cast"><b>polymorphic_downcast</b></a>,
|
||||
and <a href="#numeric_cast"><b>numeric_cast</b></a> function templates designed
|
||||
to complement the C++ built-in casts.</p>
|
||||
<p>The program <a href="cast_test.cpp">cast_test.cpp</a> can be used to
|
||||
verify these function templates work as expected.</p>
|
||||
|
||||
<p>The program <a href="cast_test.cpp">cast_test.cpp</a> can be used to
|
||||
verify these function templates work as expected.</p>
|
||||
<h3><a name="Polymorphic_cast">Polymorphic casts</a></h3>
|
||||
<p>Pointers to polymorphic objects (objects of classes which define at least one
|
||||
virtual function) are sometimes downcast or crosscast. Downcasting means
|
||||
casting from a base class to a derived class. Crosscasting means casting
|
||||
across an inheritance hierarchy diagram, such as from one base to the other in a
|
||||
<b>Y</b> diagram hierarchy.</p>
|
||||
<p>Such casts can be done with old-style casts, but this approach is never to be
|
||||
recommended. Old-style casts are sorely lacking in type safety, suffer
|
||||
poor readability, and are difficult to locate with search tools.</p>
|
||||
<p>The C++ built-in <b>static_cast</b> can be used for efficiently downcasting
|
||||
pointers to polymorphic objects, but provides no error detection for the case
|
||||
where the pointer being cast actually points to the wrong derived class. The <b>polymorphic_downcast</b>
|
||||
template retains the efficiency of <b>static_cast</b> for non-debug
|
||||
compilations, but for debug compilations adds safety via an assert() that a <b>dynamic_cast</b>
|
||||
succeeds.</p>
|
||||
<p>The C++ built-in <b>dynamic_cast</b> can be used for downcasts and crosscasts
|
||||
of pointers to polymorphic objects, but error notification in the form of a
|
||||
returned value of 0 is inconvenient to test, or worse yet, easy to forget to
|
||||
test. The <b>polymorphic_cast</b> template performs a <b>dynamic_cast</b>,
|
||||
and throws an exception if the <b>dynamic_cast</b> returns 0.</p>
|
||||
<p>A <b>polymorphic_downcast</b> is preferred when debug-mode tests will cover
|
||||
100% of the object types possibly cast and when non-debug-mode efficiency is an
|
||||
issue. If these two conditions are not present, <b>polymorphic_cast</b> is
|
||||
preferred. It must also be used for crosscasts. It does an assert(
|
||||
dynamic_cast<Derived>(x) == x ) where x is the base pointer, ensuring that
|
||||
not only is a non-zero pointer returned, but also that it correct in the
|
||||
presence of multiple inheritance.<b> Warning:</b>: Because <b>polymorphic_downcast</b>
|
||||
uses assert(), it violates the one definition rule (ODR) if NDEBUG is inconsistently
|
||||
defined across translation units. [See ISO Std 3.2]</p>
|
||||
<p>The C++ built-in <b>dynamic_cast</b> must be used to cast references rather
|
||||
than pointers. It is also the only cast that can be used to check whether
|
||||
a given interface is supported; in that case a return of 0 isn't an error
|
||||
condition.</p>
|
||||
<h3>polymorphic_cast and polymorphic_downcast synopsis</h3>
|
||||
<blockquote>
|
||||
<pre>namespace boost {
|
||||
<h3><a name="Polymorphic_cast">Polymorphic casts</a></h3>
|
||||
|
||||
<p>Pointers to polymorphic objects (objects of classes which define at
|
||||
least one virtual function) are sometimes downcast or crosscast.
|
||||
Downcasting means casting from a base class to a derived class.
|
||||
Crosscasting means casting across an inheritance hierarchy diagram, such
|
||||
as from one base to the other in a <code>Y</code> diagram hierarchy.</p>
|
||||
|
||||
<p>Such casts can be done with old-style casts, but this approach is
|
||||
never to be recommended. Old-style casts are sorely lacking in type
|
||||
safety, suffer poor readability, and are difficult to locate with search
|
||||
tools.</p>
|
||||
|
||||
<p>The C++ built-in <code>static_cast</code> can be used for efficiently
|
||||
downcasting pointers to polymorphic objects, but provides no error
|
||||
detection for the case where the pointer being cast actually points to
|
||||
the wrong derived class. The <code>polymorphic_downcast</code> template retains
|
||||
the efficiency of <code>static_cast</code> for non-debug compilations, but for
|
||||
debug compilations adds safety via an assert() that a <code>dynamic_cast</code>
|
||||
succeeds.</p>
|
||||
|
||||
<p>The C++ built-in <code>dynamic_cast</code> can be used for downcasts and
|
||||
crosscasts of pointers to polymorphic objects, but error notification in
|
||||
the form of a returned value of 0 is inconvenient to test, or worse yet,
|
||||
easy to forget to test. The throwing form of <code>dynamic_cast</code>, which
|
||||
works on references, can be used on pointers through the ugly expression
|
||||
&<code>dynamic_cast<T&>(*p)</code>, which causes undefined
|
||||
behavior if <code>p</code> is <code>0</code>. The <code>polymorphic_cast</code>
|
||||
template performs a <code>dynamic_cast</code> on a pointer, and throws an
|
||||
exception if the <code>dynamic_cast</code> returns 0.</p>
|
||||
|
||||
<p>A <code>polymorphic_downcast</code> should be used for
|
||||
downcasts that you are certain should succeed. Error checking is
|
||||
only performed in translation units where <code>NDEBUG</code> is
|
||||
not defined, via
|
||||
<pre> assert( dynamic_cast<Derived>(x) == x )
|
||||
</pre> where <code>x</code> is the source pointer. This approach
|
||||
ensures that not only is a non-zero pointer returned, but also
|
||||
that it is correct in the presence of multiple inheritance.
|
||||
Attempts to crosscast using <code>polymorphic_downcast</code> will
|
||||
fail to compile.
|
||||
<b>Warning:</b> Because <code>polymorphic_downcast</code> uses assert(), it
|
||||
violates the One Definition Rule (ODR) if NDEBUG is inconsistently
|
||||
defined across translation units. [See ISO Std 3.2]
|
||||
</p><p>
|
||||
For crosscasts, or when the success of a cast can only be known at
|
||||
runtime, or when efficiency is not important,
|
||||
<code>polymorphic_cast</code> is preferred. </p>
|
||||
|
||||
<p>The C++ built-in <code>dynamic_cast</code> must be used to cast references
|
||||
rather than pointers. It is also the only cast that can be used to check
|
||||
whether a given interface is supported; in that case a return of 0 isn't
|
||||
an error condition.</p>
|
||||
|
||||
<h3>polymorphic_cast and polymorphic_downcast synopsis</h3>
|
||||
|
||||
<blockquote>
|
||||
<pre>namespace boost {
|
||||
|
||||
template <class Derived, class Base>
|
||||
inline Derived polymorphic_cast(Base* x);
|
||||
@@ -70,11 +96,14 @@ inline Derived polymorphic_downcast(Base* x);
|
||||
// Effects: assert( dynamic_cast<Derived>(x) == x );
|
||||
// Returns: static_cast<Derived>(x)
|
||||
|
||||
}</pre>
|
||||
</blockquote>
|
||||
<h3>polymorphic_downcast example</h3>
|
||||
<blockquote>
|
||||
<pre>#include <boost/cast.hpp>
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<h3>polymorphic_downcast example</h3>
|
||||
|
||||
<blockquote>
|
||||
<pre>#include <boost/cast.hpp>
|
||||
...
|
||||
class Fruit { public: virtual ~Fruit(){}; ... };
|
||||
class Banana : public Fruit { ... };
|
||||
@@ -82,69 +111,30 @@ class Banana : public Fruit { ... };
|
||||
void f( Fruit * fruit ) {
|
||||
// ... logic which leads us to believe it is a Banana
|
||||
Banana * banana = boost::polymorphic_downcast<Banana*>(fruit);
|
||||
...</pre>
|
||||
</blockquote>
|
||||
<h3><a name="numeric_cast">numeric_cast</a></h3>
|
||||
<p>A <b>static_cast</b> or implicit conversion will not
|
||||
detect failure to preserve range for numeric casts. The <b>numeric_cast</b> function
|
||||
templates are similar to <b>static_cast</b> and certain (dubious)
|
||||
implicit conversions in this respect, except that they detect loss of numeric
|
||||
range. An exception is thrown when a runtime value-preservation check fails.</p>
|
||||
<p>The requirements on the argument and result types are:</p>
|
||||
<blockquote>
|
||||
<ul>
|
||||
<li>Both argument and result types are CopyConstructible [ISO Std 20.1.3].</li>
|
||||
<li>Both argument and result types are Numeric, defined by <code>std::numeric_limits<>::is_specialized</code>
|
||||
being true.</li>
|
||||
<li>The argument can be converted to the result type using <b>static_cast</b>.</li>
|
||||
</ul>
|
||||
</blockquote>
|
||||
<h3>numeric_cast synopsis</h3>
|
||||
<blockquote>
|
||||
<pre>namespace boost {
|
||||
...
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
class bad_numeric_cast : public std::bad_cast {...};
|
||||
<h3>History</h3>
|
||||
|
||||
template<typename Target, typename Source>
|
||||
inline Target numeric_cast(Source arg);
|
||||
// Throws: bad_numeric_cast unless, in converting arg from Source to Target,
|
||||
// there is no loss of negative range, and no underflow, and no
|
||||
// overflow, as determined by std::numeric_limits
|
||||
// Returns: static_cast<Target>(arg)
|
||||
<p><code>polymorphic_cast</code> was suggested by Bjarne Stroustrup in "The C++
|
||||
Programming Language".<br>
|
||||
<code>polymorphic_downcast</code> was contributed by <a href=
|
||||
"http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a>.<code><br>
|
||||
An old
|
||||
numeric_cast</code> that was contributed by <a href=
|
||||
"http://www.boost.org/people/kevlin_henney.htm">Kevlin Henney</a> is now superseeded by the <a href="../numeric/conversion/doc/html/index.html">Boost Numeric Conversion Library</a></p>
|
||||
<hr>
|
||||
|
||||
}</pre>
|
||||
</blockquote>
|
||||
<h3>numeric_cast example</h3>
|
||||
<blockquote>
|
||||
<pre>#include <boost/cast.hpp>
|
||||
using namespace boost::cast;
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan
|
||||
-->June 23, 2005<!--webbot bot="Timestamp" endspan i-checksum="30348"
|
||||
--></p>
|
||||
|
||||
void ariane(double vx)
|
||||
{
|
||||
...
|
||||
unsigned short dx = numeric_cast<unsigned short>(vx);
|
||||
...
|
||||
}</pre>
|
||||
</blockquote>
|
||||
<h3>numeric_cast rationale</h3>
|
||||
<p>The form of the throws condition is specified so that != is not a required
|
||||
operation.</p>
|
||||
<h3>History</h3>
|
||||
<p><b>polymorphic_cast</b> was suggested by Bjarne Stroustrup in "The C++
|
||||
Programming Language".<br>
|
||||
<b>polymorphic_downcast</b> was contributed by <a href="../../people/dave_abrahams.htm">Dave
|
||||
Abrahams</a>.<b><br>
|
||||
numeric_cast</b> was contributed by <a href="../../people/kevlin_henney.htm">Kevlin
|
||||
Henney</a>.</p>
|
||||
<hr>
|
||||
<p>Revised <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan
|
||||
-->06 January, 2001<!--webbot bot="Timestamp" endspan i-checksum="38320"
|
||||
--></p>
|
||||
<p>© Copyright boost.org 1999. Permission to copy, use, modify, sell and
|
||||
distribute this document is granted provided this copyright notice appears in
|
||||
all copies. This document is provided "as is" without express or
|
||||
implied warranty, and with no claim as to its suitability for any purpose.</p>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
<p>© Copyright boost.org 1999. Permission to copy, use, modify, sell
|
||||
and distribute this document is granted provided this copyright notice
|
||||
appears in all copies. This document is provided "as is" without express
|
||||
or implied warranty, and with no claim as to its suitability for any
|
||||
purpose.</p>
|
||||
</body>
|
||||
</html>
|
||||
+13
-76
@@ -1,14 +1,13 @@
|
||||
// boost utility cast test program -----------------------------------------//
|
||||
|
||||
// (C) Copyright boost.org 1999. Permission to copy, use, modify, sell
|
||||
// and distribute this software is granted provided this copyright
|
||||
// notice appears in all copies. This software is provided "as is" without
|
||||
// express or implied warranty, and with no claim as to its suitability for
|
||||
// any purpose.
|
||||
// (C) Copyright Beman Dawes, Dave Abrahams 1999. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
|
||||
// Revision History
|
||||
// 28 Set 04 factored out numeric_cast<> test (Fernando Cacciola)
|
||||
// 20 Jan 01 removed use of <limits> for portability to raw GCC (David Abrahams)
|
||||
// 28 Jun 00 implicit_cast removed (Beman Dawes)
|
||||
// 30 Aug 99 value_cast replaced by numeric_cast
|
||||
@@ -21,7 +20,7 @@
|
||||
|
||||
# if SCHAR_MAX == LONG_MAX
|
||||
# error "This test program doesn't work if SCHAR_MAX == LONG_MAX"
|
||||
# endif
|
||||
# endif
|
||||
|
||||
using namespace boost;
|
||||
using std::cout;
|
||||
@@ -29,39 +28,34 @@ using std::cout;
|
||||
namespace
|
||||
{
|
||||
struct Base
|
||||
{
|
||||
{
|
||||
virtual char kind() { return 'B'; }
|
||||
};
|
||||
|
||||
|
||||
struct Base2
|
||||
{
|
||||
{
|
||||
virtual char kind2() { return '2'; }
|
||||
};
|
||||
|
||||
|
||||
struct Derived : public Base, Base2
|
||||
{
|
||||
virtual char kind() { return 'D'; }
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
int main( int argc, char * argv[] )
|
||||
{
|
||||
cout << "Usage: test_casts [n], where n omitted or is:\n"
|
||||
" 1 = execute #1 assert failure (#ifndef NDEBUG)\n"
|
||||
" 2 = execute #2 assert failure (#ifndef NDEBUG)\n"
|
||||
"Example: test_casts 2\n\n";
|
||||
|
||||
# ifdef NDEBUG
|
||||
cout << "NDEBUG is defined\n";
|
||||
# else
|
||||
cout << "NDEBUG is not defined\n";
|
||||
# endif
|
||||
|
||||
cout << "\nBeginning tests...\n";
|
||||
cout << "\nBeginning tests...\n";
|
||||
|
||||
// test polymorphic_cast ---------------------------------------------------//
|
||||
|
||||
|
||||
// tests which should succeed
|
||||
Base * base = new Derived;
|
||||
Base2 * base2 = 0;
|
||||
@@ -91,63 +85,6 @@ int main( int argc, char * argv[] )
|
||||
// the following is just so generated code can be inspected
|
||||
if ( derived->kind() == 'B' ) ++err_count;
|
||||
|
||||
// test implicit_cast and numeric_cast -------------------------------------//
|
||||
|
||||
// tests which should succeed
|
||||
long small_value = 1;
|
||||
long small_negative_value = -1;
|
||||
long large_value = LONG_MAX;
|
||||
long large_negative_value = LONG_MIN;
|
||||
signed char c = 0;
|
||||
|
||||
c = large_value; // see if compiler generates warning
|
||||
|
||||
c = numeric_cast<signed char>( small_value );
|
||||
assert( c == 1 );
|
||||
c = 0;
|
||||
c = numeric_cast<signed char>( small_value );
|
||||
assert( c == 1 );
|
||||
c = 0;
|
||||
c = numeric_cast<signed char>( small_negative_value );
|
||||
assert( c == -1 );
|
||||
|
||||
// These tests courtesy of Joe R NWP Swatosh<joe.r.swatosh@usace.army.mil>
|
||||
assert( 0.0f == numeric_cast<float>( 0.0 ) );
|
||||
assert( 0.0 == numeric_cast<double>( 0.0 ) );
|
||||
|
||||
// tests which should result in errors being detected
|
||||
|
||||
caught_exception = false;
|
||||
try { c = numeric_cast<signed char>( large_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #1\n"; caught_exception = true; }
|
||||
if ( !caught_exception ) ++err_count;
|
||||
|
||||
caught_exception = false;
|
||||
try { c = numeric_cast<signed char>( large_negative_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #2\n"; caught_exception = true; }
|
||||
if ( !caught_exception ) ++err_count;
|
||||
|
||||
unsigned long ul;
|
||||
caught_exception = false;
|
||||
try { ul = numeric_cast<unsigned long>( large_negative_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #3\n"; caught_exception = true; }
|
||||
if ( !caught_exception ) ++err_count;
|
||||
|
||||
caught_exception = false;
|
||||
try { ul = numeric_cast<unsigned long>( small_negative_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #4\n"; caught_exception = true; }
|
||||
if ( !caught_exception ) ++err_count;
|
||||
|
||||
caught_exception = false;
|
||||
try { numeric_cast<int>( DBL_MAX ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #5\n"; caught_exception = true; }
|
||||
if ( !caught_exception ) ++err_count;
|
||||
|
||||
cout << err_count << " errors detected\nTest "
|
||||
<< (err_count==0 ? "passed\n" : "failed\n");
|
||||
return err_count;
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
# Copyright Antony Polukhin 2011. Use, modification, and distribution are
|
||||
# subject to the Boost Software License, Version 1.0. (See accompanying
|
||||
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
using quickbook ;
|
||||
import boostbook : boostbook ;
|
||||
|
||||
xml lexical_cast : lexical_cast.qbk ;
|
||||
boostbook standalone
|
||||
:
|
||||
lexical_cast
|
||||
:
|
||||
<xsl:param>boost.root=../../../..
|
||||
<format>pdf:<xsl:param>boost.url.prefix=http://www.boost.org/doc/libs/release/doc/html
|
||||
;
|
||||
|
||||
@@ -0,0 +1,620 @@
|
||||
[library Boost.Lexical_Cast
|
||||
[quickbook 1.5]
|
||||
[version 1.0]
|
||||
[copyright 2000-2005 Kevlin Henney]
|
||||
[copyright 2006-2010 Alexander Nasonov]
|
||||
[copyright 2011 Antony Polukhin]
|
||||
[category String and text processing]
|
||||
[category Miscellaneous]
|
||||
[license
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
[@http://www.boost.org/LICENSE_1_0.txt])
|
||||
]
|
||||
]
|
||||
|
||||
[def __numericcast__ [@boost:libs/numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__.html `boost::numeric_cast`]]
|
||||
[def __proposallong__ [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1973.html Lexical Conversion Library Proposal for TR2, N1973 by Kevlin Henney and Beman Dawes]]
|
||||
[def __proposalshort__ [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1973.html Lexical Conversion Library Proposal for TR2, N1973]]
|
||||
|
||||
[section Motivation]
|
||||
Sometimes a value must be converted to a literal text form, such as an [c++] `int` represented as a `std::string`, or vice-versa, when a `std::string` is interpreted as an `int`. 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.
|
||||
|
||||
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.
|
||||
|
||||
For instance, there are a number of limitations with the family of standard C functions typified by `atoi`:
|
||||
|
||||
* 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 `sprintf` function, or the loss of portability associated with non-standard functions such as `itoa`.
|
||||
* The range of types supported is only a subset of the built-in numeric types, namely `int`, `long`, and `double`.
|
||||
* The range of types cannot be extended in a uniform manner. For instance, conversion from string representation to complex or rational.
|
||||
|
||||
The standard C functions typified by `strtol` 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 `scanf` family of functions offer even greater control, but also lack safety and ease of use.
|
||||
|
||||
The standard C++ library offers `stringstream` 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 `stringstream` can be either clumsy (with the introduction of extra local variables and the loss of infix-expression convenience) or obscure (where `stringstream` 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.
|
||||
|
||||
The `lexical_cast` 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 `lexical_cast`, the conventional `std::stringstream` approach is recommended. Where the conversions are numeric to numeric, __numericcast__ may offer more reasonable behavior than `lexical_cast`.
|
||||
|
||||
For a good discussion of the options and issues involved in string-based formatting, including comparison of `stringstream`, `lexical_cast`, and others, see Herb Sutter's article, [@http://www.gotw.ca/publications/mill19.htm The String Formatters of Manor Farm]. Also, take a look at the [link boost_lexical_cast.performance Performance] section.
|
||||
[endsect]
|
||||
|
||||
[section Examples]
|
||||
The following example treats command line arguments as a sequence of numeric data:
|
||||
``
|
||||
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);
|
||||
}
|
||||
}
|
||||
...
|
||||
}
|
||||
``
|
||||
The following example uses numeric data in a string expression:
|
||||
``
|
||||
void log_message(const std::string &);
|
||||
|
||||
void log_errno(int yoko)
|
||||
{
|
||||
log_message("Error " + boost::lexical_cast<std::string>(yoko) + ": " + strerror(yoko));
|
||||
}
|
||||
``
|
||||
[endsect]
|
||||
|
||||
[section Synopsis]
|
||||
Library features defined in [@boost:boost/lexical_cast.hpp boost/lexical_cast.hpp]:
|
||||
``
|
||||
namespace boost
|
||||
{
|
||||
class bad_lexical_cast;
|
||||
template<typename Target, typename Source>
|
||||
Target lexical_cast(const Source& arg);
|
||||
}
|
||||
``
|
||||
|
||||
[section lexical_cast]
|
||||
``
|
||||
template<typename Target, typename Source>
|
||||
Target lexical_cast(const Source& arg);
|
||||
``
|
||||
Returns the result of streaming arg into a standard library string-based stream and then out as a Target object. Where Target is either `std::string` or `std::wstring`, stream extraction takes the whole content of the string, including spaces, rather than relying on the default `operator>>` behavior. If the conversion is unsuccessful, a `bad_lexical_cast` exception is thrown.
|
||||
|
||||
The requirements on the argument and result types are:
|
||||
|
||||
* Source is OutputStreamable, meaning that an `operator<<` is defined that takes a `std::ostream` or `std::wostream` object on the left hand side and an instance of the argument type on the right.
|
||||
* Target is InputStreamable, meaning that an `operator>>` is defined that takes a `std::istream` or `std::wistream` object on the left hand side and an instance of the result type on the right.
|
||||
* Target is CopyConstructible [20.1.3].
|
||||
* Target is DefaultConstructible, meaning that it is possible to default-initialize an object of that type [8.5, 20.1.4].
|
||||
|
||||
The character type of the underlying stream is assumed to be char unless either the Source or the Target requires wide-character streaming, in which case the underlying stream uses `wchar_t`. Source types that require wide-character streaming are `wchar_t`, `wchar_t *`, and `std::wstring`. Target types that require wide-character streaming are `wchar_t` and `std::wstring`.
|
||||
|
||||
Where a higher degree of control is required over conversions, `std::stringstream` and `std::wstringstream` offer a more appropriate path. Where non-stream-based conversions are required, `lexical_cast` is the wrong tool for the job and is not special-cased for such scenarios.
|
||||
[endsect]
|
||||
|
||||
[section bad_lexical_cast]
|
||||
``
|
||||
class bad_lexical_cast : public std::bad_cast
|
||||
{
|
||||
public:
|
||||
... // same member function interface as std::exception
|
||||
};
|
||||
``
|
||||
Exception used to indicate runtime lexical_cast failure.
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Frequently Asked Questions]
|
||||
|
||||
* [*Question:] Why does `lexical_cast<int8_t>("127")` throw `bad_lexical_cast`?
|
||||
* [*Answer:] The type `int8_t` is a `typedef` to `char` or `signed char`. 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.
|
||||
Please use other integer types such as `int` or `short int`. If bounds checking is important, you can also
|
||||
call __numericcast__:
|
||||
`numeric_cast<int8_t>(lexical_cast<int>("127"));`
|
||||
|
||||
[pre
|
||||
]
|
||||
|
||||
* [*Question:] Why does `lexical_cast<unsigned char>("127")` throw `bad_lexical_cast`?
|
||||
* [*Answer:] Lexical conversion to any char type is simply reading a byte from source. But since the source has more than one byte, the exception is thrown.
|
||||
Please use other integer types such as `int` or `short int`. If bounds checking is important, you can also
|
||||
call __numericcast__:
|
||||
`numeric_cast<unsigned char>(lexical_cast<int>("127"));`
|
||||
|
||||
[pre
|
||||
]
|
||||
|
||||
* [*Question:] What does `lexical_cast<std::string>` of an `int8_t` or `uint8_t` not do what I expect?
|
||||
* [*Answer:] As above, note that int8_t and uint8_t are actually chars and are formatted as such. To avoid
|
||||
this, cast to an integer type first: `lexical_cast<std::string>(static_cast<int>(n));`
|
||||
|
||||
[pre
|
||||
]
|
||||
|
||||
* [*Question:] The implementation always resets the `ios_base::skipws` flag of an underlying stream object.
|
||||
It breaks my `operator>>` that works only in presence of this flag. Can you remove code that resets the flag?
|
||||
* [*Answer:] May be in a future version. There is no requirement in
|
||||
__proposallong__ to reset the flag but
|
||||
remember that __proposalshort__ is not yet accepted by the committee. By the way, it's a great opportunity to
|
||||
make your `operator>>` conform to the standard.
|
||||
Read a good C++ book, study `std::sentry` and [@boost:libs/io/doc/ios_state.html `ios_state_saver`].
|
||||
|
||||
[pre
|
||||
]
|
||||
|
||||
* [*Question:] Why `std::cout << boost::lexical_cast<unsigned int>("-1");` does not throw, but outputs 4294967295?
|
||||
* [*Answer:] `boost::lexical_cast` has the behavior of `std::stringstream`, which uses `num_get` functions of
|
||||
`std::locale` to convert numbers. If we look at the Programming languages — C++, we'll see, that `num_get` uses
|
||||
the rules of `scanf` for conversions. And in the C99 standard for unsigned input value minus sign is optional, so
|
||||
if a negative number is read, no errors will arise and the result will be the two's complement.
|
||||
|
||||
[pre
|
||||
]
|
||||
|
||||
* [*Question:] Why `boost::lexical_cast<int>(L'A');` outputs 65 and `boost::lexical_cast<wchar_t>(L"65");` does not throw?
|
||||
* [*Answer:] If you are using an old version of Visual Studio or compile code with /Zc:wchar_t- flag,
|
||||
`boost::lexical_cast` sees single `wchar_t` character as `unsigned short`. It is not a `boost::lexical_cast` mistake, but a
|
||||
limitation of compiler options that you use.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Changes]
|
||||
* [*boost 1.49.0 :]
|
||||
|
||||
* Restored work with typedefed wchar_t (compilation flag /Zc:wchar_t- for Visual Studio).
|
||||
* Better performance and less memory usage for `boost::container::basic_string` conversions.
|
||||
|
||||
* [*boost 1.48.0 :]
|
||||
|
||||
* Added code to work with Inf and NaN on any platform.
|
||||
* Better performance and less memory usage for conversions to float type (and to double type, if `sizeof(double) < sizeof(long double)`).
|
||||
|
||||
* [*boost 1.47.0 :]
|
||||
|
||||
* Optimizations for "C" and other locales without number grouping.
|
||||
* Better performance and less memory usage for unsigned char and signed char conversions.
|
||||
* Better performance and less memory usage for conversions to arithmetic types.
|
||||
* Better performance and less memory usage for conversions from arithmetic type to arithmetic type.
|
||||
* Directly construct Target from Source on some conversions (like conversions from string to string, from char array to string, from char to char and others).
|
||||
|
||||
* [*boost 1.34.0 :]
|
||||
|
||||
* Better performance for many combinations of Source and Target types. For more details refer to Alexander Nasonovs article [@http://accu.org/index.php/journals/1375 Fine Tuning for lexical_cast, Overload #74, August 2006] [@http://www.accu.org/var/uploads/journals/overload74.pdf (PDF)].
|
||||
|
||||
* [*boost 1.33.0 :]
|
||||
|
||||
* 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.
|
||||
* The MSVC 6 support is deprecated, and will be removed in a future Boost version.
|
||||
|
||||
* [*Earlier :]
|
||||
|
||||
* The previous version of lexical_cast used the default stream precision for reading and writing floating-point numbers. For numerics that have a corresponding specialization of `std::numeric_limits`, the current version now chooses a precision to match.
|
||||
* The previous version of lexical_cast did not support conversion to or from any wide-character-based types. For compilers with full language and library support for wide characters, `lexical_cast` now supports conversions from `wchar_t`, `wchar_t *`, and `std::wstring` and to `wchar_t` and `std::wstring`.
|
||||
* The previous version of `lexical_cast` 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 `std::string` and, where supported, `std::wstring`: `lexical_cast<std::string>("Hello, World")` succeeds instead of failing with a `bad_lexical_cast` exception.
|
||||
* The previous version of `lexical_cast` allowed unsafe and meaningless conversions to pointers. The current version now throws a `bad_lexical_cast` for conversions to pointers: `lexical_cast<char *>("Goodbye, World")` now throws an exception instead of causing undefined behavior.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Performance]
|
||||
|
||||
In most cases `boost::lexical_cast` is faster than `scanf`, `printf`, `std::stringstream`. For more detailed info you can look at the tables below.
|
||||
|
||||
[section Tests description]
|
||||
All the tests measure execution speed in milliseconds for 10000 iterations of the following code blocks:
|
||||
[table:legend Tests source code
|
||||
[[Test name] [Code]]
|
||||
[[lexical_cast]
|
||||
[``
|
||||
_out = boost::lexical_cast<OUTTYPE>(_in);
|
||||
``]
|
||||
]
|
||||
[[std::stringstream with construction]
|
||||
[``
|
||||
std::stringstream ss;
|
||||
ss << _in;
|
||||
if (ss.fail()) throw std::logic_error(descr);
|
||||
ss >> _out;
|
||||
if (ss.fail()) throw std::logic_error(descr);
|
||||
``]
|
||||
]
|
||||
[[std::stringstream without construction]
|
||||
[``
|
||||
ss << _in; // ss is an instance of std::stringstream
|
||||
if (ss.fail()) throw std::logic_error(descr);
|
||||
ss >> _out;
|
||||
if (ss.fail()) throw std::logic_error(descr);
|
||||
/* reseting std::stringstream to use it again */
|
||||
ss.str(std::string());
|
||||
ss.clear();
|
||||
``]
|
||||
]
|
||||
[[scanf/printf]
|
||||
[``
|
||||
typename OUTTYPE::value_type buffer[500];
|
||||
sprintf( (char*)buffer, conv, _in);
|
||||
_out = buffer;
|
||||
``]
|
||||
]
|
||||
]
|
||||
Fastest results are highlitened with "!!! *x* !!!".
|
||||
Do not use this results to compare compilers, because tests were taken on different hardware.
|
||||
|
||||
[endsect]
|
||||
|
||||
[/ BEGIN of section, generated by performance measuring program ]
|
||||
|
||||
[section clang-linux-2.8][table:id Performance Table (clang-linux-2.8)
|
||||
[[From->To] [lexical_cast] [std::stringstream with construction] [std::stringstream without construction][scanf/printf]]
|
||||
[[ string->char ][ !!! *<1* !!! ][ 148 ][ 14 ][ 12 ]]
|
||||
[[ string->signed char ][ !!! *<1* !!! ][ 97 ][ 8 ][ 7 ]]
|
||||
[[ string->unsigned char ][ !!! *<1* !!! ][ 90 ][ 8 ][ 13 ]]
|
||||
[[ string->int ][ !!! *4* !!! ][ 102 ][ 19 ][ 15 ]]
|
||||
[[ string->short ][ !!! *4* !!! ][ 105 ][ 20 ][ 15 ]]
|
||||
[[ string->long int ][ !!! *4* !!! ][ 105 ][ 19 ][ 15 ]]
|
||||
[[ string->long long ][ !!! *4* !!! ][ 115 ][ 19 ][ 14 ]]
|
||||
[[ string->unsigned int ][ !!! *4* !!! ][ 102 ][ 18 ][ 14 ]]
|
||||
[[ string->unsigned short ][ !!! *4* !!! ][ 101 ][ 19 ][ 15 ]]
|
||||
[[ string->unsigned long int ][ !!! *3* !!! ][ 107 ][ 20 ][ 14 ]]
|
||||
[[ string->unsigned long long ][ !!! *3* !!! ][ 103 ][ 20 ][ 14 ]]
|
||||
[[ string->bool ][ !!! *<1* !!! ][ 97 ][ 16 ][ 8 ]]
|
||||
[[ string->float ][ !!! *21* !!! ][ 170 ][ 61 ][ 32 ]]
|
||||
[[ string->double ][ !!! *18* !!! ][ 206 ][ 93 ][ 58 ]]
|
||||
[[ string->long double ][ 135 ][ 221 ][ 94 ][ !!! *57* !!! ]]
|
||||
[[ char->string ][ !!! *7* !!! ][ 100 ][ 17 ][ 13 ]]
|
||||
[[ unsigned char->string ][ !!! *7* !!! ][ 99 ][ 18 ][ 16 ]]
|
||||
[[ signed char->string ][ !!! *7* !!! ][ 101 ][ 17 ][ 12 ]]
|
||||
[[ int->string ][ !!! *13* !!! ][ 110 ][ 23 ][ 15 ]]
|
||||
[[ short->string ][ !!! *13* !!! ][ 112 ][ 24 ][ 18 ]]
|
||||
[[ long int->string ][ !!! *13* !!! ][ 119 ][ 23 ][ 17 ]]
|
||||
[[ long long->string ][ !!! *13* !!! ][ 110 ][ 23 ][ 18 ]]
|
||||
[[ unsigned int->string ][ !!! *14* !!! ][ 113 ][ 24 ][ 17 ]]
|
||||
[[ unsigned short->string ][ !!! *13* !!! ][ 108 ][ 24 ][ 17 ]]
|
||||
[[ unsigned long int->string ][ !!! *13* !!! ][ 109 ][ 24 ][ 16 ]]
|
||||
[[ unsigned long long->string ][ !!! *13* !!! ][ 110 ][ 23 ][ 17 ]]
|
||||
[[ bool->string ][ !!! *7* !!! ][ 105 ][ 24 ][ 12 ]]
|
||||
[[ float->string ][ 70 ][ 192 ][ 94 ][ !!! *49* !!! ]]
|
||||
[[ double->string ][ 106 ][ 217 ][ 122 ][ !!! *76* !!! ]]
|
||||
[[ long double->string ][ 120 ][ 219 ][ 123 ][ !!! *80* !!! ]]
|
||||
[[ char*->char ][ !!! *2* !!! ][ 90 ][ 9 ][ 8 ]]
|
||||
[[ char*->signed char ][ !!! *2* !!! ][ 87 ][ 10 ][ 7 ]]
|
||||
[[ char*->unsigned char ][ !!! *3* !!! ][ 90 ][ 10 ][ 13 ]]
|
||||
[[ char*->int ][ !!! *6* !!! ][ 107 ][ 21 ][ 15 ]]
|
||||
[[ char*->short ][ !!! *6* !!! ][ 110 ][ 19 ][ 14 ]]
|
||||
[[ char*->long int ][ !!! *6* !!! ][ 103 ][ 19 ][ 14 ]]
|
||||
[[ char*->long long ][ !!! *7* !!! ][ 104 ][ 20 ][ 15 ]]
|
||||
[[ char*->unsigned int ][ !!! *6* !!! ][ 101 ][ 20 ][ 15 ]]
|
||||
[[ char*->unsigned short ][ !!! *7* !!! ][ 100 ][ 20 ][ 14 ]]
|
||||
[[ char*->unsigned long int ][ !!! *6* !!! ][ 105 ][ 22 ][ 15 ]]
|
||||
[[ char*->unsigned long long ][ !!! *7* !!! ][ 106 ][ 21 ][ 14 ]]
|
||||
[[ char*->bool ][ !!! *2* !!! ][ 99 ][ 18 ][ 7 ]]
|
||||
[[ char*->float ][ !!! *22* !!! ][ 159 ][ 67 ][ 33 ]]
|
||||
[[ char*->double ][ !!! *20* !!! ][ 205 ][ 94 ][ 58 ]]
|
||||
[[ char*->long double ][ 140 ][ 214 ][ 95 ][ !!! *58* !!! ]]
|
||||
[[ unsigned char*->char ][ !!! *2* !!! ][ 92 ][ 9 ][ 7 ]]
|
||||
[[ unsigned char*->signed char ][ !!! *2* !!! ][ 89 ][ 10 ][ 7 ]]
|
||||
[[ unsigned char*->unsigned char ][ !!! *2* !!! ][ 89 ][ 10 ][ 14 ]]
|
||||
[[ unsigned char*->int ][ !!! *6* !!! ][ 104 ][ 20 ][ 14 ]]
|
||||
[[ unsigned char*->short ][ !!! *6* !!! ][ 106 ][ 21 ][ 14 ]]
|
||||
[[ unsigned char*->long int ][ !!! *6* !!! ][ 105 ][ 19 ][ 14 ]]
|
||||
[[ unsigned char*->long long ][ !!! *6* !!! ][ 106 ][ 20 ][ 15 ]]
|
||||
[[ unsigned char*->unsigned int ][ !!! *7* !!! ][ 105 ][ 19 ][ 14 ]]
|
||||
[[ unsigned char*->unsigned short ][ !!! *6* !!! ][ 103 ][ 19 ][ 14 ]]
|
||||
[[ unsigned char*->unsigned long int ][ !!! *6* !!! ][ 106 ][ 19 ][ 14 ]]
|
||||
[[ unsigned char*->unsigned long long ][ !!! *6* !!! ][ 104 ][ 21 ][ 15 ]]
|
||||
[[ unsigned char*->bool ][ !!! *2* !!! ][ 102 ][ 18 ][ 7 ]]
|
||||
[[ unsigned char*->float ][ !!! *23* !!! ][ 160 ][ 66 ][ 32 ]]
|
||||
[[ unsigned char*->double ][ !!! *20* !!! ][ 201 ][ 95 ][ 58 ]]
|
||||
[[ unsigned char*->long double ][ 144 ][ 221 ][ 95 ][ !!! *60* !!! ]]
|
||||
[[ unsigned char*->string ][ !!! *12* !!! ][ 104 ][ 23 ][ --- ]]
|
||||
[[ signed char*->char ][ !!! *2* !!! ][ 90 ][ 9 ][ 7 ]]
|
||||
[[ signed char*->signed char ][ !!! *2* !!! ][ 89 ][ 9 ][ 7 ]]
|
||||
[[ signed char*->unsigned char ][ !!! *2* !!! ][ 89 ][ 10 ][ 13 ]]
|
||||
[[ signed char*->int ][ !!! *6* !!! ][ 106 ][ 19 ][ 15 ]]
|
||||
[[ signed char*->short ][ !!! *6* !!! ][ 107 ][ 20 ][ 15 ]]
|
||||
[[ signed char*->long int ][ !!! *6* !!! ][ 103 ][ 19 ][ 14 ]]
|
||||
[[ signed char*->long long ][ !!! *6* !!! ][ 103 ][ 19 ][ 14 ]]
|
||||
[[ signed char*->unsigned int ][ !!! *6* !!! ][ 101 ][ 19 ][ 15 ]]
|
||||
[[ signed char*->unsigned short ][ !!! *6* !!! ][ 101 ][ 19 ][ 16 ]]
|
||||
[[ signed char*->unsigned long int ][ !!! *6* !!! ][ 105 ][ 22 ][ 15 ]]
|
||||
[[ signed char*->unsigned long long ][ !!! *6* !!! ][ 104 ][ 21 ][ 15 ]]
|
||||
[[ signed char*->bool ][ !!! *2* !!! ][ 100 ][ 18 ][ 7 ]]
|
||||
[[ signed char*->float ][ !!! *23* !!! ][ 161 ][ 62 ][ 32 ]]
|
||||
[[ signed char*->double ][ !!! *20* !!! ][ 207 ][ 102 ][ 57 ]]
|
||||
[[ signed char*->long double ][ 144 ][ 216 ][ 96 ][ !!! *63* !!! ]]
|
||||
[[ signed char*->string ][ !!! *12* !!! ][ 104 ][ 23 ][ --- ]]
|
||||
[[ int->int ][ !!! *<1* !!! ][ 110 ][ 22 ][ --- ]]
|
||||
[[ float->double ][ !!! *<1* !!! ][ 223 ][ 113 ][ --- ]]
|
||||
[[ double->double ][ !!! *<1* !!! ][ 227 ][ 111 ][ --- ]]
|
||||
[[ int->int ][ !!! *<1* !!! ][ 231 ][ 122 ][ --- ]]
|
||||
[[ int->int ][ !!! *<1* !!! ][ 229 ][ 121 ][ --- ]]
|
||||
[[ char->unsigned char ][ !!! *<1* !!! ][ 90 ][ 8 ][ --- ]]
|
||||
[[ char->signed char ][ !!! *<1* !!! ][ 88 ][ 8 ][ --- ]]
|
||||
[[ unsigned char->char ][ !!! *<1* !!! ][ 89 ][ 8 ][ --- ]]
|
||||
[[ signed char->char ][ !!! *<1* !!! ][ 91 ][ 9 ][ --- ]]
|
||||
]
|
||||
[endsect]
|
||||
[section gcc-4.4][table:id Performance Table (gcc-4.4)
|
||||
[[From->To] [lexical_cast] [std::stringstream with construction] [std::stringstream without construction][scanf/printf]]
|
||||
[[ string->char ][ !!! *<1* !!! ][ 90 ][ 7 ][ 7 ]]
|
||||
[[ string->signed char ][ !!! *<1* !!! ][ 88 ][ 7 ][ 8 ]]
|
||||
[[ string->unsigned char ][ !!! *<1* !!! ][ 88 ][ 8 ][ 14 ]]
|
||||
[[ string->int ][ !!! *3* !!! ][ 103 ][ 18 ][ 15 ]]
|
||||
[[ string->short ][ !!! *3* !!! ][ 105 ][ 20 ][ 15 ]]
|
||||
[[ string->long int ][ !!! *3* !!! ][ 101 ][ 18 ][ 16 ]]
|
||||
[[ string->long long ][ !!! *3* !!! ][ 101 ][ 18 ][ 15 ]]
|
||||
[[ string->unsigned int ][ !!! *3* !!! ][ 98 ][ 23 ][ 14 ]]
|
||||
[[ string->unsigned short ][ !!! *3* !!! ][ 100 ][ 17 ][ 14 ]]
|
||||
[[ string->unsigned long int ][ !!! *3* !!! ][ 100 ][ 21 ][ 15 ]]
|
||||
[[ string->unsigned long long ][ !!! *3* !!! ][ 99 ][ 19 ][ 15 ]]
|
||||
[[ string->bool ][ !!! *<1* !!! ][ 95 ][ 16 ][ 8 ]]
|
||||
[[ string->float ][ !!! *13* !!! ][ 160 ][ 61 ][ 33 ]]
|
||||
[[ string->double ][ !!! *14* !!! ][ 206 ][ 93 ][ 59 ]]
|
||||
[[ string->long double ][ 128 ][ 217 ][ 96 ][ !!! *61* !!! ]]
|
||||
[[ char->string ][ !!! *7* !!! ][ 100 ][ 17 ][ 12 ]]
|
||||
[[ unsigned char->string ][ !!! *7* !!! ][ 109 ][ 17 ][ 16 ]]
|
||||
[[ signed char->string ][ !!! *7* !!! ][ 99 ][ 17 ][ 12 ]]
|
||||
[[ int->string ][ !!! *13* !!! ][ 110 ][ 21 ][ 15 ]]
|
||||
[[ short->string ][ !!! *14* !!! ][ 110 ][ 22 ][ 17 ]]
|
||||
[[ long int->string ][ !!! *14* !!! ][ 109 ][ 21 ][ 16 ]]
|
||||
[[ long long->string ][ !!! *13* !!! ][ 114 ][ 20 ][ 17 ]]
|
||||
[[ unsigned int->string ][ !!! *13* !!! ][ 109 ][ 23 ][ 15 ]]
|
||||
[[ unsigned short->string ][ !!! *14* !!! ][ 109 ][ 23 ][ 17 ]]
|
||||
[[ unsigned long int->string ][ !!! *13* !!! ][ 112 ][ 23 ][ 16 ]]
|
||||
[[ unsigned long long->string ][ !!! *14* !!! ][ 109 ][ 21 ][ 17 ]]
|
||||
[[ bool->string ][ !!! *7* !!! ][ 108 ][ 23 ][ 11 ]]
|
||||
[[ float->string ][ 63 ][ 185 ][ 92 ][ !!! *50* !!! ]]
|
||||
[[ double->string ][ 106 ][ 216 ][ 116 ][ !!! *75* !!! ]]
|
||||
[[ long double->string ][ 118 ][ 219 ][ 119 ][ !!! *80* !!! ]]
|
||||
[[ char*->char ][ !!! *1* !!! ][ 93 ][ 9 ][ 9 ]]
|
||||
[[ char*->signed char ][ !!! *1* !!! ][ 92 ][ 9 ][ 9 ]]
|
||||
[[ char*->unsigned char ][ !!! *1* !!! ][ 92 ][ 9 ][ 14 ]]
|
||||
[[ char*->int ][ !!! *4* !!! ][ 107 ][ 19 ][ 15 ]]
|
||||
[[ char*->short ][ !!! *5* !!! ][ 109 ][ 19 ][ 15 ]]
|
||||
[[ char*->long int ][ !!! *4* !!! ][ 113 ][ 19 ][ 15 ]]
|
||||
[[ char*->long long ][ !!! *4* !!! ][ 108 ][ 20 ][ 15 ]]
|
||||
[[ char*->unsigned int ][ !!! *4* !!! ][ 106 ][ 19 ][ 15 ]]
|
||||
[[ char*->unsigned short ][ !!! *4* !!! ][ 106 ][ 18 ][ 15 ]]
|
||||
[[ char*->unsigned long int ][ !!! *4* !!! ][ 103 ][ 22 ][ 15 ]]
|
||||
[[ char*->unsigned long long ][ !!! *4* !!! ][ 105 ][ 20 ][ 15 ]]
|
||||
[[ char*->bool ][ !!! *1* !!! ][ 104 ][ 18 ][ 8 ]]
|
||||
[[ char*->float ][ !!! *15* !!! ][ 164 ][ 62 ][ 33 ]]
|
||||
[[ char*->double ][ !!! *16* !!! ][ 203 ][ 97 ][ 58 ]]
|
||||
[[ char*->long double ][ 132 ][ 223 ][ 98 ][ !!! *60* !!! ]]
|
||||
[[ unsigned char*->char ][ !!! *2* !!! ][ 90 ][ 9 ][ 8 ]]
|
||||
[[ unsigned char*->signed char ][ !!! *2* !!! ][ 92 ][ 10 ][ 8 ]]
|
||||
[[ unsigned char*->unsigned char ][ !!! *2* !!! ][ 91 ][ 9 ][ 14 ]]
|
||||
[[ unsigned char*->int ][ !!! *6* !!! ][ 106 ][ 20 ][ 15 ]]
|
||||
[[ unsigned char*->short ][ !!! *6* !!! ][ 106 ][ 21 ][ 15 ]]
|
||||
[[ unsigned char*->long int ][ !!! *6* !!! ][ 111 ][ 19 ][ 15 ]]
|
||||
[[ unsigned char*->long long ][ !!! *6* !!! ][ 107 ][ 20 ][ 15 ]]
|
||||
[[ unsigned char*->unsigned int ][ !!! *6* !!! ][ 105 ][ 19 ][ 15 ]]
|
||||
[[ unsigned char*->unsigned short ][ !!! *6* !!! ][ 103 ][ 18 ][ 15 ]]
|
||||
[[ unsigned char*->unsigned long int ][ !!! *6* !!! ][ 106 ][ 22 ][ 14 ]]
|
||||
[[ unsigned char*->unsigned long long ][ !!! *6* !!! ][ 105 ][ 20 ][ 14 ]]
|
||||
[[ unsigned char*->bool ][ !!! *2* !!! ][ 106 ][ 18 ][ 8 ]]
|
||||
[[ unsigned char*->float ][ !!! *15* !!! ][ 167 ][ 68 ][ 33 ]]
|
||||
[[ unsigned char*->double ][ !!! *17* !!! ][ 203 ][ 99 ][ 58 ]]
|
||||
[[ unsigned char*->long double ][ 129 ][ 216 ][ 97 ][ !!! *61* !!! ]]
|
||||
[[ unsigned char*->string ][ !!! *13* !!! ][ 111 ][ 23 ][ --- ]]
|
||||
[[ signed char*->char ][ !!! *2* !!! ][ 92 ][ 9 ][ 8 ]]
|
||||
[[ signed char*->signed char ][ !!! *2* !!! ][ 91 ][ 9 ][ 8 ]]
|
||||
[[ signed char*->unsigned char ][ !!! *2* !!! ][ 91 ][ 9 ][ 14 ]]
|
||||
[[ signed char*->int ][ !!! *6* !!! ][ 107 ][ 19 ][ 15 ]]
|
||||
[[ signed char*->short ][ !!! *6* !!! ][ 109 ][ 24 ][ 14 ]]
|
||||
[[ signed char*->long int ][ !!! *6* !!! ][ 112 ][ 19 ][ 15 ]]
|
||||
[[ signed char*->long long ][ !!! *5* !!! ][ 107 ][ 20 ][ 15 ]]
|
||||
[[ signed char*->unsigned int ][ !!! *6* !!! ][ 108 ][ 20 ][ 15 ]]
|
||||
[[ signed char*->unsigned short ][ !!! *6* !!! ][ 104 ][ 18 ][ 15 ]]
|
||||
[[ signed char*->unsigned long int ][ !!! *6* !!! ][ 102 ][ 22 ][ 15 ]]
|
||||
[[ signed char*->unsigned long long ][ !!! *6* !!! ][ 104 ][ 20 ][ 15 ]]
|
||||
[[ signed char*->bool ][ !!! *2* !!! ][ 104 ][ 18 ][ 8 ]]
|
||||
[[ signed char*->float ][ !!! *16* !!! ][ 165 ][ 63 ][ 33 ]]
|
||||
[[ signed char*->double ][ !!! *16* !!! ][ 203 ][ 98 ][ 59 ]]
|
||||
[[ signed char*->long double ][ 129 ][ 215 ][ 98 ][ !!! *61* !!! ]]
|
||||
[[ signed char*->string ][ !!! *13* !!! ][ 109 ][ 21 ][ --- ]]
|
||||
[[ int->int ][ !!! *<1* !!! ][ 109 ][ 21 ][ --- ]]
|
||||
[[ float->double ][ !!! *<1* !!! ][ 221 ][ 102 ][ --- ]]
|
||||
[[ double->double ][ !!! *<1* !!! ][ 223 ][ 103 ][ --- ]]
|
||||
[[ int->int ][ !!! *<1* !!! ][ 231 ][ 115 ][ --- ]]
|
||||
[[ int->int ][ !!! *<1* !!! ][ 231 ][ 115 ][ --- ]]
|
||||
[[ char->unsigned char ][ !!! *<1* !!! ][ 92 ][ 8 ][ --- ]]
|
||||
[[ char->signed char ][ !!! *<1* !!! ][ 88 ][ 8 ][ --- ]]
|
||||
[[ unsigned char->char ][ !!! *<1* !!! ][ 88 ][ 7 ][ --- ]]
|
||||
[[ signed char->char ][ !!! *<1* !!! ][ 89 ][ 8 ][ --- ]]
|
||||
]
|
||||
[endsect]
|
||||
[section gcc-4.5][table:id Performance Table (gcc-4.5)
|
||||
[[From->To] [lexical_cast] [std::stringstream with construction] [std::stringstream without construction][scanf/printf]]
|
||||
[[ string->char ][ !!! *<1* !!! ][ 91 ][ 8 ][ 7 ]]
|
||||
[[ string->signed char ][ !!! *<1* !!! ][ 91 ][ 8 ][ 7 ]]
|
||||
[[ string->unsigned char ][ !!! *<1* !!! ][ 90 ][ 8 ][ 13 ]]
|
||||
[[ string->int ][ !!! *3* !!! ][ 100 ][ 20 ][ 14 ]]
|
||||
[[ string->short ][ !!! *3* !!! ][ 106 ][ 20 ][ 14 ]]
|
||||
[[ string->long int ][ !!! *3* !!! ][ 100 ][ 18 ][ 14 ]]
|
||||
[[ string->long long ][ !!! *9* !!! ][ 100 ][ 18 ][ 15 ]]
|
||||
[[ string->unsigned int ][ !!! *3* !!! ][ 97 ][ 20 ][ 14 ]]
|
||||
[[ string->unsigned short ][ !!! *3* !!! ][ 102 ][ 17 ][ 14 ]]
|
||||
[[ string->unsigned long int ][ !!! *3* !!! ][ 97 ][ 21 ][ 14 ]]
|
||||
[[ string->unsigned long long ][ !!! *3* !!! ][ 97 ][ 19 ][ 14 ]]
|
||||
[[ string->bool ][ !!! *<1* !!! ][ 95 ][ 16 ][ 7 ]]
|
||||
[[ string->float ][ !!! *15* !!! ][ 157 ][ 63 ][ 32 ]]
|
||||
[[ string->double ][ !!! *17* !!! ][ 203 ][ 95 ][ 59 ]]
|
||||
[[ string->long double ][ 129 ][ 216 ][ 93 ][ !!! *58* !!! ]]
|
||||
[[ char->string ][ !!! *8* !!! ][ 100 ][ 17 ][ 10 ]]
|
||||
[[ unsigned char->string ][ !!! *8* !!! ][ 96 ][ 18 ][ 16 ]]
|
||||
[[ signed char->string ][ !!! *8* !!! ][ 96 ][ 18 ][ 10 ]]
|
||||
[[ int->string ][ !!! *14* !!! ][ 105 ][ 22 ][ 15 ]]
|
||||
[[ short->string ][ !!! *14* !!! ][ 107 ][ 23 ][ 17 ]]
|
||||
[[ long int->string ][ !!! *14* !!! ][ 109 ][ 22 ][ 17 ]]
|
||||
[[ long long->string ][ !!! *14* !!! ][ 105 ][ 22 ][ 18 ]]
|
||||
[[ unsigned int->string ][ !!! *14* !!! ][ 105 ][ 25 ][ 15 ]]
|
||||
[[ unsigned short->string ][ !!! *15* !!! ][ 105 ][ 23 ][ 17 ]]
|
||||
[[ unsigned long int->string ][ !!! *14* !!! ][ 109 ][ 24 ][ 17 ]]
|
||||
[[ unsigned long long->string ][ !!! *14* !!! ][ 102 ][ 23 ][ 17 ]]
|
||||
[[ bool->string ][ !!! *8* !!! ][ 104 ][ 23 ][ 12 ]]
|
||||
[[ float->string ][ 66 ][ 181 ][ 92 ][ !!! *49* !!! ]]
|
||||
[[ double->string ][ 107 ][ 215 ][ 120 ][ !!! *75* !!! ]]
|
||||
[[ long double->string ][ 117 ][ 221 ][ 125 ][ !!! *79* !!! ]]
|
||||
[[ char*->char ][ !!! *1* !!! ][ 89 ][ 9 ][ 7 ]]
|
||||
[[ char*->signed char ][ !!! *1* !!! ][ 90 ][ 9 ][ 7 ]]
|
||||
[[ char*->unsigned char ][ !!! *2* !!! ][ 90 ][ 9 ][ 13 ]]
|
||||
[[ char*->int ][ !!! *7* !!! ][ 103 ][ 20 ][ 15 ]]
|
||||
[[ char*->short ][ !!! *6* !!! ][ 102 ][ 29 ][ 14 ]]
|
||||
[[ char*->long int ][ !!! *7* !!! ][ 101 ][ 20 ][ 15 ]]
|
||||
[[ char*->long long ][ !!! *6* !!! ][ 102 ][ 20 ][ 14 ]]
|
||||
[[ char*->unsigned int ][ !!! *6* !!! ][ 99 ][ 19 ][ 14 ]]
|
||||
[[ char*->unsigned short ][ !!! *6* !!! ][ 101 ][ 18 ][ 14 ]]
|
||||
[[ char*->unsigned long int ][ !!! *6* !!! ][ 102 ][ 22 ][ 14 ]]
|
||||
[[ char*->unsigned long long ][ !!! *6* !!! ][ 101 ][ 21 ][ 14 ]]
|
||||
[[ char*->bool ][ !!! *3* !!! ][ 98 ][ 18 ][ 7 ]]
|
||||
[[ char*->float ][ !!! *18* !!! ][ 162 ][ 63 ][ 31 ]]
|
||||
[[ char*->double ][ !!! *17* !!! ][ 203 ][ 96 ][ 58 ]]
|
||||
[[ char*->long double ][ 135 ][ 214 ][ 98 ][ !!! *58* !!! ]]
|
||||
[[ unsigned char*->char ][ !!! *2* !!! ][ 87 ][ 9 ][ 7 ]]
|
||||
[[ unsigned char*->signed char ][ !!! *2* !!! ][ 87 ][ 9 ][ 7 ]]
|
||||
[[ unsigned char*->unsigned char ][ !!! *3* !!! ][ 87 ][ 9 ][ 13 ]]
|
||||
[[ unsigned char*->int ][ !!! *6* !!! ][ 105 ][ 20 ][ 14 ]]
|
||||
[[ unsigned char*->short ][ !!! *6* !!! ][ 102 ][ 21 ][ 14 ]]
|
||||
[[ unsigned char*->long int ][ !!! *6* !!! ][ 101 ][ 20 ][ 14 ]]
|
||||
[[ unsigned char*->long long ][ !!! *6* !!! ][ 102 ][ 20 ][ 14 ]]
|
||||
[[ unsigned char*->unsigned int ][ !!! *6* !!! ][ 99 ][ 19 ][ 14 ]]
|
||||
[[ unsigned char*->unsigned short ][ !!! *6* !!! ][ 100 ][ 18 ][ 14 ]]
|
||||
[[ unsigned char*->unsigned long int ][ !!! *6* !!! ][ 101 ][ 24 ][ 14 ]]
|
||||
[[ unsigned char*->unsigned long long ][ !!! *6* !!! ][ 100 ][ 20 ][ 14 ]]
|
||||
[[ unsigned char*->bool ][ !!! *3* !!! ][ 99 ][ 18 ][ 8 ]]
|
||||
[[ unsigned char*->float ][ !!! *17* !!! ][ 164 ][ 64 ][ 32 ]]
|
||||
[[ unsigned char*->double ][ !!! *18* !!! ][ 201 ][ 94 ][ 58 ]]
|
||||
[[ unsigned char*->long double ][ 133 ][ 217 ][ 95 ][ !!! *60* !!! ]]
|
||||
[[ unsigned char*->string ][ !!! *14* !!! ][ 103 ][ 23 ][ --- ]]
|
||||
[[ signed char*->char ][ !!! *3* !!! ][ 88 ][ 10 ][ 8 ]]
|
||||
[[ signed char*->signed char ][ !!! *2* !!! ][ 87 ][ 10 ][ 7 ]]
|
||||
[[ signed char*->unsigned char ][ !!! *3* !!! ][ 87 ][ 9 ][ 13 ]]
|
||||
[[ signed char*->int ][ !!! *6* !!! ][ 104 ][ 20 ][ 14 ]]
|
||||
[[ signed char*->short ][ !!! *6* !!! ][ 105 ][ 21 ][ 14 ]]
|
||||
[[ signed char*->long int ][ !!! *6* !!! ][ 104 ][ 20 ][ 15 ]]
|
||||
[[ signed char*->long long ][ !!! *6* !!! ][ 106 ][ 20 ][ 14 ]]
|
||||
[[ signed char*->unsigned int ][ !!! *6* !!! ][ 99 ][ 20 ][ 14 ]]
|
||||
[[ signed char*->unsigned short ][ !!! *6* !!! ][ 100 ][ 18 ][ 14 ]]
|
||||
[[ signed char*->unsigned long int ][ !!! *6* !!! ][ 102 ][ 23 ][ 14 ]]
|
||||
[[ signed char*->unsigned long long ][ !!! *6* !!! ][ 103 ][ 20 ][ 14 ]]
|
||||
[[ signed char*->bool ][ !!! *3* !!! ][ 99 ][ 18 ][ 7 ]]
|
||||
[[ signed char*->float ][ !!! *18* !!! ][ 159 ][ 60 ][ 32 ]]
|
||||
[[ signed char*->double ][ !!! *18* !!! ][ 203 ][ 95 ][ 57 ]]
|
||||
[[ signed char*->long double ][ 129 ][ 213 ][ 97 ][ !!! *56* !!! ]]
|
||||
[[ signed char*->string ][ !!! *14* !!! ][ 105 ][ 22 ][ --- ]]
|
||||
[[ int->int ][ !!! *<1* !!! ][ 109 ][ 22 ][ --- ]]
|
||||
[[ float->double ][ !!! *<1* !!! ][ 226 ][ 104 ][ --- ]]
|
||||
[[ double->double ][ !!! *<1* !!! ][ 229 ][ 103 ][ --- ]]
|
||||
[[ int->int ][ !!! *<1* !!! ][ 225 ][ 115 ][ --- ]]
|
||||
[[ int->int ][ !!! *<1* !!! ][ 227 ][ 115 ][ --- ]]
|
||||
[[ char->unsigned char ][ !!! *<1* !!! ][ 90 ][ 8 ][ --- ]]
|
||||
[[ char->signed char ][ !!! *<1* !!! ][ 84 ][ 8 ][ --- ]]
|
||||
[[ unsigned char->char ][ !!! *<1* !!! ][ 88 ][ 8 ][ --- ]]
|
||||
[[ signed char->char ][ !!! *<1* !!! ][ 89 ][ 8 ][ --- ]]
|
||||
]
|
||||
[endsect]
|
||||
[section gcc-4.6][table:id Performance Table (gcc-4.6)
|
||||
[[From->To] [lexical_cast] [std::stringstream with construction] [std::stringstream without construction][scanf/printf]]
|
||||
[[ string->char ][ !!! *<1* !!! ][ 94 ][ 8 ][ 7 ]]
|
||||
[[ string->signed char ][ !!! *<1* !!! ][ 96 ][ 9 ][ 7 ]]
|
||||
[[ string->unsigned char ][ !!! *<1* !!! ][ 96 ][ 8 ][ 13 ]]
|
||||
[[ string->int ][ !!! *3* !!! ][ 110 ][ 18 ][ 16 ]]
|
||||
[[ string->short ][ !!! *3* !!! ][ 111 ][ 18 ][ 16 ]]
|
||||
[[ string->long int ][ !!! *3* !!! ][ 109 ][ 18 ][ 15 ]]
|
||||
[[ string->long long ][ !!! *3* !!! ][ 111 ][ 18 ][ 15 ]]
|
||||
[[ string->unsigned int ][ !!! *3* !!! ][ 110 ][ 20 ][ 15 ]]
|
||||
[[ string->unsigned short ][ !!! *3* !!! ][ 111 ][ 18 ][ 15 ]]
|
||||
[[ string->unsigned long int ][ !!! *3* !!! ][ 109 ][ 18 ][ 15 ]]
|
||||
[[ string->unsigned long long ][ !!! *3* !!! ][ 114 ][ 19 ][ 15 ]]
|
||||
[[ string->bool ][ !!! *<1* !!! ][ 106 ][ 17 ][ 8 ]]
|
||||
[[ string->float ][ !!! *13* !!! ][ 175 ][ 70 ][ 33 ]]
|
||||
[[ string->double ][ !!! *14* !!! ][ 182 ][ 81 ][ 58 ]]
|
||||
[[ string->long double ][ 118 ][ 190 ][ 87 ][ !!! *58* !!! ]]
|
||||
[[ char->string ][ !!! *8* !!! ][ 118 ][ 21 ][ 12 ]]
|
||||
[[ unsigned char->string ][ !!! *8* !!! ][ 109 ][ 18 ][ 16 ]]
|
||||
[[ signed char->string ][ !!! *8* !!! ][ 108 ][ 18 ][ 12 ]]
|
||||
[[ int->string ][ 20 ][ 121 ][ 21 ][ !!! *16* !!! ]]
|
||||
[[ short->string ][ !!! *15* !!! ][ 120 ][ 22 ][ 17 ]]
|
||||
[[ long int->string ][ !!! *15* !!! ][ 120 ][ 22 ][ 16 ]]
|
||||
[[ long long->string ][ !!! *15* !!! ][ 120 ][ 22 ][ 17 ]]
|
||||
[[ unsigned int->string ][ !!! *15* !!! ][ 120 ][ 22 ][ 16 ]]
|
||||
[[ unsigned short->string ][ !!! *15* !!! ][ 120 ][ 22 ][ 18 ]]
|
||||
[[ unsigned long int->string ][ 16 ][ 118 ][ 22 ][ !!! *15* !!! ]]
|
||||
[[ unsigned long long->string ][ !!! *15* !!! ][ 117 ][ 21 ][ 17 ]]
|
||||
[[ bool->string ][ !!! *8* !!! ][ 117 ][ 23 ][ 10 ]]
|
||||
[[ float->string ][ 77 ][ 218 ][ 105 ][ !!! *50* !!! ]]
|
||||
[[ double->string ][ 108 ][ 247 ][ 129 ][ !!! *73* !!! ]]
|
||||
[[ long double->string ][ 120 ][ 250 ][ 131 ][ !!! *79* !!! ]]
|
||||
[[ char*->char ][ !!! *2* !!! ][ 99 ][ 9 ][ 7 ]]
|
||||
[[ char*->signed char ][ !!! *2* !!! ][ 98 ][ 9 ][ 8 ]]
|
||||
[[ char*->unsigned char ][ !!! *2* !!! ][ 98 ][ 9 ][ 13 ]]
|
||||
[[ char*->int ][ !!! *6* !!! ][ 115 ][ 22 ][ 15 ]]
|
||||
[[ char*->short ][ !!! *6* !!! ][ 114 ][ 22 ][ 15 ]]
|
||||
[[ char*->long int ][ !!! *6* !!! ][ 114 ][ 22 ][ 16 ]]
|
||||
[[ char*->long long ][ !!! *6* !!! ][ 119 ][ 22 ][ 15 ]]
|
||||
[[ char*->unsigned int ][ !!! *6* !!! ][ 114 ][ 20 ][ 15 ]]
|
||||
[[ char*->unsigned short ][ !!! *6* !!! ][ 116 ][ 20 ][ 15 ]]
|
||||
[[ char*->unsigned long int ][ !!! *6* !!! ][ 117 ][ 22 ][ 15 ]]
|
||||
[[ char*->unsigned long long ][ !!! *6* !!! ][ 118 ][ 22 ][ 15 ]]
|
||||
[[ char*->bool ][ !!! *3* !!! ][ 113 ][ 18 ][ 8 ]]
|
||||
[[ char*->float ][ !!! *15* !!! ][ 180 ][ 78 ][ 32 ]]
|
||||
[[ char*->double ][ !!! *16* !!! ][ 185 ][ 89 ][ 58 ]]
|
||||
[[ char*->long double ][ 119 ][ 193 ][ 91 ][ !!! *60* !!! ]]
|
||||
[[ unsigned char*->char ][ !!! *2* !!! ][ 99 ][ 9 ][ 8 ]]
|
||||
[[ unsigned char*->signed char ][ !!! *2* !!! ][ 99 ][ 10 ][ 8 ]]
|
||||
[[ unsigned char*->unsigned char ][ !!! *2* !!! ][ 100 ][ 9 ][ 15 ]]
|
||||
[[ unsigned char*->int ][ !!! *6* !!! ][ 118 ][ 22 ][ 15 ]]
|
||||
[[ unsigned char*->short ][ !!! *6* !!! ][ 117 ][ 26 ][ 15 ]]
|
||||
[[ unsigned char*->long int ][ !!! *6* !!! ][ 119 ][ 21 ][ 15 ]]
|
||||
[[ unsigned char*->long long ][ !!! *6* !!! ][ 118 ][ 21 ][ 14 ]]
|
||||
[[ unsigned char*->unsigned int ][ !!! *6* !!! ][ 115 ][ 22 ][ 14 ]]
|
||||
[[ unsigned char*->unsigned short ][ !!! *6* !!! ][ 117 ][ 20 ][ 15 ]]
|
||||
[[ unsigned char*->unsigned long int ][ !!! *6* !!! ][ 115 ][ 21 ][ 15 ]]
|
||||
[[ unsigned char*->unsigned long long ][ !!! *6* !!! ][ 117 ][ 22 ][ 15 ]]
|
||||
[[ unsigned char*->bool ][ !!! *3* !!! ][ 112 ][ 18 ][ 8 ]]
|
||||
[[ unsigned char*->float ][ !!! *15* !!! ][ 181 ][ 78 ][ 33 ]]
|
||||
[[ unsigned char*->double ][ !!! *16* !!! ][ 185 ][ 92 ][ 59 ]]
|
||||
[[ unsigned char*->long double ][ 120 ][ 190 ][ 89 ][ !!! *58* !!! ]]
|
||||
[[ unsigned char*->string ][ !!! *14* !!! ][ 121 ][ 22 ][ --- ]]
|
||||
[[ signed char*->char ][ !!! *2* !!! ][ 99 ][ 9 ][ 9 ]]
|
||||
[[ signed char*->signed char ][ !!! *2* !!! ][ 98 ][ 9 ][ 8 ]]
|
||||
[[ signed char*->unsigned char ][ !!! *2* !!! ][ 98 ][ 9 ][ 14 ]]
|
||||
[[ signed char*->int ][ !!! *6* !!! ][ 119 ][ 22 ][ 16 ]]
|
||||
[[ signed char*->short ][ !!! *6* !!! ][ 115 ][ 22 ][ 15 ]]
|
||||
[[ signed char*->long int ][ !!! *6* !!! ][ 119 ][ 22 ][ 15 ]]
|
||||
[[ signed char*->long long ][ !!! *6* !!! ][ 117 ][ 22 ][ 15 ]]
|
||||
[[ signed char*->unsigned int ][ !!! *6* !!! ][ 117 ][ 23 ][ 15 ]]
|
||||
[[ signed char*->unsigned short ][ !!! *6* !!! ][ 117 ][ 21 ][ 14 ]]
|
||||
[[ signed char*->unsigned long int ][ !!! *7* !!! ][ 119 ][ 24 ][ 15 ]]
|
||||
[[ signed char*->unsigned long long ][ !!! *6* !!! ][ 116 ][ 22 ][ 15 ]]
|
||||
[[ signed char*->bool ][ !!! *3* !!! ][ 111 ][ 18 ][ 8 ]]
|
||||
[[ signed char*->float ][ !!! *16* !!! ][ 180 ][ 78 ][ 33 ]]
|
||||
[[ signed char*->double ][ !!! *16* !!! ][ 185 ][ 89 ][ 59 ]]
|
||||
[[ signed char*->long double ][ 120 ][ 191 ][ 91 ][ !!! *59* !!! ]]
|
||||
[[ signed char*->string ][ !!! *14* !!! ][ 122 ][ 23 ][ --- ]]
|
||||
[[ int->int ][ !!! *<1* !!! ][ 120 ][ 22 ][ --- ]]
|
||||
[[ float->double ][ !!! *<1* !!! ][ 242 ][ 115 ][ --- ]]
|
||||
[[ double->double ][ !!! *<1* !!! ][ 243 ][ 115 ][ --- ]]
|
||||
[[ int->int ][ !!! *<1* !!! ][ 265 ][ 141 ][ --- ]]
|
||||
[[ int->int ][ !!! *<1* !!! ][ 266 ][ 140 ][ --- ]]
|
||||
[[ char->unsigned char ][ !!! *<1* !!! ][ 95 ][ 8 ][ --- ]]
|
||||
[[ char->signed char ][ !!! *<1* !!! ][ 95 ][ 8 ][ --- ]]
|
||||
[[ unsigned char->char ][ !!! *<1* !!! ][ 94 ][ 8 ][ --- ]]
|
||||
[[ signed char->char ][ !!! *<1* !!! ][ 94 ][ 8 ][ --- ]]
|
||||
]
|
||||
[endsect]
|
||||
|
||||
[/ END of section, generated by performance measuring program ]
|
||||
[endsect]
|
||||
|
||||
+23
-256
@@ -1,17 +1,17 @@
|
||||
// boost cast.hpp header file ----------------------------------------------//
|
||||
|
||||
// (C) Copyright boost.org 1999. Permission to copy, use, modify, sell
|
||||
// and distribute this software is granted provided this copyright
|
||||
// notice appears in all copies. This software is provided "as is" without
|
||||
// express or implied warranty, and with no claim as to its suitability for
|
||||
// any purpose.
|
||||
// (C) Copyright Kevlin Henney and Dave Abrahams 1999.
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
// See http://www.boost.org/libs/conversion for Documentation.
|
||||
|
||||
// Revision History
|
||||
// 02 Apr 01 Removed BOOST_NO_LIMITS workarounds and included
|
||||
// <boost/limits.hpp> instead (the workaround did not
|
||||
// actually compile when BOOST_NO_LIMITS was defined in
|
||||
// 23 JUn 05 numeric_cast removed and redirected to the new verion (Fernando Cacciola)
|
||||
// 02 Apr 01 Removed BOOST_NO_LIMITS workarounds and included
|
||||
// <boost/limits.hpp> instead (the workaround did not
|
||||
// actually compile when BOOST_NO_LIMITS was defined in
|
||||
// any case, so we loose nothing). (John Maddock)
|
||||
// 21 Jan 01 Undid a bug I introduced yesterday. numeric_cast<> never
|
||||
// worked with stock GCC; trying to get it to do that broke
|
||||
@@ -27,14 +27,14 @@
|
||||
// (Dave Abrahams)
|
||||
// 30 Jun 00 More MSVC6 wordarounds. See comments below. (Dave Abrahams)
|
||||
// 28 Jun 00 Removed implicit_cast<>. See comment below. (Beman Dawes)
|
||||
// 27 Jun 00 More MSVC6 workarounds
|
||||
// 27 Jun 00 More MSVC6 workarounds
|
||||
// 15 Jun 00 Add workarounds for MSVC6
|
||||
// 2 Feb 00 Remove bad_numeric_cast ";" syntax error (Doncho Angelov)
|
||||
// 26 Jan 00 Add missing throw() to bad_numeric_cast::what(0 (Adam Levar)
|
||||
// 29 Dec 99 Change using declarations so usages in other namespaces work
|
||||
// correctly (Dave Abrahams)
|
||||
// 23 Sep 99 Change polymorphic_downcast assert to also detect M.I. errors
|
||||
// as suggested Darin Adler and improved by Valentin Bonnard.
|
||||
// as suggested Darin Adler and improved by Valentin Bonnard.
|
||||
// 2 Sep 99 Remove controversial asserts, simplify, rename.
|
||||
// 30 Aug 99 Move to cast.hpp, replace value_cast with numeric_cast,
|
||||
// place in nested namespace.
|
||||
@@ -44,17 +44,18 @@
|
||||
#define BOOST_CAST_HPP
|
||||
|
||||
# include <boost/config.hpp>
|
||||
# include <cassert>
|
||||
# include <boost/assert.hpp>
|
||||
# include <typeinfo>
|
||||
# include <boost/type.hpp>
|
||||
# include <boost/limits.hpp>
|
||||
# include <boost/detail/select_type.hpp>
|
||||
|
||||
// It has been demonstrated numerous times that MSVC 6.0 fails silently at link
|
||||
// time if you use a template function which has template parameters that don't
|
||||
// appear in the function's argument list.
|
||||
//
|
||||
// TODO: Add this to config.hpp?
|
||||
# if defined(BOOST_MSVC) && BOOST_MSVC <= 1200 // 1200 = VC6
|
||||
# if defined(BOOST_MSVC) && BOOST_MSVC < 1300
|
||||
# define BOOST_EXPLICIT_DEFAULT_TARGET , ::boost::type<Target>* = 0
|
||||
# else
|
||||
# define BOOST_EXPLICIT_DEFAULT_TARGET
|
||||
@@ -68,7 +69,7 @@ namespace boost
|
||||
// polymorphic_cast --------------------------------------------------------//
|
||||
|
||||
// Runtime checked polymorphic downcasts and crosscasts.
|
||||
// Suggested in The C++ Programming Language, 3rd Ed, Bjarne Stroustrup,
|
||||
// Suggested in The C++ Programming Language, 3rd Ed, Bjarne Stroustrup,
|
||||
// section 15.8 exercise 1, page 425.
|
||||
|
||||
template <class Target, class Source>
|
||||
@@ -81,260 +82,26 @@ namespace boost
|
||||
|
||||
// polymorphic_downcast ----------------------------------------------------//
|
||||
|
||||
// assert() checked polymorphic downcast. Crosscasts prohibited.
|
||||
// BOOST_ASSERT() checked polymorphic downcast. Crosscasts prohibited.
|
||||
|
||||
// WARNING: Because this cast uses assert(), it violates the One Definition
|
||||
// Rule if NDEBUG is inconsistently defined across translation units.
|
||||
// WARNING: Because this cast uses BOOST_ASSERT(), it violates
|
||||
// the One Definition Rule if used in multiple translation units
|
||||
// where BOOST_DISABLE_ASSERTS, BOOST_ENABLE_ASSERT_HANDLER
|
||||
// NDEBUG are defined inconsistently.
|
||||
|
||||
// Contributed by Dave Abrahams
|
||||
|
||||
template <class Target, class Source>
|
||||
inline Target polymorphic_downcast(Source* x BOOST_EXPLICIT_DEFAULT_TARGET)
|
||||
{
|
||||
assert( dynamic_cast<Target>(x) == x ); // detect logic error
|
||||
BOOST_ASSERT( dynamic_cast<Target>(x) == x ); // detect logic error
|
||||
return static_cast<Target>(x);
|
||||
}
|
||||
|
||||
// implicit_cast -----------------------------------------------------------//
|
||||
//
|
||||
// Removed due to uncertain purpose. Use either numeric_cast (see below)
|
||||
// or static_cast according to the need.
|
||||
|
||||
// numeric_cast and related exception --------------------------------------//
|
||||
|
||||
// Contributed by Kevlin Henney
|
||||
|
||||
// bad_numeric_cast --------------------------------------------------------//
|
||||
|
||||
// exception used to indicate runtime numeric_cast failure
|
||||
class bad_numeric_cast : public std::bad_cast
|
||||
{
|
||||
public:
|
||||
// constructors, destructors and assignment operator defaulted
|
||||
|
||||
// function inlined for brevity and consistency with rest of library
|
||||
virtual const char *what() const throw()
|
||||
{
|
||||
return "bad numeric cast: loss of range in numeric_cast";
|
||||
}
|
||||
};
|
||||
|
||||
// numeric_cast ------------------------------------------------------------//
|
||||
|
||||
#if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || defined(BOOST_SGI_CPP_LIMITS)
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <bool is_signed> struct numeric_min_select;
|
||||
|
||||
template<>
|
||||
struct numeric_min_select<true>
|
||||
{
|
||||
template <class T>
|
||||
struct limits : std::numeric_limits<T>
|
||||
{
|
||||
static inline T min()
|
||||
# ifndef __GNUC__ // bug workaround courtesy Jens Maurer
|
||||
{
|
||||
return std::numeric_limits<T>::min() >= 0
|
||||
// unary minus causes integral promotion, thus the static_cast<>
|
||||
? static_cast<T>(-std::numeric_limits<T>::max())
|
||||
: std::numeric_limits<T>::min();
|
||||
}
|
||||
# else
|
||||
;
|
||||
# endif
|
||||
};
|
||||
};
|
||||
|
||||
# ifdef __GNUC__ // bug workaround courtesy Jens Maurer
|
||||
template<> template<class T>
|
||||
inline T numeric_min_select<true>::limits<T>::min()
|
||||
{
|
||||
return std::numeric_limits<T>::min() >= 0
|
||||
// unary minus causes integral promotion, thus the static_cast<>
|
||||
? static_cast<T>(-std::numeric_limits<T>::max())
|
||||
: std::numeric_limits<T>::min();
|
||||
}
|
||||
# endif
|
||||
|
||||
template<>
|
||||
struct numeric_min_select<false>
|
||||
{
|
||||
template <class T>
|
||||
struct limits : std::numeric_limits<T> {};
|
||||
};
|
||||
|
||||
// Move to namespace boost in utility.hpp?
|
||||
template <class T>
|
||||
struct fixed_numeric_limits
|
||||
: public numeric_min_select<
|
||||
std::numeric_limits<T>::is_signed
|
||||
>::template limits<T>
|
||||
{
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
// less_than_type_min -
|
||||
// x_is_signed should be numeric_limits<X>::is_signed
|
||||
// y_is_signed should be numeric_limits<Y>::is_signed
|
||||
// y_min should be numeric_limits<Y>::min()
|
||||
//
|
||||
// check(x, y_min) returns true iff x < y_min without invoking comparisons
|
||||
// between signed and unsigned values.
|
||||
//
|
||||
// "poor man's partial specialization" is in use here.
|
||||
template <bool x_is_signed, bool y_is_signed>
|
||||
struct less_than_type_min
|
||||
{
|
||||
template <class X, class Y>
|
||||
static bool check(X x, Y y_min)
|
||||
{ return x < y_min; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct less_than_type_min<false, true>
|
||||
{
|
||||
template <class X, class Y>
|
||||
static bool check(X, Y)
|
||||
{ return false; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct less_than_type_min<true, false>
|
||||
{
|
||||
template <class X, class Y>
|
||||
static bool check(X x, Y)
|
||||
{ return x < 0; }
|
||||
};
|
||||
|
||||
// greater_than_type_max -
|
||||
// same_sign should be:
|
||||
// numeric_limits<X>::is_signed == numeric_limits<Y>::is_signed
|
||||
// y_max should be numeric_limits<Y>::max()
|
||||
//
|
||||
// check(x, y_max) returns true iff x > y_max without invoking comparisons
|
||||
// between signed and unsigned values.
|
||||
//
|
||||
// "poor man's partial specialization" is in use here.
|
||||
template <bool same_sign, bool x_is_signed>
|
||||
struct greater_than_type_max;
|
||||
|
||||
template<>
|
||||
struct greater_than_type_max<true, true>
|
||||
{
|
||||
template <class X, class Y>
|
||||
static inline bool check(X x, Y y_max)
|
||||
{ return x > y_max; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct greater_than_type_max<false, true>
|
||||
{
|
||||
// What does the standard say about this? I think it's right, and it
|
||||
// will work with every compiler I know of.
|
||||
template <class X, class Y>
|
||||
static inline bool check(X x, Y)
|
||||
{ return x >= 0 && static_cast<X>(static_cast<Y>(x)) != x; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct greater_than_type_max<true, false>
|
||||
{
|
||||
template <class X, class Y>
|
||||
static inline bool check(X x, Y y_max)
|
||||
{ return x > y_max; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct greater_than_type_max<false, false>
|
||||
{
|
||||
// What does the standard say about this? I think it's right, and it
|
||||
// will work with every compiler I know of.
|
||||
template <class X, class Y>
|
||||
static inline bool check(X x, Y)
|
||||
{ return static_cast<X>(static_cast<Y>(x)) != x; }
|
||||
};
|
||||
|
||||
#else // use #pragma hacks if available
|
||||
|
||||
namespace detail
|
||||
{
|
||||
# if BOOST_MSVC
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable : 4018)
|
||||
# pragma warning(disable : 4146)
|
||||
#elif defined(__BORLANDC__)
|
||||
# pragma option push -w-8041
|
||||
# endif
|
||||
|
||||
// Move to namespace boost in utility.hpp?
|
||||
template <class T>
|
||||
struct fixed_numeric_limits : public std::numeric_limits<T>
|
||||
{
|
||||
static inline T min()
|
||||
{
|
||||
return std::numeric_limits<T>::is_signed && std::numeric_limits<T>::min() >= 0
|
||||
? T(-std::numeric_limits<T>::max()) : std::numeric_limits<T>::min();
|
||||
}
|
||||
};
|
||||
|
||||
# if BOOST_MSVC
|
||||
# pragma warning(pop)
|
||||
#elif defined(__BORLANDC__)
|
||||
# pragma option pop
|
||||
# endif
|
||||
} // namespace detail
|
||||
|
||||
#endif
|
||||
|
||||
template<typename Target, typename Source>
|
||||
inline Target numeric_cast(Source arg BOOST_EXPLICIT_DEFAULT_TARGET)
|
||||
{
|
||||
// typedefs abbreviating respective trait classes
|
||||
typedef std::numeric_limits<Source> arg_traits;
|
||||
typedef detail::fixed_numeric_limits<Target> result_traits;
|
||||
|
||||
#if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || defined(BOOST_SGI_CPP_LIMITS)
|
||||
// typedefs that act as compile time assertions
|
||||
// (to be replaced by boost compile time assertions
|
||||
// as and when they become available and are stable)
|
||||
typedef bool argument_must_be_numeric[arg_traits::is_specialized];
|
||||
typedef bool result_must_be_numeric[result_traits::is_specialized];
|
||||
|
||||
const bool arg_is_signed = arg_traits::is_signed;
|
||||
const bool result_is_signed = result_traits::is_signed;
|
||||
const bool same_sign = arg_is_signed == result_is_signed;
|
||||
|
||||
if (less_than_type_min<arg_is_signed, result_is_signed>::check(arg, result_traits::min())
|
||||
|| greater_than_type_max<same_sign, arg_is_signed>::check(arg, result_traits::max())
|
||||
)
|
||||
|
||||
#else // We need to use #pragma hacks if available
|
||||
|
||||
# if BOOST_MSVC
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable : 4018)
|
||||
#elif defined(__BORLANDC__)
|
||||
#pragma option push -w-8012
|
||||
# endif
|
||||
if ((arg < 0 && !result_traits::is_signed) // loss of negative range
|
||||
|| (arg_traits::is_signed && arg < result_traits::min()) // underflow
|
||||
|| arg > result_traits::max()) // overflow
|
||||
# if BOOST_MSVC
|
||||
# pragma warning(pop)
|
||||
#elif defined(__BORLANDC__)
|
||||
#pragma option pop
|
||||
# endif
|
||||
#endif
|
||||
{
|
||||
throw bad_numeric_cast();
|
||||
}
|
||||
return static_cast<Target>(arg);
|
||||
} // numeric_cast
|
||||
|
||||
# undef BOOST_EXPLICIT_DEFAULT_TARGET
|
||||
|
||||
} // namespace boost
|
||||
|
||||
# include <boost/numeric/conversion/cast.hpp>
|
||||
|
||||
#endif // BOOST_CAST_HPP
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
// Copyright Alexander Nasonov & Paul A. Bristow 2006.
|
||||
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
|
||||
#define BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
|
||||
|
||||
#include <climits>
|
||||
#include <ios>
|
||||
#include <limits>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/integer_traits.hpp>
|
||||
|
||||
#ifndef BOOST_NO_IS_ABSTRACT
|
||||
// Fix for SF:1358600 - lexical_cast & pure virtual functions & VC 8 STL
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/type_traits/is_abstract.hpp>
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || \
|
||||
(defined(BOOST_MSVC) && (BOOST_MSVC<1310))
|
||||
|
||||
#define BOOST_LCAST_NO_COMPILE_TIME_PRECISION
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
|
||||
#include <boost/assert.hpp>
|
||||
#else
|
||||
#include <boost/static_assert.hpp>
|
||||
#endif
|
||||
|
||||
namespace boost { namespace detail {
|
||||
|
||||
class lcast_abstract_stub {};
|
||||
|
||||
#ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
|
||||
// Calculate an argument to pass to std::ios_base::precision from
|
||||
// lexical_cast. See alternative implementation for broken standard
|
||||
// libraries in lcast_get_precision below. Keep them in sync, please.
|
||||
template<class T>
|
||||
struct lcast_precision
|
||||
{
|
||||
#ifdef BOOST_NO_IS_ABSTRACT
|
||||
typedef std::numeric_limits<T> limits; // No fix for SF:1358600.
|
||||
#else
|
||||
typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
|
||||
boost::is_abstract<T>
|
||||
, std::numeric_limits<lcast_abstract_stub>
|
||||
, std::numeric_limits<T>
|
||||
>::type limits;
|
||||
#endif
|
||||
|
||||
BOOST_STATIC_CONSTANT(bool, use_default_precision =
|
||||
!limits::is_specialized || limits::is_exact
|
||||
);
|
||||
|
||||
BOOST_STATIC_CONSTANT(bool, is_specialized_bin =
|
||||
!use_default_precision &&
|
||||
limits::radix == 2 && limits::digits > 0
|
||||
);
|
||||
|
||||
BOOST_STATIC_CONSTANT(bool, is_specialized_dec =
|
||||
!use_default_precision &&
|
||||
limits::radix == 10 && limits::digits10 > 0
|
||||
);
|
||||
|
||||
BOOST_STATIC_CONSTANT(std::streamsize, streamsize_max =
|
||||
boost::integer_traits<std::streamsize>::const_max
|
||||
);
|
||||
|
||||
BOOST_STATIC_CONSTANT(unsigned int, precision_dec = limits::digits10 + 1U);
|
||||
|
||||
BOOST_STATIC_ASSERT(!is_specialized_dec ||
|
||||
precision_dec <= streamsize_max + 0UL
|
||||
);
|
||||
|
||||
BOOST_STATIC_CONSTANT(unsigned long, precision_bin =
|
||||
2UL + limits::digits * 30103UL / 100000UL
|
||||
);
|
||||
|
||||
BOOST_STATIC_ASSERT(!is_specialized_bin ||
|
||||
(limits::digits + 0UL < ULONG_MAX / 30103UL &&
|
||||
precision_bin > limits::digits10 + 0UL &&
|
||||
precision_bin <= streamsize_max + 0UL)
|
||||
);
|
||||
|
||||
BOOST_STATIC_CONSTANT(std::streamsize, value =
|
||||
is_specialized_bin ? precision_bin
|
||||
: is_specialized_dec ? precision_dec : 6
|
||||
);
|
||||
};
|
||||
#endif
|
||||
|
||||
template<class T>
|
||||
inline std::streamsize lcast_get_precision(T* = 0)
|
||||
{
|
||||
#ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
|
||||
return lcast_precision<T>::value;
|
||||
#else // Follow lcast_precision algorithm at run-time:
|
||||
|
||||
#ifdef BOOST_NO_IS_ABSTRACT
|
||||
typedef std::numeric_limits<T> limits; // No fix for SF:1358600.
|
||||
#else
|
||||
typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
|
||||
boost::is_abstract<T>
|
||||
, std::numeric_limits<lcast_abstract_stub>
|
||||
, std::numeric_limits<T>
|
||||
>::type limits;
|
||||
#endif
|
||||
|
||||
bool const use_default_precision =
|
||||
!limits::is_specialized || limits::is_exact;
|
||||
|
||||
if(!use_default_precision)
|
||||
{ // Includes all built-in floating-point types, float, double ...
|
||||
// and UDT types for which digits (significand bits) is defined (not zero)
|
||||
|
||||
bool const is_specialized_bin =
|
||||
limits::radix == 2 && limits::digits > 0;
|
||||
bool const is_specialized_dec =
|
||||
limits::radix == 10 && limits::digits10 > 0;
|
||||
std::streamsize const streamsize_max =
|
||||
(boost::integer_traits<std::streamsize>::max)();
|
||||
|
||||
if(is_specialized_bin)
|
||||
{ // Floating-point types with
|
||||
// limits::digits defined by the specialization.
|
||||
|
||||
unsigned long const digits = limits::digits;
|
||||
unsigned long const precision = 2UL + digits * 30103UL / 100000UL;
|
||||
// unsigned long is selected because it is at least 32-bits
|
||||
// and thus ULONG_MAX / 30103UL is big enough for all types.
|
||||
BOOST_ASSERT(
|
||||
digits < ULONG_MAX / 30103UL &&
|
||||
precision > limits::digits10 + 0UL &&
|
||||
precision <= streamsize_max + 0UL
|
||||
);
|
||||
return precision;
|
||||
}
|
||||
else if(is_specialized_dec)
|
||||
{ // Decimal Floating-point type, most likely a User Defined Type
|
||||
// rather than a real floating-point hardware type.
|
||||
unsigned int const precision = limits::digits10 + 1U;
|
||||
BOOST_ASSERT(precision <= streamsize_max + 0UL);
|
||||
return precision;
|
||||
}
|
||||
}
|
||||
|
||||
// Integral type (for which precision has no effect)
|
||||
// or type T for which limits is NOT specialized,
|
||||
// so assume stream precision remains the default 6 decimal digits.
|
||||
// Warning: if your User-defined Floating-point type T is NOT specialized,
|
||||
// then you may lose accuracy by only using 6 decimal digits.
|
||||
// To avoid this, you need to specialize T with either
|
||||
// radix == 2 and digits == the number of significand bits,
|
||||
// OR
|
||||
// radix = 10 and digits10 == the number of decimal digits.
|
||||
|
||||
return 6;
|
||||
#endif
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void lcast_set_precision(std::ios_base& stream, T*)
|
||||
{
|
||||
stream.precision(lcast_get_precision<T>());
|
||||
}
|
||||
|
||||
template<class Source, class Target>
|
||||
inline void lcast_set_precision(std::ios_base& stream, Source*, Target*)
|
||||
{
|
||||
std::streamsize const s = lcast_get_precision(static_cast<Source*>(0));
|
||||
std::streamsize const t = lcast_get_precision(static_cast<Target*>(0));
|
||||
stream.precision(s > t ? s : t);
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
#endif // BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright David Abrahams 2003.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
#ifndef IMPLICIT_CAST_DWA200356_HPP
|
||||
# define IMPLICIT_CAST_DWA200356_HPP
|
||||
|
||||
# include <boost/mpl/identity.hpp>
|
||||
|
||||
namespace boost {
|
||||
|
||||
// implementation originally suggested by C. Green in
|
||||
// http://lists.boost.org/MailArchives/boost/msg00886.php
|
||||
|
||||
// The use of identity creates a non-deduced form, so that the
|
||||
// explicit template argument must be supplied
|
||||
template <typename T>
|
||||
inline T implicit_cast (typename mpl::identity<T>::type x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
// incomplete return type now is here
|
||||
//template <typename T>
|
||||
//void implicit_cast (...);
|
||||
|
||||
} // namespace boost
|
||||
|
||||
|
||||
#endif // IMPLICIT_CAST_DWA200356_HPP
|
||||
+2163
-44
File diff suppressed because it is too large
Load Diff
@@ -3,14 +3,14 @@
|
||||
<head>
|
||||
<meta http-equiv="Content-Language" content="en-us">
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
|
||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||
<title>Boost Conversion Library</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" text="#000000">
|
||||
|
||||
<h1><img border="0" src="../../c++boost.gif" align="center" width="277" height="86">Boost
|
||||
<h1><img border="0" src="../../boost.png" align="center" width="277" height="86">Boost
|
||||
Conversion Library</h1>
|
||||
|
||||
<p>The Conversion Library improves program safety and clarity by performing
|
||||
@@ -22,18 +22,17 @@ supplied by several headers:</p>
|
||||
<ul>
|
||||
<li>The <a href="cast.htm">boost/cast</a> header provides <b>polymorphic_cast<></b>
|
||||
and <b>polymorphic_downcast<></b> to perform safe casting between
|
||||
polymorphic types, and <b> numeric_cast</b><i><></i> to perform safe casting
|
||||
between numeric types.<br>
|
||||
polymorphic types.<br>
|
||||
</li>
|
||||
<li>The <a href="lexical_cast.htm">boost/lexical_cast</a> header provides <b>lexical_cast<></b>
|
||||
<li>The <a href="../../doc/html/boost_lexical_cast.html">boost/lexical_cast</a> header provides <b>lexical_cast<></b>
|
||||
general literal text conversions, such as an <code>int</code> represented as
|
||||
a <code>string</code>, or vice-versa.</li>
|
||||
</ul>
|
||||
<hr>
|
||||
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED"
|
||||
S-Format="%d %B, %Y" startspan -->06 January, 2001<!--webbot bot="Timestamp" endspan i-checksum="38320" -->
|
||||
S-Format="%d %B, %Y" startspan -->June 23, 2005<!--webbot bot="Timestamp" endspan i-checksum="30348" -->
|
||||
</p>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
+11
-237
@@ -1,242 +1,16 @@
|
||||
<!doctype html public "-//W3C//DTD HTML Transitional 4.0//EN">
|
||||
|
||||
<!--
|
||||
Copyright 2005-2007 Daniel James.
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
-->
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>lexical_cast</title>
|
||||
<meta name="author" content="Kevlin Henney, mailto:kevlin@curbralan.com">
|
||||
<meta name="generator" content="Microsoft FrontPage 4.0">
|
||||
<meta http-equiv="refresh" content="0; URL=../../doc/html/boost_lexical_cast.html">
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" text="#000000">
|
||||
|
||||
<h1><img src="../../c++boost.gif" alt="c++boost.gif (8819 bytes)" align="center" width="277" height="86">Header
|
||||
<a href="../../boost/lexical_cast.hpp">boost/lexical_cast.hpp</a></h1>
|
||||
|
||||
<ul>
|
||||
<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="#portability">Portability</a></li>
|
||||
<li><a href="#future">Future directions</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>
|
||||
<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 relatively high entry level requires an extreme degree of involvement
|
||||
for simple conversions.
|
||||
<p>
|
||||
The <code>lexical_cast</code> template function 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="cast.htm#numeric_cast">numeric_cast</a></code> may offer more reasonable
|
||||
behavior than <code>lexical_cast</code>.
|
||||
<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>(Source arg);
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
Test harness 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(Source arg);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
Returns the result of streaming <code>arg</code> into a <code>std::stringstream</code> and then
|
||||
out as a <code>Target</code> object. The conversion is parameterized by the current
|
||||
<a href="#lexical_context"><code>lexical_context</code></a>, if set. If the conversion is
|
||||
unsuccessful, a <a href="#bad_lexical_cast"><code>bad_lexical_cast</code></a> exception is thrown
|
||||
if the current <a href="#lexical_context"><code>lexical_context</code></a> is set for throwing or
|
||||
if there is no current <a href="#lexical_context"><code>lexical_context</code></a> set, otherwise a
|
||||
<code>Target()</code> is returned.
|
||||
<p>
|
||||
The requirements on the argument and result types are:
|
||||
<ul>
|
||||
<li>
|
||||
<code>Source</code> is <i>OutputStreamable</i>, meaning that an
|
||||
<code>operator<<</code> is defined that takes a
|
||||
<code>std::ostream</code> object on the left hand side and an instance
|
||||
of the argument type on the right.
|
||||
</li>
|
||||
<li>
|
||||
Both <code>Source</code> and <code>Target</code> are <i>CopyConstructible</i> [20.1.3].
|
||||
</li>
|
||||
<li>
|
||||
<code>Target</code> is <i>InputStreamable</i>, meaning that an
|
||||
<code>operator>></code> is defined that takes a
|
||||
<code>std::istream</code> object on the left hand side and an instance
|
||||
of the result type on the right.
|
||||
</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.3].
|
||||
</li>
|
||||
<li>
|
||||
<code>Target</code> is <i>Assignable</i> [23.1].
|
||||
</li>
|
||||
</ul>
|
||||
<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:
|
||||
virtual const char * what() const throw();
|
||||
};
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
Exception used to indicate runtime <a href="#lexical_cast"><code>lexical_cast</code></a> failure.
|
||||
<p>
|
||||
|
||||
<hr>
|
||||
<h2><a name="portability">Portability</a></h2>
|
||||
|
||||
To date the code and test harness have been compiled successfully using
|
||||
Microsoft Visual C++ 6.0, Borland C++ 5.5, and GNU g++ 2.91. Tests have run successfully for
|
||||
Microsoft Visual C++ 6.0 and Borland C++ 5.5. For g++ streams interpret any integer, rather than
|
||||
just <code>0</code> and <code>1</code>, as valid for <code>bool</code>; the other tests pass
|
||||
without problem. The deprecated standard header <code><strstream></code> is used in
|
||||
preference to the standard <code><sstream></code> header for out-of-the-box g++ support.
|
||||
<p>
|
||||
|
||||
<hr>
|
||||
<h2><a name="future">Future directions</a></h2>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
A mechanism for providing quality-of-service control is needed, e.g. formatting and exception
|
||||
behavior. In the name of simplicity (and release), the current version strips out an earlier
|
||||
experimental version.
|
||||
</li>
|
||||
<li>
|
||||
Wide character and incompatible <code>std::basic_string</code> issues need to be catered for.
|
||||
</li>
|
||||
<li>
|
||||
An <code>interpret_cast</code> that performs a <i>do-something-reasonable</i> conversion between
|
||||
types. It would, for instance, select between <code>numeric_cast</code> and <code>lexical_cast</code>
|
||||
based on <code>std::numeric_limits<>::is_specialized</code>.
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
|
||||
<div align="right"><small><i>© Copyright Kevlin Henney, 2000</i></small></div>
|
||||
|
||||
<body>
|
||||
Automatic redirection failed, please go to
|
||||
<a href="../../doc/html/boost_lexical_cast.html">../../doc/html/boost_lexical_cast.html</a>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
+1031
-137
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,100 @@
|
||||
// boost utility cast test program -----------------------------------------//
|
||||
|
||||
// (C) Copyright Beman Dawes, Dave Abrahams 1999. Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
|
||||
// Revision History
|
||||
// 28 Set 04 factored out numeric_cast<> test (Fernando Cacciola)
|
||||
// 20 Jan 01 removed use of <limits> for portability to raw GCC (David Abrahams)
|
||||
// 28 Jun 00 implicit_cast removed (Beman Dawes)
|
||||
// 30 Aug 99 value_cast replaced by numeric_cast
|
||||
// 3 Aug 99 Initial Version
|
||||
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
#include <cfloat> // for DBL_MAX (Peter Schmid)
|
||||
#include <boost/cast.hpp>
|
||||
|
||||
#include "boost/test/minimal.hpp"
|
||||
|
||||
# if SCHAR_MAX == LONG_MAX
|
||||
# error "This test program doesn't work if SCHAR_MAX == LONG_MAX"
|
||||
# endif
|
||||
|
||||
using namespace boost;
|
||||
using std::cout;
|
||||
|
||||
|
||||
int test_main( int argc, char * argv[] )
|
||||
{
|
||||
|
||||
# ifdef NDEBUG
|
||||
cout << "NDEBUG is defined\n";
|
||||
# else
|
||||
cout << "NDEBUG is not defined\n";
|
||||
# endif
|
||||
|
||||
cout << "\nBeginning tests...\n";
|
||||
|
||||
// test implicit_cast and numeric_cast -------------------------------------//
|
||||
|
||||
// tests which should succeed
|
||||
long small_value = 1;
|
||||
long small_negative_value = -1;
|
||||
long large_value = LONG_MAX;
|
||||
long large_negative_value = LONG_MIN;
|
||||
signed char c = 0;
|
||||
|
||||
c = large_value; // see if compiler generates warning
|
||||
|
||||
c = numeric_cast<signed char>( small_value );
|
||||
BOOST_CHECK( c == 1 );
|
||||
c = 0;
|
||||
c = numeric_cast<signed char>( small_value );
|
||||
BOOST_CHECK( c == 1 );
|
||||
c = 0;
|
||||
c = numeric_cast<signed char>( small_negative_value );
|
||||
BOOST_CHECK( c == -1 );
|
||||
|
||||
// These tests courtesy of Joe R NWP Swatosh<joe.r.swatosh@usace.army.mil>
|
||||
BOOST_CHECK( 0.0f == numeric_cast<float>( 0.0 ) );
|
||||
BOOST_CHECK( 0.0 == numeric_cast<double>( 0.0 ) );
|
||||
|
||||
// tests which should result in errors being detected
|
||||
|
||||
bool caught_exception = false;
|
||||
try { c = numeric_cast<signed char>( large_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #1\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
caught_exception = false;
|
||||
try { c = numeric_cast<signed char>( large_negative_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #2\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
unsigned long ul;
|
||||
caught_exception = false;
|
||||
try { ul = numeric_cast<unsigned long>( large_negative_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #3\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
caught_exception = false;
|
||||
try { ul = numeric_cast<unsigned long>( small_negative_value ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #4\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
caught_exception = false;
|
||||
try { numeric_cast<int>( DBL_MAX ); }
|
||||
catch (bad_numeric_cast)
|
||||
{ cout<<"caught bad_numeric_cast #5\n"; caught_exception = true; }
|
||||
BOOST_CHECK ( caught_exception );
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
@@ -44,7 +44,7 @@ namespace test // failure exception used to indicate checked test failures
|
||||
}
|
||||
|
||||
// std::~string has no exception-specification (could throw anything),
|
||||
// but we need to be compatible with std::~exception's empty one
|
||||
// but we need to be compatible with std::~exception's empty one
|
||||
// see std::15.4p13 and std::15.4p3
|
||||
~failure() throw()
|
||||
{
|
||||
@@ -303,10 +303,6 @@ namespace test // tester is the driver class for a sequence of tests
|
||||
|
||||
// Copyright Kevlin Henney, 2000. All rights reserved.
|
||||
//
|
||||
// Permission to use, copy, modify, and distribute this software for any
|
||||
// purpose is hereby granted without fee, provided that this copyright and
|
||||
// permissions notice appear in all copies and derivatives, and that no
|
||||
// charge may be made for the software and its documentation except to cover
|
||||
// cost of distribution.
|
||||
//
|
||||
// This software is provided "as is" without express or implied warranty.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
# Signals library
|
||||
|
||||
# Copyright (C) 2001-2003 Douglas Gregor
|
||||
|
||||
# Permission to copy, use, sell and distribute this software is granted
|
||||
# provided this copyright notice appears in all copies. Permission to modify
|
||||
# the code and to distribute modified code is granted provided this copyright
|
||||
# notice appears in all copies, and a notice that the code was modified is
|
||||
# included with the copyright notice. This software is provided "as is"
|
||||
# without express or implied warranty, and with no claim as to its suitability
|
||||
# for any purpose.
|
||||
|
||||
# For more information, see http://www.boost.org/
|
||||
|
||||
# bring in rules for testing
|
||||
import testing ;
|
||||
import feature ;
|
||||
|
||||
feature.feature nowchar : on :
|
||||
composite optional propagated link-incompatible ;
|
||||
feature.compose <nowchar>on : <cxxflags>/Zc:wchar_t- ;
|
||||
|
||||
test-suite conversion
|
||||
: [ run implicit_cast.cpp ]
|
||||
[ compile-fail implicit_cast_fail.cpp ]
|
||||
[ run ../cast_test.cpp ]
|
||||
[ run ../numeric_cast_test.cpp ]
|
||||
[ run ../lexical_cast_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_loopback_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_abstract_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_noncopyable_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_vc8_bug_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_wchars_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_float_types_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_inf_nan_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_containers_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_empty_input_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_pointers_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ compile lexical_cast_typedefed_wchar_test.cpp : <toolset>msvc:<nowchar>on ]
|
||||
[ run lexical_cast_typedefed_wchar_test_runtime.cpp ../../test/build//boost_unit_test_framework/<link>static : : : <toolset>msvc:<nowchar>on ]
|
||||
;
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright David Abrahams 2003.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/implicit_cast.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
#include <boost/type.hpp>
|
||||
using boost::implicit_cast;
|
||||
using boost::type;
|
||||
|
||||
template <class T>
|
||||
type<T> check_return(T) { return type<T>(); }
|
||||
|
||||
struct foo
|
||||
{
|
||||
foo(char const*) {}
|
||||
operator long() const { return 0; }
|
||||
};
|
||||
|
||||
typedef type<long> long_type;
|
||||
typedef type<foo> foo_type;
|
||||
|
||||
int main()
|
||||
{
|
||||
type<long> x = check_return(boost::implicit_cast<long>(1));
|
||||
BOOST_TEST(boost::implicit_cast<long>(1) == 1L);
|
||||
|
||||
type<foo> f = check_return(boost::implicit_cast<foo>("hello"));
|
||||
type<long> z = check_return(boost::implicit_cast<long>(foo("hello")));
|
||||
return boost::report_errors();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright David Abrahams 2003.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/implicit_cast.hpp>
|
||||
#include <boost/type.hpp>
|
||||
|
||||
#define BOOST_INCLUDE_MAIN
|
||||
#include <boost/test/test_tools.hpp>
|
||||
|
||||
using boost::implicit_cast;
|
||||
|
||||
struct foo
|
||||
{
|
||||
explicit foo(char const*) {}
|
||||
};
|
||||
|
||||
int test_main(int, char*[])
|
||||
{
|
||||
foo x = implicit_cast<foo>("foobar");
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Sergey Shandar 2005, Alexander Nasonov, 2007.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
//
|
||||
// Test abstract class. Bug 1358600:
|
||||
// http://sf.net/tracker/?func=detail&aid=1358600&group_id=7586&atid=107586
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
void test_abstract();
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_abstract));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
virtual void out(std::ostream &) const = 0;
|
||||
};
|
||||
|
||||
class B: public A
|
||||
{
|
||||
public:
|
||||
virtual void out(std::ostream &O) const { O << "B"; }
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &O, const A &a)
|
||||
{
|
||||
a.out(O);
|
||||
return O;
|
||||
}
|
||||
|
||||
void test_abstract()
|
||||
{
|
||||
const A &a = B();
|
||||
BOOST_CHECK(boost::lexical_cast<std::string>(a) == "B");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// Testing boost::lexical_cast with boost::container::string.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2011.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/container/string.hpp>
|
||||
|
||||
void testing_boost_containers_basic_string();
|
||||
|
||||
using namespace boost;
|
||||
|
||||
boost::unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("Testing boost::lexical_cast with boost::container::string");
|
||||
suite->add(BOOST_TEST_CASE(testing_boost_containers_basic_string));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
void testing_boost_containers_basic_string()
|
||||
{
|
||||
BOOST_CHECK("100" == lexical_cast<boost::container::string>("100"));
|
||||
BOOST_CHECK(L"100" == lexical_cast<boost::container::wstring>(L"100"));
|
||||
|
||||
BOOST_CHECK("100" == lexical_cast<boost::container::string>(100));
|
||||
boost::container::string str("1000");
|
||||
BOOST_CHECK(1000 == lexical_cast<int>(str));
|
||||
}
|
||||
|
||||
|
||||
Executable
+151
@@ -0,0 +1,151 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2011.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
|
||||
#define BOOST_LCAST_NO_WCHAR_T
|
||||
#endif
|
||||
|
||||
template <class T>
|
||||
void do_test_on_empty_input(T& v)
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<float>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<long double>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned int>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned short>(v), bad_lexical_cast);
|
||||
#if defined(BOOST_HAS_LONG_LONG)
|
||||
BOOST_CHECK_THROW(lexical_cast<boost::ulong_long_type>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<boost::long_long_type>(v), bad_lexical_cast);
|
||||
#elif defined(BOOST_HAS_MS_INT64)
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned __int64>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<__int64>(v), bad_lexical_cast);
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_empty_iterator_range()
|
||||
{
|
||||
|
||||
boost::iterator_range<char*> v;
|
||||
do_test_on_empty_input(v);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(v), std::string());
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
|
||||
|
||||
boost::iterator_range<const char*> cv;
|
||||
do_test_on_empty_input(cv);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(cv), std::string());
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(cv), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(cv), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(cv), bad_lexical_cast);
|
||||
|
||||
const boost::iterator_range<const char*> ccv;
|
||||
do_test_on_empty_input(ccv);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(ccv), std::string());
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(ccv), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(ccv), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(ccv), bad_lexical_cast);
|
||||
}
|
||||
|
||||
void test_empty_string()
|
||||
{
|
||||
std::string v;
|
||||
do_test_on_empty_input(v);
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
|
||||
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
std::wstring vw;
|
||||
do_test_on_empty_input(vw);
|
||||
BOOST_CHECK_THROW(lexical_cast<wchar_t>(vw), bad_lexical_cast);
|
||||
#endif
|
||||
|
||||
// Currently, no compiler and STL library fully support char16_t and char32_t
|
||||
//#ifndef BOOST_NO_CHAR16_T
|
||||
// std::basic_string<char16_t> v16w;
|
||||
// do_test_on_empty_input(v16w);
|
||||
// BOOST_CHECK_THROW(lexical_cast<char16_t>(v16w), bad_lexical_cast);
|
||||
//#endif
|
||||
//#ifndef BOOST_NO_CHAR32_T
|
||||
// std::basic_string<char32_t> v32w;
|
||||
// do_test_on_empty_input(v32w);
|
||||
// BOOST_CHECK_THROW(lexical_cast<char32_t>(v32w), bad_lexical_cast);
|
||||
//#endif
|
||||
}
|
||||
|
||||
struct Escape
|
||||
{
|
||||
Escape(const std::string& s)
|
||||
: str_(s)
|
||||
{}
|
||||
|
||||
std::string str_;
|
||||
};
|
||||
|
||||
inline std::ostream& operator<< (std::ostream& o, const Escape& rhs)
|
||||
{
|
||||
return o << rhs.str_;
|
||||
}
|
||||
|
||||
void test_empty_user_class()
|
||||
{
|
||||
Escape v("");
|
||||
do_test_on_empty_input(v);
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
|
||||
}
|
||||
|
||||
namespace std {
|
||||
inline std::ostream & operator<<(std::ostream & out, const std::vector<long> & v)
|
||||
{
|
||||
std::ostream_iterator<long> it(out);
|
||||
std::copy(v.begin(), v.end(), it);
|
||||
assert(out);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
void test_empty_vector()
|
||||
{
|
||||
std::vector<long> v;
|
||||
do_test_on_empty_input(v);
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast. Empty input unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_iterator_range));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_string));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_user_class));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_vector));
|
||||
|
||||
return suite;
|
||||
}
|
||||
Executable
+513
@@ -0,0 +1,513 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2011.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/floating_point_comparison.hpp>
|
||||
|
||||
void test_conversion_from_to_float();
|
||||
void test_conversion_from_to_double();
|
||||
void test_conversion_from_to_long_double();
|
||||
|
||||
using namespace boost;
|
||||
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast float types unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_float));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_double));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_long_double));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
|
||||
// Replace "-,999" with "-999".
|
||||
template<class CharT>
|
||||
std::basic_string<CharT> to_str_gcc_workaround(std::basic_string<CharT> str)
|
||||
{
|
||||
std::locale loc;
|
||||
std::numpunct<CharT> const& np = BOOST_USE_FACET(std::numpunct<CharT>, loc);
|
||||
std::ctype<CharT> const& ct = BOOST_USE_FACET(std::ctype<CharT>, loc);
|
||||
|
||||
if(np.grouping().empty())
|
||||
return str;
|
||||
|
||||
CharT prefix[3] = { ct.widen('-'), np.thousands_sep(), CharT() };
|
||||
|
||||
if(str.find(prefix) != 0)
|
||||
return str;
|
||||
|
||||
prefix[1] = CharT();
|
||||
str.replace(0, 2, prefix);
|
||||
return str;
|
||||
}
|
||||
|
||||
template<class CharT, class T>
|
||||
std::basic_string<CharT> to_str(T t)
|
||||
{
|
||||
std::basic_ostringstream<CharT> o;
|
||||
o << t;
|
||||
return to_str_gcc_workaround(o.str());
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
void test_conversion_from_to_float_for_locale()
|
||||
{
|
||||
std::locale current_locale;
|
||||
typedef std::numpunct<char> numpunct;
|
||||
numpunct const& np = BOOST_USE_FACET(numpunct, current_locale);
|
||||
if ( !np.grouping().empty() )
|
||||
{
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<T>( std::string("100") + np.thousands_sep() + np.thousands_sep() + "0" )
|
||||
, bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( std::string("100") + np.thousands_sep() ), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( np.thousands_sep() + std::string("100") ), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( std::string("1") + np.thousands_sep() + np.decimal_point() + "e10" ), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( std::string("1e10") + np.thousands_sep() ), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( std::string("1") + np.thousands_sep() + "e10" ), bad_lexical_cast);
|
||||
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( to_str< char >(100000) ), 100000, (std::numeric_limits<T>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( to_str< char >(10000000u) ), 10000000u, (std::numeric_limits<T>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( to_str< char >(100) ), 100, (std::numeric_limits<T>::epsilon()) );
|
||||
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( to_str< wchar_t >(100000) ), 100000, (std::numeric_limits<T>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( to_str< wchar_t >(10000000u) ), 10000000u, (std::numeric_limits<T>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( to_str< wchar_t >(100) ), 100, (std::numeric_limits<T>::epsilon()) );
|
||||
#endif
|
||||
// Exception must not be thrown, when we are using no separators at all
|
||||
BOOST_CHECK_CLOSE_FRACTION( lexical_cast<T>("30000"), static_cast<T>(30000), (std::numeric_limits<T>::epsilon()) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Converts char* [and wchar_t] to float number type and checks, that generated
|
||||
* number is in interval [base_value-epsilon, base_value+epsilon].
|
||||
*/
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
#define CHECK_CLOSE_ABS_DIFF(VAL,PREFIX) \
|
||||
converted_val = lexical_cast<test_t>(#VAL); \
|
||||
BOOST_CHECK_CLOSE_FRACTION( (VAL ## L? VAL ## L : std::numeric_limits<test_t>::epsilon()), \
|
||||
(converted_val ? converted_val : std::numeric_limits<test_t>::epsilon()), \
|
||||
std::numeric_limits<test_t>::epsilon() \
|
||||
); \
|
||||
BOOST_CHECK_EQUAL(converted_val, lexical_cast<test_t>(L## #VAL) );
|
||||
|
||||
#else
|
||||
#define CHECK_CLOSE_ABS_DIFF(VAL,TYPE) \
|
||||
converted_val = lexical_cast<test_t>(#VAL); \
|
||||
BOOST_CHECK_CLOSE_FRACTION( (VAL ## L? VAL ## L : std::numeric_limits<test_t>::epsilon()), \
|
||||
(converted_val ? converted_val : std::numeric_limits<test_t>::epsilon()), \
|
||||
std::numeric_limits<test_t>::epsilon() \
|
||||
);
|
||||
#endif
|
||||
|
||||
template <class TestType>
|
||||
void test_converion_to_float_types()
|
||||
{
|
||||
typedef TestType test_t;
|
||||
test_t converted_val;
|
||||
|
||||
BOOST_CHECK_CLOSE_FRACTION(1.0, lexical_cast<test_t>('1'), (std::numeric_limits<test_t>::epsilon()));
|
||||
BOOST_CHECK_EQUAL(0.0, lexical_cast<test_t>('0'));
|
||||
|
||||
unsigned char const uc_one = '1';
|
||||
unsigned char const uc_zero ='0';
|
||||
BOOST_CHECK_CLOSE_FRACTION(1.0, lexical_cast<test_t>(uc_one), (std::numeric_limits<test_t>::epsilon()));
|
||||
BOOST_CHECK_EQUAL(0.0, lexical_cast<test_t>(uc_zero));
|
||||
|
||||
signed char const sc_one = '1';
|
||||
signed char const sc_zero ='0';
|
||||
BOOST_CHECK_CLOSE_FRACTION(1.0, lexical_cast<test_t>(sc_one), (std::numeric_limits<test_t>::epsilon()));
|
||||
BOOST_CHECK_EQUAL(0.0, lexical_cast<test_t>(sc_zero));
|
||||
|
||||
BOOST_CHECK_CLOSE_FRACTION(1e34L, lexical_cast<test_t>( "10000000000000000000000000000000000"), (std::numeric_limits<test_t>::epsilon()) );
|
||||
|
||||
// VC failes the next test
|
||||
// BOOST_CHECK_CLOSE_FRACTION(1e-35L, lexical_cast<test_t>("0.00000000000000000000000000000000001"), (std::numeric_limits<test_t>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(
|
||||
0.1111111111111111111111111111111111111111111111111111111111111111111111111L
|
||||
, lexical_cast<test_t>("0.1111111111111111111111111111111111111111111111111111111111111111111111111")
|
||||
, (std::numeric_limits<test_t>::epsilon()) );
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(1,test_t);
|
||||
BOOST_CHECK_EQUAL(0,lexical_cast<test_t>("0"));
|
||||
CHECK_CLOSE_ABS_DIFF(-1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(1.0, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(0.0, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-1.0,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(1e1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(0e1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-1e1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(1.0e1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(0.0e1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-1.0e1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(1e-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(0e-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-1e-1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(1.0e-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(0.0e-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-1.0e-1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(1E1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(0E1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-1E1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(1.0E1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(0.0E1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-1.0E1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(1E-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(0E-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-1E-1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(1.0E-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(0.0E-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-1.0E-1, test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(.0E-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(.0E-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-.0E-1, test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(10.0, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(00.0, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-10.0,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(10e1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(00e1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-10e1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(10.0e1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(00.0e1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-10.0e1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(10e-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(00e-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-10e-1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(10.0e-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(00.0e-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-10.0e-1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(10E1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(00E1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-10E1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(10.0E1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(00.0E1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-10.0E1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(10E-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(00E-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-10E-1,test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(10.0E-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(00.0E-1, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-10.0E-1, test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(-10101.0E-011, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(-10101093, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(10101093, test_t);
|
||||
|
||||
CHECK_CLOSE_ABS_DIFF(-.34, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(.34, test_t);
|
||||
CHECK_CLOSE_ABS_DIFF(.34e10, test_t);
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("-1.e"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("-1.E"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("1.e"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("1.E"), bad_lexical_cast);
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("1.0e"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("1.0E"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("10E"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("10e"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("1.0e-"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("1.0E-"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("10E-"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("10e-"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("e1"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("e-1"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("e-"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>(".e"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>(".11111111111111111111111111111111111111111111111111111111111111111111ee"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>(".11111111111111111111111111111111111111111111111111111111111111111111e-"), bad_lexical_cast);
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("-B"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("0xB"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("0x0"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("--1.0"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("1.0e--1"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("1.0.0"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("1e1e1"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("1.0e-1e-1"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>(" 1.0"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("1.0 "), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>(""), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("-"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>('\0'), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>('-'), bad_lexical_cast);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void test_float_typess_for_overflows()
|
||||
{
|
||||
typedef T test_t;
|
||||
test_t minvalue = (std::numeric_limits<test_t>::min)();
|
||||
std::string s_min_value = lexical_cast<std::string>(minvalue);
|
||||
BOOST_CHECK_CLOSE_FRACTION(minvalue, lexical_cast<test_t>(minvalue), (std::numeric_limits<test_t>::epsilon()));
|
||||
BOOST_CHECK_CLOSE_FRACTION(minvalue, lexical_cast<test_t>(s_min_value), (std::numeric_limits<test_t>::epsilon()));
|
||||
|
||||
test_t maxvalue = (std::numeric_limits<test_t>::max)();
|
||||
std::string s_max_value = lexical_cast<std::string>(maxvalue);
|
||||
BOOST_CHECK_CLOSE_FRACTION(maxvalue, lexical_cast<test_t>(maxvalue), (std::numeric_limits<test_t>::epsilon()));
|
||||
BOOST_CHECK_CLOSE_FRACTION(maxvalue, lexical_cast<test_t>(s_max_value), (std::numeric_limits<test_t>::epsilon()));
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>(s_max_value+"1"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>(s_max_value+"9"), bad_lexical_cast);
|
||||
|
||||
// VC9 can fail the fllowing tests on floats and doubles when using stingstream...
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("1"+s_max_value), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>("9"+s_max_value), bad_lexical_cast);
|
||||
|
||||
if ( is_same<test_t,float>::value )
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>( (std::numeric_limits<double>::max)() ), bad_lexical_cast);
|
||||
BOOST_CHECK(
|
||||
(std::numeric_limits<double>::min)() - std::numeric_limits<test_t>::epsilon()
|
||||
<= lexical_cast<test_t>( (std::numeric_limits<double>::min)() )
|
||||
&& lexical_cast<test_t>( (std::numeric_limits<double>::min)() )
|
||||
<= (std::numeric_limits<double>::min)() + std::numeric_limits<test_t>::epsilon()
|
||||
);
|
||||
}
|
||||
|
||||
if ( sizeof(test_t) < sizeof(long double) )
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<test_t>( (std::numeric_limits<long double>::max)() ), bad_lexical_cast);
|
||||
BOOST_CHECK(
|
||||
(std::numeric_limits<long double>::min)() - std::numeric_limits<test_t>::epsilon()
|
||||
<= lexical_cast<test_t>( (std::numeric_limits<long double>::min)() )
|
||||
&& lexical_cast<test_t>( (std::numeric_limits<long double>::min)() )
|
||||
<= (std::numeric_limits<long double>::min)() + std::numeric_limits<test_t>::epsilon()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#undef CHECK_CLOSE_ABS_DIFF
|
||||
|
||||
#define TEST_TO_FROM_CAST_AROUND_TYPED(VAL,STRING_TYPE) \
|
||||
test_value = VAL + std::numeric_limits<test_t>::epsilon() * i ; \
|
||||
converted_val = lexical_cast<test_t>( lexical_cast<STRING_TYPE>(test_value) ); \
|
||||
BOOST_CHECK_CLOSE_FRACTION( \
|
||||
test_value, \
|
||||
converted_val, \
|
||||
std::numeric_limits<test_t>::epsilon() \
|
||||
);
|
||||
|
||||
/*
|
||||
* For interval [ from_mult*epsilon+VAL, to_mult*epsilon+VAL ], converts float type
|
||||
* numbers to string[wstring] and then back to float type, then compares initial
|
||||
* values and generated.
|
||||
* Step is epsilon
|
||||
*/
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
# define TEST_TO_FROM_CAST_AROUND(VAL) \
|
||||
for(i=from_mult; i<=to_mult; ++i) { \
|
||||
TEST_TO_FROM_CAST_AROUND_TYPED(VAL, std::string) \
|
||||
TEST_TO_FROM_CAST_AROUND_TYPED(VAL, std::wstring) \
|
||||
}
|
||||
#else
|
||||
# define TEST_TO_FROM_CAST_AROUND(VAL) \
|
||||
for(i=from_mult; i<=to_mult; ++i) { \
|
||||
TEST_TO_FROM_CAST_AROUND_TYPED(VAL, std::string) \
|
||||
}
|
||||
#endif
|
||||
|
||||
template <class TestType>
|
||||
void test_converion_from_to_float_types()
|
||||
{
|
||||
typedef TestType test_t;
|
||||
test_t test_value;
|
||||
test_t converted_val;
|
||||
|
||||
int i;
|
||||
int from_mult = -50;
|
||||
int to_mult = 50;
|
||||
|
||||
TEST_TO_FROM_CAST_AROUND( 0.0 );
|
||||
|
||||
long double val1;
|
||||
for(val1 = 1.0e-10L; val1 < 1e11; val1*=10 )
|
||||
TEST_TO_FROM_CAST_AROUND( val1 );
|
||||
|
||||
long double val2;
|
||||
for(val2 = -1.0e-10L; val2 > -1e11; val2*=10 )
|
||||
TEST_TO_FROM_CAST_AROUND( val2 );
|
||||
|
||||
from_mult = -100;
|
||||
to_mult = 0;
|
||||
TEST_TO_FROM_CAST_AROUND( (std::numeric_limits<test_t>::max)() );
|
||||
|
||||
from_mult = 0;
|
||||
to_mult = 100;
|
||||
TEST_TO_FROM_CAST_AROUND( (std::numeric_limits<test_t>::min)() );
|
||||
}
|
||||
|
||||
#undef TEST_TO_FROM_CAST_AROUND
|
||||
#undef TEST_TO_FROM_CAST_AROUND_TYPED
|
||||
|
||||
|
||||
template<class T, class CharT>
|
||||
void test_conversion_from_float_to_char(CharT zero)
|
||||
{
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(0)) == zero + 0);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(1)) == zero + 1);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(2)) == zero + 2);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(3)) == zero + 3);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(4)) == zero + 4);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(5)) == zero + 5);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(6)) == zero + 6);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(7)) == zero + 7);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(8)) == zero + 8);
|
||||
BOOST_CHECK(lexical_cast<CharT>(static_cast<T>(9)) == zero + 9);
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<CharT>(static_cast<T>(10)), bad_lexical_cast);
|
||||
|
||||
T t = (std::numeric_limits<T>::max)();
|
||||
BOOST_CHECK_THROW(lexical_cast<CharT>(t), bad_lexical_cast);
|
||||
}
|
||||
|
||||
template<class T, class CharT>
|
||||
void test_conversion_from_char_to_float(CharT zero)
|
||||
{
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( static_cast<CharT>(zero + 0)), static_cast<T>(0), (std::numeric_limits<T>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( static_cast<CharT>(zero + 1)), static_cast<T>(1), (std::numeric_limits<T>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( static_cast<CharT>(zero + 2)), static_cast<T>(2), (std::numeric_limits<T>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( static_cast<CharT>(zero + 3)), static_cast<T>(3), (std::numeric_limits<T>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( static_cast<CharT>(zero + 4)), static_cast<T>(4), (std::numeric_limits<T>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( static_cast<CharT>(zero + 5)), static_cast<T>(5), (std::numeric_limits<T>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( static_cast<CharT>(zero + 6)), static_cast<T>(6), (std::numeric_limits<T>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( static_cast<CharT>(zero + 7)), static_cast<T>(7), (std::numeric_limits<T>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( static_cast<CharT>(zero + 8)), static_cast<T>(8), (std::numeric_limits<T>::epsilon()) );
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>( static_cast<CharT>(zero + 9)), static_cast<T>(9), (std::numeric_limits<T>::epsilon()) );
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( static_cast<CharT>(zero + 10)), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>( static_cast<CharT>(zero - 1)), bad_lexical_cast);
|
||||
}
|
||||
|
||||
struct restore_oldloc
|
||||
{
|
||||
std::locale oldloc;
|
||||
~restore_oldloc() { std::locale::global(oldloc); }
|
||||
};
|
||||
|
||||
template<class T>
|
||||
void test_conversion_from_to_float()
|
||||
{ char const zero = '0';
|
||||
signed char const szero = '0';
|
||||
unsigned char const uzero = '0';
|
||||
test_conversion_from_float_to_char<T>(zero);
|
||||
test_conversion_from_char_to_float<T>(zero);
|
||||
test_conversion_from_float_to_char<T>(szero);
|
||||
test_conversion_from_char_to_float<T>(szero);
|
||||
test_conversion_from_float_to_char<T>(uzero);
|
||||
test_conversion_from_char_to_float<T>(uzero);
|
||||
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
wchar_t const wzero = L'0';
|
||||
test_conversion_from_float_to_char<T>(wzero);
|
||||
test_conversion_from_char_to_float<T>(wzero);
|
||||
#endif
|
||||
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>("+1"), 1, std::numeric_limits<T>::epsilon());
|
||||
BOOST_CHECK_CLOSE_FRACTION(lexical_cast<T>("+9"), 9, std::numeric_limits<T>::epsilon());
|
||||
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("++1"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("-+9"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("--1"), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<T>("+-9"), bad_lexical_cast);
|
||||
|
||||
test_converion_to_float_types<T>();
|
||||
test_float_typess_for_overflows<T>();
|
||||
test_converion_from_to_float_types<T>();
|
||||
|
||||
|
||||
typedef std::numpunct<char> numpunct;
|
||||
|
||||
restore_oldloc guard;
|
||||
std::locale const& oldloc = guard.oldloc;
|
||||
|
||||
std::string grouping1 = BOOST_USE_FACET(numpunct, oldloc).grouping();
|
||||
std::string grouping2(grouping1);
|
||||
|
||||
test_conversion_from_to_float_for_locale<T>();
|
||||
|
||||
try
|
||||
{
|
||||
std::locale newloc("");
|
||||
std::locale::global(newloc);
|
||||
|
||||
grouping2 = BOOST_USE_FACET(numpunct, newloc).grouping();
|
||||
}
|
||||
catch(std::exception const& ex)
|
||||
{
|
||||
std::string msg("Failed to set system locale: ");
|
||||
msg += ex.what();
|
||||
BOOST_TEST_MESSAGE(msg);
|
||||
}
|
||||
|
||||
if(grouping1 != grouping2)
|
||||
test_conversion_from_to_float_for_locale<T>();
|
||||
|
||||
if(grouping1.empty() && grouping2.empty())
|
||||
BOOST_TEST_MESSAGE("Formatting with thousands_sep has not been tested");
|
||||
}
|
||||
|
||||
|
||||
void test_conversion_from_to_float()
|
||||
{
|
||||
test_conversion_from_to_float<float>();
|
||||
}
|
||||
void test_conversion_from_to_double()
|
||||
{
|
||||
test_conversion_from_to_float<double>();
|
||||
}
|
||||
void test_conversion_from_to_long_double()
|
||||
{
|
||||
test_conversion_from_to_float<long double>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Executable
+180
@@ -0,0 +1,180 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2011.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
|
||||
#include <boost/math/special_functions/sign.hpp>
|
||||
#include <boost/math/special_functions/fpclassify.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/floating_point_comparison.hpp>
|
||||
|
||||
#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
|
||||
#define BOOST_LCAST_NO_WCHAR_T
|
||||
#endif
|
||||
|
||||
using namespace boost;
|
||||
|
||||
template <class T>
|
||||
bool is_pos_inf(T value)
|
||||
{
|
||||
return (boost::math::isinf)(value) && !(boost::math::signbit)(value);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool is_neg_inf(T value)
|
||||
{
|
||||
return (boost::math::isinf)(value) && (boost::math::signbit)(value);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool is_pos_nan(T value)
|
||||
{
|
||||
return (boost::math::isnan)(value) && !(boost::math::signbit)(value);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool is_neg_nan(T value)
|
||||
{
|
||||
/* There is some strange behaviour on Itanium platform with -nan nuber for long double.
|
||||
* It is a IA64 feature, or it is a boost::math feature, not a lexical_cast bug */
|
||||
#if defined(__ia64__) || defined(_M_IA64)
|
||||
return (boost::math::isnan)(value)
|
||||
&& ( boost::is_same<T, long double >::value || (boost::math::signbit)(value) );
|
||||
#else
|
||||
return (boost::math::isnan)(value) && (boost::math::signbit)(value);
|
||||
#endif
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void test_inf_nan_templated()
|
||||
{
|
||||
typedef T test_t;
|
||||
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>("inf") ) );
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>("INF") ) );
|
||||
|
||||
BOOST_CHECK( is_neg_inf( lexical_cast<test_t>("-inf") ) );
|
||||
BOOST_CHECK( is_neg_inf( lexical_cast<test_t>("-INF") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>("+inf") ) );
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>("+INF") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>("infinity") ) );
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>("INFINITY") ) );
|
||||
|
||||
BOOST_CHECK( is_neg_inf( lexical_cast<test_t>("-infinity") ) );
|
||||
BOOST_CHECK( is_neg_inf( lexical_cast<test_t>("-INFINITY") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>("+infinity") ) );
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>("+INFINITY") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("nan") ) );
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("NAN") ) );
|
||||
|
||||
BOOST_CHECK( is_neg_nan( lexical_cast<test_t>("-nan") ) );
|
||||
BOOST_CHECK( is_neg_nan( lexical_cast<test_t>("-NAN") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("+nan") ) );
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("+NAN") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("nan()") ) );
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>("NAN(some string)") ) );
|
||||
BOOST_CHECK_THROW( lexical_cast<test_t>("NAN(some string"), bad_lexical_cast );
|
||||
|
||||
BOOST_CHECK(lexical_cast<std::string>( (boost::math::changesign)(std::numeric_limits<test_t >::infinity()))
|
||||
== "-inf" );
|
||||
BOOST_CHECK(lexical_cast<std::string>( std::numeric_limits<test_t >::infinity()) == "inf" );
|
||||
BOOST_CHECK(lexical_cast<std::string>( std::numeric_limits<test_t >::quiet_NaN()) == "nan" );
|
||||
#if !defined(__ia64__) && !defined(_M_IA64)
|
||||
BOOST_CHECK(lexical_cast<std::string>(
|
||||
(boost::math::changesign)(std::numeric_limits<test_t >::quiet_NaN()))
|
||||
== "-nan" );
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>(L"inf") ) );
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>(L"INF") ) );
|
||||
|
||||
BOOST_CHECK( is_neg_inf( lexical_cast<test_t>(L"-inf") ) );
|
||||
BOOST_CHECK( is_neg_inf( lexical_cast<test_t>(L"-INF") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>(L"+inf") ) );
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>(L"+INF") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>(L"infinity") ) );
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>(L"INFINITY") ) );
|
||||
|
||||
BOOST_CHECK( is_neg_inf( lexical_cast<test_t>(L"-infinity") ) );
|
||||
BOOST_CHECK( is_neg_inf( lexical_cast<test_t>(L"-INFINITY") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>(L"+infinity") ) );
|
||||
BOOST_CHECK( is_pos_inf( lexical_cast<test_t>(L"+INFINITY") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>(L"nan") ) );
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>(L"NAN") ) );
|
||||
|
||||
BOOST_CHECK( is_neg_nan( lexical_cast<test_t>(L"-nan") ) );
|
||||
BOOST_CHECK( is_neg_nan( lexical_cast<test_t>(L"-NAN") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>(L"+nan") ) );
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>(L"+NAN") ) );
|
||||
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>(L"nan()") ) );
|
||||
BOOST_CHECK( is_pos_nan( lexical_cast<test_t>(L"NAN(some string)") ) );
|
||||
BOOST_CHECK_THROW( lexical_cast<test_t>(L"NAN(some string"), bad_lexical_cast );
|
||||
|
||||
BOOST_CHECK(lexical_cast<std::wstring>( (boost::math::changesign)(std::numeric_limits<test_t >::infinity()))
|
||||
== L"-inf" );
|
||||
BOOST_CHECK(lexical_cast<std::wstring>( std::numeric_limits<test_t >::infinity()) == L"inf" );
|
||||
BOOST_CHECK(lexical_cast<std::wstring>( std::numeric_limits<test_t >::quiet_NaN()) == L"nan" );
|
||||
#if !defined(__ia64__) && !defined(_M_IA64)
|
||||
BOOST_CHECK(lexical_cast<std::wstring>(
|
||||
(boost::math::changesign)(std::numeric_limits<test_t >::quiet_NaN()))
|
||||
== L"-nan" );
|
||||
#endif
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_inf_nan_float()
|
||||
{
|
||||
test_inf_nan_templated<float >();
|
||||
}
|
||||
|
||||
void test_inf_nan_double()
|
||||
{
|
||||
test_inf_nan_templated<double >();
|
||||
}
|
||||
|
||||
void test_inf_nan_long_double()
|
||||
{
|
||||
test_inf_nan_templated<long double >();
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast inf anf nan parsing unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_inf_nan_float));
|
||||
suite->add(BOOST_TEST_CASE(&test_inf_nan_double));
|
||||
suite->add(BOOST_TEST_CASE(&test_inf_nan_long_double));
|
||||
|
||||
return suite;
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Alexander Nasonov, 2006.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
//
|
||||
// Test round-tripping conversion FPT -> string -> FPT,
|
||||
// where FPT is Floating Point Type.
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
void test_round_conversion_float();
|
||||
void test_round_conversion_double();
|
||||
void test_round_conversion_long_double();
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_round_conversion_float));
|
||||
suite->add(BOOST_TEST_CASE(&test_round_conversion_double));
|
||||
suite->add(BOOST_TEST_CASE(&test_round_conversion_long_double));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void test_round_conversion()
|
||||
{
|
||||
T epsilon = std::numeric_limits<T>::epsilon();
|
||||
std::string const epsilon_s = boost::lexical_cast<std::string>(epsilon);
|
||||
BOOST_CHECK(epsilon == lexical_cast<T>(epsilon_s));
|
||||
|
||||
T max_ = (std::numeric_limits<T>::max)();
|
||||
std::string const max_s = boost::lexical_cast<std::string>(max_);
|
||||
BOOST_CHECK(max_ == lexical_cast<T>(max_s));
|
||||
|
||||
T min_ = (std::numeric_limits<T>::min)();
|
||||
std::string const min_s = boost::lexical_cast<std::string>(min_);
|
||||
BOOST_CHECK(min_ == lexical_cast<T>(min_s));
|
||||
|
||||
T max_div137 = max_ / 137;
|
||||
std::string max_div137_s = boost::lexical_cast<std::string>(max_div137);
|
||||
BOOST_CHECK(max_div137 == lexical_cast<T>(max_div137_s));
|
||||
|
||||
T epsilon_mult137 = epsilon * 137;
|
||||
std::string epsilon_mult137_s(lexical_cast<std::string>(epsilon_mult137));
|
||||
BOOST_CHECK(epsilon_mult137 == lexical_cast<T>(epsilon_mult137_s));
|
||||
|
||||
}
|
||||
|
||||
// See bug http://tinyurl.com/vhpvo
|
||||
template<class T>
|
||||
void test_msvc_magic_values()
|
||||
{
|
||||
T magic_msvc = 0.00010000433948393407;
|
||||
std::string magic_msvc_s = boost::lexical_cast<std::string>(magic_msvc);
|
||||
BOOST_CHECK(magic_msvc == lexical_cast<T>(magic_msvc_s));
|
||||
}
|
||||
|
||||
void test_round_conversion_float()
|
||||
{
|
||||
test_round_conversion<float>();
|
||||
}
|
||||
|
||||
void test_round_conversion_double()
|
||||
{
|
||||
test_round_conversion<double>();
|
||||
test_msvc_magic_values<double>();
|
||||
}
|
||||
|
||||
void test_round_conversion_long_double()
|
||||
{
|
||||
test_round_conversion<long double>();
|
||||
test_msvc_magic_values<long double>();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Alexander Nasonov, 2007.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
//
|
||||
// Test that Source can be non-copyable.
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/noncopyable.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
void test_noncopyable();
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_noncopyable));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
class Noncopyable : private boost::noncopyable
|
||||
{
|
||||
public:
|
||||
Noncopyable() {}
|
||||
};
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &out, const Noncopyable&)
|
||||
{
|
||||
return out << "Noncopyable";
|
||||
}
|
||||
|
||||
void test_noncopyable()
|
||||
{
|
||||
Noncopyable x;
|
||||
BOOST_CHECK(boost::lexical_cast<std::string>(x) == "Noncopyable");
|
||||
}
|
||||
|
||||
Executable
+96
@@ -0,0 +1,96 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2012.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
#if defined(BOOST_NO_STRINGSTREAM)
|
||||
typedef std::strstream ss_t;
|
||||
#else
|
||||
typedef std::stringstream ss_t;
|
||||
#endif
|
||||
|
||||
void test_void_pointers_conversions()
|
||||
{
|
||||
void *p_to_null = NULL;
|
||||
const void *cp_to_data = "Some data";
|
||||
char nonconst_data[5];
|
||||
void *p_to_data = nonconst_data;
|
||||
ss_t ss;
|
||||
|
||||
ss << p_to_null;
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(p_to_null), ss.str());
|
||||
ss.str(std::string());
|
||||
|
||||
ss << cp_to_data;
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(cp_to_data), ss.str());
|
||||
ss.str(std::string());
|
||||
|
||||
ss << p_to_data;
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(p_to_data), ss.str());
|
||||
ss.str(std::string());
|
||||
}
|
||||
|
||||
struct incomplete_type;
|
||||
|
||||
void test_incomplete_type_pointers_conversions()
|
||||
{
|
||||
incomplete_type *p_to_null = NULL;
|
||||
const incomplete_type *cp_to_data = NULL;
|
||||
char nonconst_data[5];
|
||||
incomplete_type *p_to_data = reinterpret_cast<incomplete_type*>(nonconst_data);
|
||||
ss_t ss;
|
||||
|
||||
ss << p_to_null;
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(p_to_null), ss.str());
|
||||
ss.str(std::string());
|
||||
|
||||
ss << cp_to_data;
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(cp_to_data), ss.str());
|
||||
ss.str(std::string());
|
||||
|
||||
ss << p_to_data;
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(p_to_data), ss.str());
|
||||
ss.str(std::string());
|
||||
}
|
||||
|
||||
struct ble;
|
||||
typedef struct ble *meh;
|
||||
std::ostream& operator <<(std::ostream &o, meh) {
|
||||
o << "yay";
|
||||
return o;
|
||||
}
|
||||
|
||||
void test_inomplete_type_with_overloaded_ostream_op() {
|
||||
meh heh = NULL;
|
||||
ss_t ss;
|
||||
ss << heh;
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(heh), ss.str());
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast pinters test");
|
||||
suite->add(BOOST_TEST_CASE(&test_void_pointers_conversions));
|
||||
suite->add(BOOST_TEST_CASE(&test_incomplete_type_pointers_conversions));
|
||||
suite->add(BOOST_TEST_CASE(&test_inomplete_type_with_overloaded_ostream_op));
|
||||
return suite;
|
||||
}
|
||||
Executable
+25
@@ -0,0 +1,25 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2011.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
#ifdef BOOST_MSVC
|
||||
BOOST_STATIC_ASSERT((boost::is_same<wchar_t, unsigned short>::value));
|
||||
#endif
|
||||
|
||||
return ::boost::lexical_cast<int>(L"1000") == 1000;
|
||||
}
|
||||
|
||||
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2011.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
using namespace boost;
|
||||
|
||||
void test_typedefed_wchar_t_runtime()
|
||||
{
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
#ifdef BOOST_MSVC
|
||||
BOOST_STATIC_ASSERT((boost::is_same<wchar_t, unsigned short>::value));
|
||||
|
||||
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<int>(L'A'), 65);
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<int>(L'B'), 66);
|
||||
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<wchar_t>(L"65"), 65);
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<wchar_t>(L"66"), 66);
|
||||
#endif
|
||||
#endif
|
||||
BOOST_CHECK(1);
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast typedefed wchar_t runtime test");
|
||||
suite->add(BOOST_TEST_CASE(&test_typedefed_wchar_t_runtime));
|
||||
|
||||
return suite;
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Alexander Nasonov, 2007.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
//
|
||||
// This tests now must pass on vc8, because lexical_cast
|
||||
// implementation has changed and it does not use stringstream for casts
|
||||
// to integral types
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
// See also test_conversion_from_string_to_integral(CharT)
|
||||
// in libs/conversion/lexical_cast_test.cpp
|
||||
template<class T, class CharT>
|
||||
void test_too_long_number(CharT zero)
|
||||
{
|
||||
typedef std::numeric_limits<T> limits;
|
||||
|
||||
std::basic_string<CharT> s;
|
||||
|
||||
std::basic_ostringstream<CharT> o;
|
||||
o << (limits::max)() << zero;
|
||||
s = o.str();
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s), bad_lexical_cast);
|
||||
s[s.size()-1] += 9; // '0' -> '9'
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s), bad_lexical_cast);
|
||||
|
||||
if(limits::is_signed)
|
||||
{
|
||||
std::basic_ostringstream<CharT> o;
|
||||
o << (limits::min)() << zero;
|
||||
s = o.str();
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s), bad_lexical_cast);
|
||||
s[s.size()-1] += 9; // '0' -> '9'
|
||||
BOOST_CHECK_THROW(lexical_cast<T>(s), bad_lexical_cast);
|
||||
}
|
||||
}
|
||||
|
||||
void test_vc8_bug()
|
||||
{
|
||||
test_too_long_number<boost::intmax_t>('0');
|
||||
test_too_long_number<boost::uintmax_t>('0');
|
||||
#if !defined(BOOST_LCAST_NO_WCHAR_T)
|
||||
test_too_long_number<boost::intmax_t>(L'0');
|
||||
test_too_long_number<boost::uintmax_t>(L'0');
|
||||
#endif
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast vc8 bug unit test");
|
||||
suite->add(BOOST_TEST_CASE(test_vc8_bug));
|
||||
return suite;
|
||||
}
|
||||
Executable
+56
@@ -0,0 +1,56 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2011.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 193 383 488 981 1418 1419)
|
||||
#elif defined(BOOST_MSVC)
|
||||
#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
|
||||
#endif
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/floating_point_comparison.hpp>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
void test_char_types_conversions()
|
||||
{
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
const char c_arr[] = "Test array of chars";
|
||||
const unsigned char uc_arr[] = "Test array of chars";
|
||||
const signed char sc_arr[] = "Test array of chars";
|
||||
const wchar_t wc_arr[] =L"Test array of chars";
|
||||
|
||||
// Following tests depend on realization of std::locale
|
||||
// and pass for popular compilers and STL realizations
|
||||
BOOST_CHECK(boost::lexical_cast<wchar_t>(c_arr[0]) == wc_arr[0]);
|
||||
BOOST_CHECK(boost::lexical_cast<std::wstring>(c_arr) == std::wstring(wc_arr));
|
||||
|
||||
BOOST_CHECK(boost::lexical_cast<std::wstring>(sc_arr) == std::wstring(wc_arr) );
|
||||
BOOST_CHECK(boost::lexical_cast<std::wstring>(uc_arr) == std::wstring(wc_arr) );
|
||||
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<wchar_t>(uc_arr[0]), wc_arr[0]);
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<wchar_t>(sc_arr[0]), wc_arr[0]);
|
||||
#endif
|
||||
BOOST_CHECK(1);
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast char<->wchar_t unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_char_types_conversions));
|
||||
|
||||
return suite;
|
||||
}
|
||||
Reference in New Issue
Block a user