Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r75937 - in trunk: boost libs/conversion/doc libs/conversion/test
From: antoshkka_at_[hidden]
Date: 2011-12-14 11:36:38


Author: apolukhin
Date: 2011-12-14 11:36:34 EST (Wed, 14 Dec 2011)
New Revision: 75937
URL: http://svn.boost.org/trac/boost/changeset/75937

Log:
Fixes #6186 (treat conversions to/from single wchar_t character as conversions to/from unsigned short. Test added, documentation updated)
Added:
   trunk/libs/conversion/test/lexical_cast_typedefed_wchar_test_runtime.cpp (contents, props changed)
Text files modified:
   trunk/boost/lexical_cast.hpp | 28 +++++++++++++---------------
   trunk/libs/conversion/doc/lexical_cast.qbk | 12 ++++++++++++
   trunk/libs/conversion/test/Jamfile.v2 | 1 +
   3 files changed, 26 insertions(+), 15 deletions(-)

Modified: trunk/boost/lexical_cast.hpp
==============================================================================
--- trunk/boost/lexical_cast.hpp (original)
+++ trunk/boost/lexical_cast.hpp 2011-12-14 11:36:34 EST (Wed, 14 Dec 2011)
@@ -17,8 +17,8 @@
 // enhanced with contributions from Terje Slettebo,
 // with additional fixes and suggestions from Gennaro Prota,
 // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
-// Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann
-// and other Boosters
+// Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann,
+// Cheng Yang and other Boosters
 // when: November 2000, March 2003, June 2005, June 2006, March 2011
 
 #include <climits>
@@ -1093,8 +1093,6 @@
 
     namespace detail // optimized stream wrapper
     {
- struct type_used_as_workaround_for_typedefed_wchar_ts{};
-
         // String representation of Source has an upper limit.
         template< class CharT // a result of widest_char transformation
                 , class Traits // usually char_traits<CharT>
@@ -1356,7 +1354,6 @@
 
 /************************************ HELPER FUNCTIONS FOR OPERATORS >> ( ... ) ********************************/
         private:
- bool shr_unsigned(type_used_as_workaround_for_typedefed_wchar_ts /*var*/) {return true;}
 
             template <typename Type>
             bool shr_unsigned(Type& output)
@@ -1484,15 +1481,8 @@
             }
 
 /************************************ OPERATORS >> ( ... ) ********************************/
-
- typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
- ::boost::is_same<CharT, unsigned short>::value
- , type_used_as_workaround_for_typedefed_wchar_ts
- , unsigned short
- >::type ushort_ambiguity_workaround;
-
             public:
- bool operator>>(ushort_ambiguity_workaround& output){ return shr_unsigned(output); }
+ bool operator>>(unsigned short& output) { return shr_unsigned(output); }
             bool operator>>(unsigned int& output) { return shr_unsigned(output); }
             bool operator>>(unsigned long int& output) { return shr_unsigned(output); }
             bool operator>>(short& output) { return shr_signed(output); }
@@ -1504,11 +1494,19 @@
 #elif defined(BOOST_HAS_MS_INT64)
             bool operator>>(unsigned __int64& output) { return shr_unsigned(output); }
             bool operator>>(__int64& output) { return shr_signed(output); }
-
 #endif
- bool operator>>(CharT& output) { return shr_xchar(output); }
+ bool operator>>(char& output) { return shr_xchar(output); }
             bool operator>>(unsigned char& output) { return shr_xchar(output); }
             bool operator>>(signed char& output) { return shr_xchar(output); }
+#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
+ bool operator>>(wchar_t& output) { return shr_xchar(output); }
+#endif
+#ifndef BOOST_NO_CHAR16_T
+ bool operator>>(char16_t& output) { return shr_xchar(output); }
+#endif
+#ifndef BOOST_NO_CHAR32_T
+ bool operator>>(char32_t& output) { return shr_xchar(output); }
+#endif
 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
             bool operator>>(std::string& str) { str.assign(start, finish); return true; }
 # ifndef BOOST_LCAST_NO_WCHAR_T

Modified: trunk/libs/conversion/doc/lexical_cast.qbk
==============================================================================
--- trunk/libs/conversion/doc/lexical_cast.qbk (original)
+++ trunk/libs/conversion/doc/lexical_cast.qbk 2011-12-14 11:36:34 EST (Wed, 14 Dec 2011)
@@ -159,9 +159,21 @@
 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 :]
+
+ * Added code to work with typedefed wchar_t (compilation flag /Zc:wchar_t- for Visual Studio).
+
 * [*boost 1.48.0 :]
   
     * Added code to work with Inf and NaN on any platform.

Modified: trunk/libs/conversion/test/Jamfile.v2
==============================================================================
--- trunk/libs/conversion/test/Jamfile.v2 (original)
+++ trunk/libs/conversion/test/Jamfile.v2 2011-12-14 11:36:34 EST (Wed, 14 Dec 2011)
@@ -36,5 +36,6 @@
     [ 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 ]
     [ 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 ]
   ;
       

Added: trunk/libs/conversion/test/lexical_cast_typedefed_wchar_test_runtime.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/conversion/test/lexical_cast_typedefed_wchar_test_runtime.cpp 2011-12-14 11:36:34 EST (Wed, 14 Dec 2011)
@@ -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;
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk