Boost logo

Boost-Commit :

From: Alexander.Nasonov_at_[hidden]
Date: 2008-04-16 17:13:25


Author: nasonov
Date: 2008-04-16 17:13:25 EDT (Wed, 16 Apr 2008)
New Revision: 44474
URL: http://svn.boost.org/trac/boost/changeset/44474

Log:
Use make_unsigned and get rid of gcc warnings when -DBOOST_LEXICAL_CAST_ASSUME_C_LOCALE
Text files modified:
   trunk/boost/lexical_cast.hpp | 58 ++++++++++++---------------------------
   1 files changed, 18 insertions(+), 40 deletions(-)

Modified: trunk/boost/lexical_cast.hpp
==============================================================================
--- trunk/boost/lexical_cast.hpp (original)
+++ trunk/boost/lexical_cast.hpp 2008-04-16 17:13:25 EDT (Wed, 16 Apr 2008)
@@ -25,6 +25,7 @@
 #include <boost/mpl/if.hpp>
 #include <boost/throw_exception.hpp>
 #include <boost/type_traits/is_pointer.hpp>
+#include <boost/type_traits/make_unsigned.hpp>
 #include <boost/call_traits.hpp>
 #include <boost/static_assert.hpp>
 #include <boost/detail/lcast_precision.hpp>
@@ -461,36 +462,16 @@
 // C4146: unary minus operator applied to unsigned type, result still unsigned
 # pragma warning( disable : 4146 )
 #endif
-
- inline unsigned int lcast_to_unsigned(int value)
- {
- unsigned int uval = value;
- return value < 0 ? -uval : uval;
- }
-
- inline unsigned long lcast_to_unsigned(long value)
- {
- unsigned long uval = value;
- return value < 0 ? -uval : uval;
- }
-
-#if defined(BOOST_HAS_LONG_LONG)
- inline boost::ulong_long_type lcast_to_unsigned(boost::long_long_type v)
- {
- boost::ulong_long_type uval = v;
- return v < 0 ? -uval : uval;
- }
-#elif defined(BOOST_HAS_MS_INT64)
- inline unsigned __int64 lcast_to_unsigned(__int64 value)
+ template<class T>
+ inline
+ BOOST_DEDUCED_TYPENAME make_unsigned<T>::type lcast_to_unsigned(T value)
         {
- unsigned __int64 uval = value;
- return value < 0 ? -uval : uval;
+ typedef BOOST_DEDUCED_TYPENAME make_unsigned<T>::type result_type;
+ result_type uvalue = static_cast<result_type>(value);
+ return value < 0 ? -uvalue : uvalue;
         }
-#endif
-
 #if (defined _MSC_VER)
-# pragma warning( pop ) // C4146: unary minus operator applied to unsigned type,
- // result still unsigned
+# pragma warning( pop )
 #endif
     }
 
@@ -502,22 +483,14 @@
 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
             BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
 #endif
- CharT thousands_sep = 0;
 
-#ifdef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
- char const* grouping = "";
- std::size_t const grouping_size = 0;
-#else
+#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
             std::locale loc;
             typedef std::numpunct<CharT> numpunct;
             numpunct const& np = BOOST_USE_FACET(numpunct, loc);
             std::string const& grouping = np.grouping();
             std::string::size_type const grouping_size = grouping.size();
-
- if(grouping_size)
- thousands_sep = np.thousands_sep();
-#endif
-
+ CharT thousands_sep = grouping_size ? np.thousands_sep() : 0;
             std::string::size_type group = 0; // current group number
             char last_grp_size = grouping[0] <= 0 ? CHAR_MAX : grouping[0];
             // a) Since grouping is const, grouping[grouping.size()] returns 0.
@@ -526,14 +499,17 @@
 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
             BOOST_STATIC_ASSERT(std::numeric_limits<T>::digits10 < CHAR_MAX);
 #endif
+
+ char left = last_grp_size;
+#endif
+
             typedef typename Traits::int_type int_type;
             CharT const czero = lcast_char_constants<CharT>::zero;
             int_type const zero = Traits::to_int_type(czero);
 
- char left = last_grp_size;
-
             do
             {
+#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
                 if(left == 0)
                 {
                     ++group;
@@ -549,6 +525,8 @@
                 }
 
                 --left;
+#endif
+
                 --finish;
                 int_type const digit = static_cast<int_type>(n % 10U);
                 Traits::assign(*finish, Traits::to_char_type(zero + digit));
@@ -907,7 +885,7 @@
         inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
                 unsigned short n)
         {
- start = lcast_put_unsigned<Traits>(lcast_to_unsigned(n), finish);
+ start = lcast_put_unsigned<Traits>(n, finish);
             return true;
         }
 


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