]> git.lyx.org Git - lyx.git/blobdiff - boost/boost/lexical_cast.hpp
Fix HTML output of \gg and \ll
[lyx.git] / boost / boost / lexical_cast.hpp
index 5a3d4f072a1f8dd6e26397584618d99c97fd03aa..ed2291d85b1c5835e8b0af7e6bf0bd3db354e981 100644 (file)
 //        with additional fixes and suggestions from Gennaro Prota,
 //        Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
 //        Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann,
-//        Cheng Yang, Matthew Bradbury, David W. Birdsall and other Boosters
-// when:  November 2000, March 2003, June 2005, June 2006, March 2011 - 2012
+//        Cheng Yang, Matthew Bradbury, David W. Birdsall, Pavel Korzh and other Boosters
+// when:  November 2000, March 2003, June 2005, June 2006, March 2011 - 2013
 
 #include <boost/config.hpp>
 #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
 #define BOOST_LCAST_NO_WCHAR_T
 #endif
 
-#if (defined(__MINGW32__) || defined(__MINGW64__)) && (__GNUC__ == 4) \
- && ((__GNUC_MINOR__ == 4) || (__GNUC_MINOR__ == 5)) && defined(__STRICT_ANSI__) \
- && !defined(BOOST_LCAST_NO_WCHAR_T)
-
-// workaround for a mingw bug
-// http://sourceforge.net/tracker/index.php?func=detail&aid=2373234&group_id=2435&atid=102435
-#include <_mingw.h>
-#if (__GNUC_MINOR__ == 4)
-extern "C" {
-_CRTIMP int __cdecl swprintf(wchar_t * __restrict__ , const wchar_t * __restrict__ , ...);
-_CRTIMP int __cdecl vswprintf(wchar_t * __restrict__ , const wchar_t * __restrict__ , ...);
-}
-#endif
-#if (__GNUC_MINOR__ == 5)
-extern "C" {
-_CRTIMP int __cdecl swprintf(wchar_t * __restrict__ , const wchar_t * __restrict__ , ...);
-_CRTIMP int __cdecl vswprintf(wchar_t * __restrict__ , const wchar_t * __restrict__ , va_list);
-}
-#endif
-#endif
-
 #include <climits>
 #include <cstddef>
-#include <istream>
 #include <string>
 #include <cstring>
 #include <cstdio>
 #include <typeinfo>
 #include <exception>
-#include <cmath>
 #include <boost/limits.hpp>
 #include <boost/mpl/if.hpp>
 #include <boost/throw_exception.hpp>
-#include <boost/type_traits/is_pointer.hpp>
-#include <boost/type_traits/is_integral.hpp>
-#include <boost/type_traits/is_arithmetic.hpp>
-#include <boost/type_traits/remove_pointer.hpp>
-#include <boost/numeric/conversion/cast.hpp>
 #include <boost/type_traits/ice.hpp>
-#include <boost/type_traits/make_unsigned.hpp>
-#include <boost/type_traits/is_signed.hpp>
-#include <boost/math/special_functions/sign.hpp>
-#include <boost/math/special_functions/fpclassify.hpp>
+#include <boost/type_traits/is_pointer.hpp>
 #include <boost/static_assert.hpp>
 #include <boost/detail/lcast_precision.hpp>
 #include <boost/detail/workaround.hpp>
-#if !defined(__SUNPRO_CC)
-#include <boost/container/container_fwd.hpp>
-#endif // !defined(__SUNPRO_CC)
-#ifndef BOOST_NO_CWCHAR
-#   include <cwchar>
-#endif
+
 
 #ifndef BOOST_NO_STD_LOCALE
 #   include <locale>
 #else
 #   ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
-#       warning "Unable to use <locale> header. boost::lexical_cast will use the 'C' locale."
-#       define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
+        // Getting error at this point means, that your STL library is old/lame/misconfigured.
+        // If nothing can be done with STL library, define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE,
+        // but beware: lexical_cast will understand only 'C' locale delimeters and thousands
+        // separators.
+#       error "Unable to use <locale> header. Define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE to force "
+#       error "boost::lexical_cast to use only 'C' locale during conversions."
 #   endif
 #endif
 
@@ -104,7 +72,7 @@ _CRTIMP int __cdecl vswprintf(wchar_t * __restrict__ , const wchar_t * __restric
 namespace boost
 {
     // exception used to indicate runtime lexical_cast failure
-    class bad_lexical_cast :
+    class BOOST_SYMBOL_VISIBLE bad_lexical_cast :
     // workaround MSVC bug with std::bad_cast when _HAS_EXCEPTIONS == 0 
 #if defined(BOOST_MSVC) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS 
         public std::exception 
@@ -119,7 +87,7 @@ namespace boost
 
     {
     public:
-        bad_lexical_cast() :
+        bad_lexical_cast() BOOST_NOEXCEPT :
 #ifndef BOOST_NO_TYPEID
           source(&typeid(void)), target(&typeid(void))
 #else
@@ -130,7 +98,7 @@ namespace boost
 
         bad_lexical_cast(
             const std::type_info &source_type_arg,
-            const std::type_info &target_type_arg) :
+            const std::type_info &target_type_arg) BOOST_NOEXCEPT :
             source(&source_type_arg), target(&target_type_arg)
         {
         }
@@ -144,213 +112,443 @@ namespace boost
             return *target;
         }
 
+#ifndef BOOST_NO_CXX11_NOEXCEPT
+        virtual const char *what() const noexcept
+#else
         virtual const char *what() const throw()
+#endif
         {
             return "bad lexical cast: "
                    "source type value could not be interpreted as target";
         }
+
+#ifndef BOOST_NO_CXX11_NOEXCEPT
+        virtual ~bad_lexical_cast() BOOST_NOEXCEPT
+#else
         virtual ~bad_lexical_cast() throw()
-        {
-        }
+#endif
+        {}
     private:
         const std::type_info *source;
         const std::type_info *target;
     };
 
-    namespace detail // selectors for choosing stream character type
-    {
-    template<typename Type>
-    struct stream_char
+    namespace detail // widest_char
     {
-        typedef char type;
-    };
+        template <typename TargetChar, typename SourceChar>
+        struct widest_char
+        {
+            typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
+                (sizeof(TargetChar) > sizeof(SourceChar))
+                , TargetChar
+                , SourceChar >::type type;
+        };
+    }
+} // namespace boost
 
-#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
-    template<class CharT, class Traits, class Alloc>
-    struct stream_char< std::basic_string<CharT,Traits,Alloc> >
-    {
-        typedef CharT type;
-    };
+#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__SUNPRO_CC) && !defined(__PGIC__)
 
-#if !defined(__SUNPRO_CC)
-    template<class CharT, class Traits, class Alloc>
-    struct stream_char< ::boost::container::basic_string<CharT,Traits,Alloc> >
-    {
-        typedef CharT type;
-    };
-#endif // !defined(__SUNPRO_CC)
+#include <cmath>
+#include <istream>
+
+#ifndef BOOST_NO_CXX11_HDR_ARRAY
+#include <array>
 #endif
 
-#ifndef BOOST_LCAST_NO_WCHAR_T
-#ifndef BOOST_NO_INTRINSIC_WCHAR_T
-    template<>
-    struct stream_char<wchar_t>
-    {
-        typedef wchar_t type;
-    };
+#include <boost/array.hpp>
+#include <boost/numeric/conversion/cast.hpp>
+#include <boost/type_traits/make_unsigned.hpp>
+#include <boost/type_traits/is_signed.hpp>
+#include <boost/type_traits/is_integral.hpp>
+#include <boost/type_traits/is_arithmetic.hpp>
+#include <boost/type_traits/remove_pointer.hpp>
+#include <boost/type_traits/has_left_shift.hpp>
+#include <boost/type_traits/has_right_shift.hpp>
+#include <boost/math/special_functions/sign.hpp>
+#include <boost/math/special_functions/fpclassify.hpp>
+#include <boost/range/iterator_range_core.hpp>
+#include <boost/container/container_fwd.hpp>
+#include <boost/integer.hpp>
+#ifndef BOOST_NO_CWCHAR
+#   include <cwchar>
 #endif
 
-    template<>
-    struct stream_char<wchar_t *>
-    {
-        typedef wchar_t type;
-    };
+namespace boost {
 
-    template<>
-    struct stream_char<const wchar_t *>
+    namespace detail // is_char_or_wchar<...>
     {
-        typedef wchar_t type;
-    };
+        // returns true, if T is one of the character types
+        template < typename T >
+        struct is_char_or_wchar
+        {
+            typedef boost::type_traits::ice_or<
+                    boost::is_same< T, char >::value,
+                    #ifndef BOOST_LCAST_NO_WCHAR_T
+                        boost::is_same< T, wchar_t >::value,
+                    #endif
+                    #ifndef BOOST_NO_CXX11_CHAR16_T
+                        boost::is_same< T, char16_t >::value,
+                    #endif
+                    #ifndef BOOST_NO_CXX11_CHAR32_T
+                        boost::is_same< T, char32_t >::value,
+                    #endif
+                    boost::is_same< T, unsigned char >::value,
+                    boost::is_same< T, signed char >::value
+            > result_type;
+
+            BOOST_STATIC_CONSTANT(bool, value = (result_type::value) );
+        };
+    }
 
-#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
-    template<>
-    struct stream_char<std::wstring>
+    namespace detail // normalize_single_byte_char<Char>
     {
-        typedef wchar_t type;
-    };
-#endif
-#endif
+        // Converts signed/unsigned char to char
+        template < class Char >
+        struct normalize_single_byte_char 
+        {
+            typedef Char type;
+        };
 
+        template <>
+        struct normalize_single_byte_char< signed char >
+        {
+            typedef char type;
+        };
 
-#ifndef BOOST_NO_CHAR16_T
+        template <>
+        struct normalize_single_byte_char< unsigned char >
+        {
+            typedef char type;
+        };
+    }
 
-    template<>
-    struct stream_char<char16_t>
+    namespace detail // deduce_character_type_later<T>
     {
-        typedef char16_t type;
-    };
+        // Helper type, meaning that stram character for T must be deduced 
+        // at Stage 2 (See deduce_source_char<T> and deduce_target_char<T>)
+        template < class T > struct deduce_character_type_later {};
+    }
 
-    template<>
-    struct stream_char<char16_t *>
+    namespace detail // stream_char_common<T>
     {
-        typedef char16_t type;
-    };
+        // Selectors to choose stream character type (common for Source and Target)
+        // Returns one of char, wchar_t, char16_t, char32_t or deduce_character_type_later<T> types
+        // Executed on Stage 1 (See deduce_source_char<T> and deduce_target_char<T>)
+        template < typename Type >
+        struct stream_char_common: public boost::mpl::if_c<
+            boost::detail::is_char_or_wchar< Type >::value,
+            Type,
+            boost::detail::deduce_character_type_later< Type >
+        > {};
+
+        template < typename Char >
+        struct stream_char_common< Char* >: public boost::mpl::if_c<
+            boost::detail::is_char_or_wchar< Char >::value,
+            Char,
+            boost::detail::deduce_character_type_later< Char* >
+        > {};
+
+        template < typename Char >
+        struct stream_char_common< const Char* >: public boost::mpl::if_c<
+            boost::detail::is_char_or_wchar< Char >::value,
+            Char,
+            boost::detail::deduce_character_type_later< const Char* >
+        > {};
+
+        template < typename Char >
+        struct stream_char_common< boost::iterator_range< Char* > >: public boost::mpl::if_c<
+            boost::detail::is_char_or_wchar< Char >::value,
+            Char,
+            boost::detail::deduce_character_type_later< boost::iterator_range< Char* > >
+        > {};
+    
+        template < typename Char >
+        struct stream_char_common< boost::iterator_range< const Char* > >: public boost::mpl::if_c<
+            boost::detail::is_char_or_wchar< Char >::value,
+            Char,
+            boost::detail::deduce_character_type_later< boost::iterator_range< const Char* > >
+        > {};
+
+        template < class Char, class Traits, class Alloc >
+        struct stream_char_common< std::basic_string< Char, Traits, Alloc > >
+        {
+            typedef Char type;
+        };
 
-    template<>
-    struct stream_char<const char16_t *>
-    {
-        typedef char16_t type;
-    };
+        template < class Char, class Traits, class Alloc >
+        struct stream_char_common< boost::container::basic_string< Char, Traits, Alloc > >
+        {
+            typedef Char type;
+        };
 
+        template < typename Char, std::size_t N >
+        struct stream_char_common< boost::array< Char, N > >: public boost::mpl::if_c<
+            boost::detail::is_char_or_wchar< Char >::value,
+            Char,
+            boost::detail::deduce_character_type_later< boost::array< Char, N > >
+        > {};
+
+        template < typename Char, std::size_t N >
+        struct stream_char_common< boost::array< const Char, N > >: public boost::mpl::if_c<
+            boost::detail::is_char_or_wchar< Char >::value,
+            Char,
+            boost::detail::deduce_character_type_later< boost::array< const Char, N > >
+        > {};
+
+#ifndef BOOST_NO_CXX11_HDR_ARRAY
+        template < typename Char, std::size_t N >
+        struct stream_char_common< std::array<Char, N > >: public boost::mpl::if_c<
+            boost::detail::is_char_or_wchar< Char >::value,
+            Char,
+            boost::detail::deduce_character_type_later< std::array< Char, N > >
+        > {};
+
+        template < typename Char, std::size_t N >
+        struct stream_char_common< std::array< const Char, N > >: public boost::mpl::if_c<
+            boost::detail::is_char_or_wchar< Char >::value,
+            Char,
+            boost::detail::deduce_character_type_later< std::array< const Char, N > >
+        > {};
+#endif
+
+#ifdef BOOST_HAS_INT128
+        template <> struct stream_char_common< boost::int128_type >: public boost::mpl::identity< char > {};
+        template <> struct stream_char_common< boost::uint128_type >: public boost::mpl::identity< char > {};
+#endif
+
+#if !defined(BOOST_LCAST_NO_WCHAR_T) && defined(BOOST_NO_INTRINSIC_WCHAR_T)
+        template <>
+        struct stream_char_common< wchar_t >
+        {
+            typedef char type;
+        };
 #endif
+    }
 
-#ifndef BOOST_NO_CHAR32_T
-
-    template<>
-    struct stream_char<char32_t>
+    namespace detail // deduce_source_char_impl<T>
     {
-        typedef char32_t type;
-    };
+        // If type T is `deduce_character_type_later` type, then tries to deduce
+        // character type using boost::has_left_shift<T> metafunction.
+        // Otherwise supplied type T is a character type, that must be normalized
+        // using normalize_single_byte_char<Char>.
+        // Executed at Stage 2  (See deduce_source_char<T> and deduce_target_char<T>)
+        template < class Char > 
+        struct deduce_source_char_impl
+        { 
+            typedef BOOST_DEDUCED_TYPENAME boost::detail::normalize_single_byte_char< Char >::type type; 
+        };
+        
+        template < class T > 
+        struct deduce_source_char_impl< deduce_character_type_later< T > > 
+        {
+            typedef boost::has_left_shift< std::basic_ostream< char >, T > result_t;
+
+#if defined(BOOST_LCAST_NO_WCHAR_T)
+            BOOST_STATIC_ASSERT_MSG((result_t::value), 
+                "Source type is not std::ostream`able and std::wostream`s are not supported by your STL implementation");
+            typedef char type;
+#else
+            typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
+                result_t::value, char, wchar_t
+            >::type type;
+
+            BOOST_STATIC_ASSERT_MSG((result_t::value || boost::has_left_shift< std::basic_ostream< type >, T >::value), 
+                "Source type is neither std::ostream`able nor std::wostream`able");
+#endif
+        };
+    }
 
-    template<>
-    struct stream_char<char32_t *>
+    namespace detail  // deduce_target_char_impl<T>
     {
-        typedef char32_t type;
-    };
+        // If type T is `deduce_character_type_later` type, then tries to deduce
+        // character type using boost::has_right_shift<T> metafunction.
+        // Otherwise supplied type T is a character type, that must be normalized
+        // using normalize_single_byte_char<Char>.
+        // Executed at Stage 2  (See deduce_source_char<T> and deduce_target_char<T>)
+        template < class Char > 
+        struct deduce_target_char_impl 
+        { 
+            typedef BOOST_DEDUCED_TYPENAME normalize_single_byte_char< Char >::type type; 
+        };
+        
+        template < class T > 
+        struct deduce_target_char_impl< deduce_character_type_later<T> > 
+        { 
+            typedef boost::has_right_shift<std::basic_istream<char>, T > result_t;
+
+#if defined(BOOST_LCAST_NO_WCHAR_T)
+            BOOST_STATIC_ASSERT_MSG((result_t::value), 
+                "Target type is not std::istream`able and std::wistream`s are not supported by your STL implementation");
+            typedef char type;
+#else
+            typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
+                result_t::value, char, wchar_t
+            >::type type;
+            
+            BOOST_STATIC_ASSERT_MSG((result_t::value || boost::has_right_shift<std::basic_istream<wchar_t>, T >::value), 
+                "Target type is neither std::istream`able nor std::wistream`able");
+#endif
+        };
+    } 
 
-    template<>
-    struct stream_char<const char32_t *>
+    namespace detail  // deduce_target_char<T> and deduce_source_char<T>
     {
-        typedef char32_t type;
-    };
+        // We deduce stream character types in two stages.
+        //
+        // Stage 1 is common for Target and Source. At Stage 1 we get 
+        // non normalized character type (may contain unsigned/signed char)
+        // or deduce_character_type_later<T> where T is the original type.
+        // Stage 1 is executed by stream_char_common<T>
+        //
+        // At Stage 2 we normalize character types or try to deduce character 
+        // type using metafunctions. 
+        // Stage 2 is executed by deduce_target_char_impl<T> and 
+        // deduce_source_char_impl<T>
+        //
+        // deduce_target_char<T> and deduce_source_char<T> functions combine 
+        // both stages
 
-#endif
+        template < class T >
+        struct deduce_target_char
+        {
+            typedef BOOST_DEDUCED_TYPENAME stream_char_common< T >::type stage1_type;
+            typedef BOOST_DEDUCED_TYPENAME deduce_target_char_impl< stage1_type >::type stage2_type;
 
-        template<typename TargetChar, typename SourceChar>
-        struct widest_char
+            typedef stage2_type type;
+        };
+
+        template < class T >
+        struct deduce_source_char
         {
-            typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
-                (sizeof(TargetChar) > sizeof(SourceChar))
-                , TargetChar
-                , SourceChar >::type type;
+            typedef BOOST_DEDUCED_TYPENAME stream_char_common< T >::type stage1_type;
+            typedef BOOST_DEDUCED_TYPENAME deduce_source_char_impl< stage1_type >::type stage2_type;
+
+            typedef stage2_type type;
         };
     }
 
     namespace detail // deduce_char_traits template
     {
-#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
-        template<class CharT, class Target, class Source>
+        // We are attempting to get char_traits<> from Source or Tagret
+        // template parameter. Otherwise we'll be using std::char_traits<Char>
+        template < class Char, class Target, class Source >
         struct deduce_char_traits
         {
-            typedef std::char_traits<CharT> type;
+            typedef std::char_traits< Char > type;
         };
 
-        template<class CharT, class Traits, class Alloc, class Source>
-        struct deduce_char_traits< CharT
-                                 , std::basic_string<CharT,Traits,Alloc>
+        template < class Char, class Traits, class Alloc, class Source >
+        struct deduce_char_traits< Char
+                                 , std::basic_string< Char, Traits, Alloc >
                                  , Source
                                  >
         {
             typedef Traits type;
         };
 
-        template<class CharT, class Target, class Traits, class Alloc>
-        struct deduce_char_traits< CharT
+        template < class Char, class Target, class Traits, class Alloc >
+        struct deduce_char_traits< Char
                                  , Target
-                                 , std::basic_string<CharT,Traits,Alloc>
+                                 , std::basic_string< Char, Traits, Alloc >
                                  >
         {
             typedef Traits type;
         };
 
-#if !defined(__SUNPRO_CC)
-        template<class CharT, class Traits, class Alloc, class Source>
-        struct deduce_char_traits< CharT
-                                 , ::boost::container::basic_string<CharT,Traits,Alloc>
+        template < class Char, class Traits, class Alloc, class Source >
+        struct deduce_char_traits< Char
+                                 , boost::container::basic_string< Char, Traits, Alloc >
                                  , Source
                                  >
         {
             typedef Traits type;
         };
 
-        template<class CharT, class Target, class Traits, class Alloc>
-        struct deduce_char_traits< CharT
+        template < class Char, class Target, class Traits, class Alloc >
+        struct deduce_char_traits< Char
                                  , Target
-                                 , ::boost::container::basic_string<CharT,Traits,Alloc>
+                                 , boost::container::basic_string< Char, Traits, Alloc >
                                  >
         {
             typedef Traits type;
         };
 
-        template<class CharT, class Traits, class Alloc1, class Alloc2>
-        struct deduce_char_traits< CharT
-                                 , std::basic_string<CharT,Traits,Alloc1>
-                                 , std::basic_string<CharT,Traits,Alloc2>
+        template < class Char, class Traits, class Alloc1, class Alloc2 >
+        struct deduce_char_traits< Char
+                                 , std::basic_string< Char, Traits, Alloc1 >
+                                 , std::basic_string< Char, Traits, Alloc2 >
                                  >
         {
             typedef Traits type;
         };
 
-        template<class CharT, class Traits, class Alloc1, class Alloc2>
-        struct deduce_char_traits< CharT
-                                 , ::boost::container::basic_string<CharT,Traits,Alloc1>
-                                 , ::boost::container::basic_string<CharT,Traits,Alloc2>
+        template<class Char, class Traits, class Alloc1, class Alloc2>
+        struct deduce_char_traits< Char
+                                 , boost::container::basic_string< Char, Traits, Alloc1 >
+                                 , boost::container::basic_string< Char, Traits, Alloc2 >
                                  >
         {
             typedef Traits type;
         };
 
-        template<class CharT, class Traits, class Alloc1, class Alloc2>
-        struct deduce_char_traits< CharT
-                                 , ::boost::container::basic_string<CharT,Traits,Alloc1>
-                                 , std::basic_string<CharT,Traits,Alloc2>
+        template < class Char, class Traits, class Alloc1, class Alloc2 >
+        struct deduce_char_traits< Char
+                                 , boost::container::basic_string< Char, Traits, Alloc1 >
+                                 , std::basic_string< Char, Traits, Alloc2 >
                                  >
         {
             typedef Traits type;
         };
 
-        template<class CharT, class Traits, class Alloc1, class Alloc2>
-        struct deduce_char_traits< CharT
-                                 , std::basic_string<CharT,Traits,Alloc1>
-                                 , ::boost::container::basic_string<CharT,Traits,Alloc2>
+        template < class Char, class Traits, class Alloc1, class Alloc2 >
+        struct deduce_char_traits< Char
+                                 , std::basic_string< Char, Traits, Alloc1 >
+                                 , boost::container::basic_string< Char, Traits, Alloc2 >
                                  >
         {
             typedef Traits type;
         };
-#endif // !defined(__SUNPRO_CC)
-#endif
     }
 
+    namespace detail // array_to_pointer_decay<T>
+    {
+        template<class T>
+        struct array_to_pointer_decay
+        {
+            typedef T type;
+        };
+
+        template<class T, std::size_t N>
+        struct array_to_pointer_decay<T[N]>
+        {
+            typedef const T * type;
+        };
+    }
+
+    namespace detail // is_this_float_conversion_optimized<Float, Char>
+    {
+        // this metafunction evaluates to true, if we have optimized comnversion 
+        // from Float type to Char array. 
+        // Must be in sync with lexical_stream_limited_src<Char, ...>::shl_real_type(...)
+        template <typename Float, typename Char>
+        struct is_this_float_conversion_optimized 
+        {
+            typedef boost::type_traits::ice_and<
+                boost::is_float<Float>::value,
+#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_SWPRINTF) && !defined(__MINGW32__)
+                boost::type_traits::ice_or<
+                    boost::type_traits::ice_eq<sizeof(Char), sizeof(char) >::value,
+                    boost::is_same<Char, wchar_t>::value
+                >::value
+#else
+                boost::type_traits::ice_eq<sizeof(Char), sizeof(char) >::value
+#endif
+            > result_type;
+
+            BOOST_STATIC_CONSTANT(bool, value = (result_type::value) );
+        };
+    }
+    
     namespace detail // lcast_src_length
     {
         // Return max. length of string representation of Source;
@@ -391,7 +589,7 @@ namespace boost
             BOOST_STATIC_ASSERT(sizeof(Source) * CHAR_BIT <= 256);
 #endif
         };
-// TODO: FIX for char16_t, char32_t, we can ignore CharT
+
 #define BOOST_LCAST_DEF(T)               \
     template<> struct lcast_src_length<T> \
         : lcast_src_length_integral<T>           \
@@ -410,6 +608,10 @@ namespace boost
         BOOST_LCAST_DEF(unsigned __int64)
         BOOST_LCAST_DEF(         __int64)
 #endif
+#ifdef BOOST_HAS_INT128
+        BOOST_LCAST_DEF(boost::int128_type)
+        BOOST_LCAST_DEF(boost::uint128_type)
+#endif
 
 #undef BOOST_LCAST_DEF
 
@@ -460,9 +662,67 @@ namespace boost
 #endif // #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
     }
 
+    namespace detail // lexical_cast_stream_traits<Source, Target>
+    {
+        template <class Source, class Target>
+        struct lexical_cast_stream_traits {
+            typedef BOOST_DEDUCED_TYPENAME boost::detail::array_to_pointer_decay<Source>::type src;
+            typedef BOOST_DEDUCED_TYPENAME boost::remove_cv<src>::type            no_cv_src;
+                
+            typedef boost::detail::deduce_source_char<no_cv_src>                           deduce_src_char_metafunc;
+            typedef BOOST_DEDUCED_TYPENAME deduce_src_char_metafunc::type           src_char_t;
+            typedef BOOST_DEDUCED_TYPENAME boost::detail::deduce_target_char<Target>::type target_char_t;
+                
+            typedef BOOST_DEDUCED_TYPENAME boost::detail::widest_char<
+                target_char_t, src_char_t
+            >::type char_type;
+
+#if !defined(BOOST_NO_CXX11_CHAR16_T) && defined(BOOST_NO_CXX11_UNICODE_LITERALS)
+            BOOST_STATIC_ASSERT_MSG(( !boost::is_same<char16_t, src_char_t>::value
+                                        && !boost::is_same<char16_t, target_char_t>::value),
+                "Your compiler does not have full support for char16_t" );
+#endif
+#if !defined(BOOST_NO_CXX11_CHAR32_T) && defined(BOOST_NO_CXX11_UNICODE_LITERALS)
+            BOOST_STATIC_ASSERT_MSG(( !boost::is_same<char32_t, src_char_t>::value
+                                        && !boost::is_same<char32_t, target_char_t>::value),
+                "Your compiler does not have full support for char32_t" );
+#endif
+
+            typedef BOOST_DEDUCED_TYPENAME boost::detail::deduce_char_traits<
+                char_type, Target, no_cv_src
+            >::type traits;
+
+            typedef boost::type_traits::ice_and<
+                boost::is_same<char, src_char_t>::value,                                  // source is not a wide character based type
+                boost::type_traits::ice_ne<sizeof(char), sizeof(target_char_t) >::value,  // target type is based on wide character
+                boost::type_traits::ice_not<
+                    boost::detail::is_char_or_wchar<no_cv_src>::value                     // single character widening is optimized
+                >::value                                                                  // and does not requires stringbuffer
+            >   is_string_widening_required_t;
+
+            typedef boost::type_traits::ice_not< boost::type_traits::ice_or<
+                boost::is_integral<no_cv_src>::value,
+                boost::detail::is_this_float_conversion_optimized<no_cv_src, char_type >::value,
+                boost::detail::is_char_or_wchar<
+                    BOOST_DEDUCED_TYPENAME deduce_src_char_metafunc::stage1_type          // if we did not get character type at stage1
+                >::value                                                                  // then we have no optimization for that type
+            >::value >   is_source_input_not_optimized_t;
+
+            // If we have an optimized conversion for
+            // Source, we do not need to construct stringbuf.
+            BOOST_STATIC_CONSTANT(bool, requires_stringbuf = 
+                (boost::type_traits::ice_or<
+                    is_string_widening_required_t::value, is_source_input_not_optimized_t::value
+                >::value)
+            );
+            
+            typedef boost::detail::lcast_src_length<no_cv_src> len_t;
+        };
+    }
+
     namespace detail // '0', '+' and '-' constants
     {
-        template<typename CharT> struct lcast_char_constants;
+        template < typename Char > struct lcast_char_constants;
 
         template<>
         struct lcast_char_constants<char>
@@ -488,7 +748,7 @@ namespace boost
         };
 #endif
 
-#ifndef BOOST_NO_CHAR16_T
+#if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
         template<>
         struct lcast_char_constants<char16_t>
         {
@@ -501,7 +761,7 @@ namespace boost
         };
 #endif
 
-#ifndef BOOST_NO_CHAR32_T
+#if !defined(BOOST_NO_CXX11_CHAR32_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
         template<>
         struct lcast_char_constants<char32_t>
         {
@@ -517,26 +777,15 @@ namespace boost
 
     namespace detail // lcast_to_unsigned
     {
-#if (defined _MSC_VER)
-# pragma warning( push )
-// C4146: unary minus operator applied to unsigned type, result still unsigned
-# pragma warning( disable : 4146 )
-#elif defined( __BORLANDC__ )
-# pragma option push -w-8041
-#endif
         template<class T>
         inline
-        BOOST_DEDUCED_TYPENAME make_unsigned<T>::type lcast_to_unsigned(T value)
+        BOOST_DEDUCED_TYPENAME make_unsigned<T>::type lcast_to_unsigned(T value) BOOST_NOEXCEPT
         {
-            typedef BOOST_DEDUCED_TYPENAME make_unsigned<T>::type result_type;
-            result_type uvalue = static_cast<result_type>(value);
-            return value < 0 ? -uvalue : uvalue;
+            typedef BOOST_DEDUCED_TYPENAME boost::make_unsigned<T>::type result_type;
+            return static_cast<result_type>(
+                value < 0 ? 0u - static_cast<result_type>(value) : value
+            );
         }
-#if (defined _MSC_VER)
-# pragma warning( pop )
-#elif defined( __BORLANDC__ )
-# pragma option pop
-#endif
     }
 
     namespace detail // lcast_put_unsigned
@@ -585,7 +834,7 @@ namespace boost
                             if(group < grouping_size)
                             {
                                 char const grp_size = grouping[group];
-                                last_grp_size = grp_size <= 0 ? CHAR_MAX : grp_size;
+                                last_grp_size = grp_size <= 0 ? static_cast<char>(CHAR_MAX) : grp_size;
                             }
 
                             left = last_grp_size;
@@ -625,15 +874,23 @@ namespace boost
         {
 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
             BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
+
+            // GCC when used with flag -std=c++0x may not have std::numeric_limits
+            // specializations for __int128 and unsigned __int128 types.
+            // Try compilation with -std=gnu++0x or -std=gnu++11.
+            //
+            // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40856
+            BOOST_STATIC_ASSERT_MSG(std::numeric_limits<T>::is_specialized,
+                "std::numeric_limits are not specialized for integral type passed to boost::lexical_cast"
+            );
 #endif
-            typedef typename Traits::int_type int_type;
             CharT const czero = lcast_char_constants<CharT>::zero;
             --end;
             value = 0;
 
             if (begin > end || *end < czero || *end >= czero + 10)
                 return false;
-            value = *end - czero;
+            value = static_cast<T>(*end - czero);
             --end;
             T multiplier = 1;
             bool multiplier_overflowed = false;
@@ -653,17 +910,17 @@ namespace boost
                 {
                     unsigned char current_grouping = 0;
                     CharT const thousands_sep = np.thousands_sep();
-                    char remained = grouping[current_grouping] - 1;
+                    char remained = static_cast<char>(grouping[current_grouping] - 1);
                     bool shall_we_return = true;
 
                     for(;end>=begin; --end)
                     {
                         if (remained) {
-                            T const multiplier_10 = multiplier * 10;
+                            T const multiplier_10 = static_cast<T>(multiplier * 10);
                             if (multiplier_10 / 10 != multiplier) multiplier_overflowed = true;
 
-                            T const dig_value = *end - czero;
-                            T const new_sub_value = multiplier_10 * dig_value;
+                            T const dig_value = static_cast<T>(*end - czero);
+                            T const new_sub_value = static_cast<T>(multiplier_10 * dig_value);
 
                             if (*end < czero || *end >= czero + 10
                                     /* detecting overflow */
@@ -673,8 +930,8 @@ namespace boost
                                     )
                                 return false;
 
-                            value += new_sub_value;
-                            multiplier *= 10;
+                            value = static_cast<T>(value + new_sub_value);
+                            multiplier = static_cast<T>(multiplier * 10);
                             --remained;
                         } else {
                             if ( !Traits::eq(*end, thousands_sep) ) //|| begin == end ) return false;
@@ -707,11 +964,11 @@ namespace boost
             {
                 while ( begin <= end )
                 {
-                    T const multiplier_10 = multiplier * 10;
+                    T const multiplier_10 = static_cast<T>(multiplier * 10);
                     if (multiplier_10 / 10 != multiplier) multiplier_overflowed = true;
 
-                    T const dig_value = *end - czero;
-                    T const new_sub_value = multiplier_10 * dig_value;
+                    T const dig_value = static_cast<T>(*end - czero);
+                    T const new_sub_value = static_cast<T>(multiplier_10 * dig_value);
 
                     if (*end < czero || *end >= czero + 10
                             /* detecting overflow */
@@ -721,8 +978,8 @@ namespace boost
                             )
                         return false;
 
-                    value += new_sub_value;
-                    multiplier *= 10;
+                    value = static_cast<T>(value + new_sub_value);
+                    multiplier = static_cast<T>(multiplier * 10);
                     --end;
                 }
             }
@@ -732,12 +989,21 @@ namespace boost
 
     namespace detail
     {
+        template <class CharT>
+        bool lc_iequal(const CharT* val, const CharT* lcase, const CharT* ucase, unsigned int len) BOOST_NOEXCEPT {
+            for( unsigned int i=0; i < len; ++i ) {
+                if ( val[i] != lcase[i] && val[i] != ucase[i] ) return false;
+            }
+
+            return true;
+        }
+
         /* Returns true and sets the correct value if found NaN or Inf. */
         template <class CharT, class T>
         inline bool parse_inf_nan_impl(const CharT* begin, const CharT* end, T& value
             , const CharT* lc_NAN, const CharT* lc_nan
             , const CharT* lc_INFINITY, const CharT* lc_infinity
-            , const CharT opening_brace, const CharT closing_brace)
+            , const CharT opening_brace, const CharT closing_brace) BOOST_NOEXCEPT
         {
             using namespace std;
             if (begin == end) return false;
@@ -755,7 +1021,7 @@ namespace boost
             else if( *begin == plus ) ++begin;
 
             if( end-begin < 3 ) return false;
-            if( !memcmp(begin, lc_nan, 3*sizeof(CharT)) || !memcmp(begin, lc_NAN, 3*sizeof(CharT)) )
+            if( lc_iequal(begin, lc_nan, lc_NAN, 3) )
             {
                 begin += 3;
                 if (end != begin) /* It is 'nan(...)' or some bad input*/
@@ -772,13 +1038,13 @@ namespace boost
             if (( /* 'INF' or 'inf' */
                   end-begin==3
                   &&
-                  (!memcmp(begin, lc_infinity, 3*sizeof(CharT)) || !memcmp(begin, lc_INFINITY, 3*sizeof(CharT)))
+                  lc_iequal(begin, lc_infinity, lc_INFINITY, 3)
                 )
                 ||
                 ( /* 'INFINITY' or 'infinity' */
                   end-begin==inifinity_size
                   &&
-                  (!memcmp(begin, lc_infinity, inifinity_size)|| !memcmp(begin, lc_INFINITY, inifinity_size))
+                  lc_iequal(begin, lc_infinity, lc_INFINITY, inifinity_size)
                 )
              )
             {
@@ -790,117 +1056,116 @@ namespace boost
             return false;
         }
 
+        template <class CharT, class T>
+        bool put_inf_nan_impl(CharT* begin, CharT*& end, const T& value
+                         , const CharT* lc_nan
+                         , const CharT* lc_infinity) BOOST_NOEXCEPT
+        {
+            using namespace std;
+            const CharT minus = lcast_char_constants<CharT>::minus;
+            if ( (boost::math::isnan)(value) )
+            {
+                if ( (boost::math::signbit)(value) )
+                {
+                    *begin = minus;
+                    ++ begin;
+                }
+
+                memcpy(begin, lc_nan, 3 * sizeof(CharT));
+                end = begin + 3;
+                return true;
+            } else if ( (boost::math::isinf)(value) )
+            {
+                if ( (boost::math::signbit)(value) )
+                {
+                    *begin = minus;
+                    ++ begin;
+                }
+
+                memcpy(begin, lc_infinity, 3 * sizeof(CharT));
+                end = begin + 3;
+                return true;
+            }
+
+            return false;
+        }
+
+
 #ifndef BOOST_LCAST_NO_WCHAR_T
         template <class T>
-        bool parse_inf_nan(const wchar_t* begin, const wchar_t* end, T& value)
+        bool parse_inf_nan(const wchar_t* begin, const wchar_t* end, T& value) BOOST_NOEXCEPT
         {
             return parse_inf_nan_impl(begin, end, value
                                , L"NAN", L"nan"
                                , L"INFINITY", L"infinity"
                                , L'(', L')');
         }
+
+        template <class T>
+        bool put_inf_nan(wchar_t* begin, wchar_t*& end, const T& value) BOOST_NOEXCEPT
+        {
+            return put_inf_nan_impl(begin, end, value, L"nan", L"infinity");
+        }
+
 #endif
-#ifndef BOOST_NO_CHAR16_T
+#if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
         template <class T>
-        bool parse_inf_nan(const char16_t* begin, const char16_t* end, T& value)
+        bool parse_inf_nan(const char16_t* begin, const char16_t* end, T& value) BOOST_NOEXCEPT
         {
             return parse_inf_nan_impl(begin, end, value
                                , u"NAN", u"nan"
                                , u"INFINITY", u"infinity"
                                , u'(', u')');
         }
+
+        template <class T>
+        bool put_inf_nan(char16_t* begin, char16_t*& end, const T& value) BOOST_NOEXCEPT
+        {
+            return put_inf_nan_impl(begin, end, value, u"nan", u"infinity");
+        }
 #endif
-#ifndef BOOST_NO_CHAR32_T
+#if !defined(BOOST_NO_CXX11_CHAR32_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
         template <class T>
-        bool parse_inf_nan(const char32_t* begin, const char32_t* end, T& value)
+        bool parse_inf_nan(const char32_t* begin, const char32_t* end, T& value) BOOST_NOEXCEPT
         {
             return parse_inf_nan_impl(begin, end, value
                                , U"NAN", U"nan"
                                , U"INFINITY", U"infinity"
                                , U'(', U')');
         }
+
+        template <class T>
+        bool put_inf_nan(char32_t* begin, char32_t*& end, const T& value) BOOST_NOEXCEPT
+        {
+            return put_inf_nan_impl(begin, end, value, U"nan", U"infinity");
+        }
 #endif
 
         template <class CharT, class T>
-        bool parse_inf_nan(const CharT* begin, const CharT* end, T& value)
+        bool parse_inf_nan(const CharT* begin, const CharT* end, T& value) BOOST_NOEXCEPT
         {
             return parse_inf_nan_impl(begin, end, value
                                , "NAN", "nan"
                                , "INFINITY", "infinity"
                                , '(', ')');
         }
-#ifndef BOOST_LCAST_NO_WCHAR_T
-        template <class T>
-        bool put_inf_nan(wchar_t* begin, wchar_t*& end, const T& value)
-        {
-            using namespace std;
-            if ( (boost::math::isnan)(value) )
-            {
-                if ( (boost::math::signbit)(value) )
-                {
-                    memcpy(begin,L"-nan", sizeof(L"-nan"));
-                    end = begin + 4;
-                } else
-                {
-                    memcpy(begin,L"nan", sizeof(L"nan"));
-                    end = begin + 3;
-                }
-                return true;
-            } else if ( (boost::math::isinf)(value) )
-            {
-                if ( (boost::math::signbit)(value) )
-                {
-                    memcpy(begin,L"-inf", sizeof(L"-inf"));
-                    end = begin + 4;
-                } else
-                {
-                    memcpy(begin,L"inf", sizeof(L"inf"));
-                    end = begin + 3;
-                }
-                return true;
-            }
 
-            return false;
-        }
-#endif
         template <class CharT, class T>
-        bool put_inf_nan(CharT* begin, CharT*& end, const T& value)
+        bool put_inf_nan(CharT* begin, CharT*& end, const T& value) BOOST_NOEXCEPT
         {
-            using namespace std;
-            if ( (boost::math::isnan)(value) )
-            {
-                if ( (boost::math::signbit)(value) )
-                {
-                    memcpy(begin,"-nan", sizeof("-nan"));
-                    end = begin + 4;
-                } else
-                {
-                    memcpy(begin,"nan", sizeof("nan"));
-                    end = begin + 3;
-                }
-                return true;
-            } else if ( (boost::math::isinf)(value) )
-            {
-                if ( (boost::math::signbit)(value) )
-                {
-                    memcpy(begin,"-inf", sizeof("-inf"));
-                    end = begin + 4;
-                } else
-                {
-                    memcpy(begin,"inf", sizeof("inf"));
-                    end = begin + 3;
-                }
-                return true;
-            }
-
-            return false;
+            return put_inf_nan_impl(begin, end, value, "nan", "infinity");
         }
-
     }
 
 
     namespace detail // lcast_ret_float
     {
+
+// Silence buggy MS warnings like C4244: '+=' : conversion from 'int' to 'unsigned short', possible loss of data 
+#if defined(_MSC_VER) && (_MSC_VER == 1400) 
+#  pragma warning(push) 
+#  pragma warning(disable:4244) 
+#endif 
         template <class T>
         struct mantissa_holder_type
         {
@@ -911,15 +1176,19 @@ namespace boost
         struct mantissa_holder_type<float>
         {
             typedef unsigned int type;
+            typedef double       wide_result_t;
         };
 
         template <>
         struct mantissa_holder_type<double>
         {
+#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
+            typedef long double  wide_result_t;
 #if defined(BOOST_HAS_LONG_LONG)
             typedef boost::ulong_long_type type;
 #elif defined(BOOST_HAS_MS_INT64)
             typedef unsigned __int64 type;
+#endif
 #endif
         };
 
@@ -937,7 +1206,7 @@ namespace boost
                     : np.grouping()
             );
             std::string::size_type const grouping_size = grouping.size();
-            CharT const thousands_sep = grouping_size ? np.thousands_sep() : 0;
+            CharT const thousands_sep = static_cast<CharT>(grouping_size ? np.thousands_sep() : 0);
             CharT const decimal_point = np.decimal_point();
             bool found_grouping = false;
             std::string::size_type last_grouping_pos = grouping_size - 1;
@@ -951,22 +1220,23 @@ namespace boost
             CharT const capital_e = lcast_char_constants<CharT>::capital_e;
             CharT const lowercase_e = lcast_char_constants<CharT>::lowercase_e;
 
-            value = 0.0;
+            value = static_cast<T>(0);
 
             if (parse_inf_nan(begin, end, value)) return true;
 
             typedef typename Traits::int_type int_type;
             typedef BOOST_DEDUCED_TYPENAME mantissa_holder_type<T>::type mantissa_type;
+            typedef BOOST_DEDUCED_TYPENAME mantissa_holder_type<T>::wide_result_t wide_result_t;
             int_type const zero = Traits::to_int_type(czero);
             if (begin == end) return false;
 
             /* Getting the plus/minus sign */
             bool has_minus = false;
-            if ( *begin == minus ) {
+            if (Traits::eq(*begin, minus) ) {
                 ++ begin;
                 has_minus = true;
                 if (begin == end) return false;
-            } else if ( *begin == plus ) {
+            } else if (Traits::eq(*begin, plus) ) {
                 ++begin;
                 if (begin == end) return false;
             }
@@ -985,7 +1255,7 @@ namespace boost
                     /* We allow no thousand_separators after decimal point */
 
                     mantissa_type tmp_mantissa = mantissa * 10u;
-                    if ( *begin == lowercase_e || *begin == capital_e ) break;
+                    if (Traits::eq(*begin, lowercase_e) || Traits::eq(*begin, capital_e)) break;
                     if ( *begin < czero || *begin >= czero + 10 ) return false;
                     if (    is_mantissa_full
                             || tmp_mantissa / 10u != mantissa
@@ -1024,7 +1294,7 @@ namespace boost
 
                         found_number_before_exp = true;
                         ++ length_since_last_delim;
-                    } else if ( *begin == decimal_point || *begin == lowercase_e || *begin == capital_e) {
+                    } else if (Traits::eq(*begin, decimal_point) || Traits::eq(*begin, lowercase_e) || Traits::eq(*begin, capital_e)) {
 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
                         /* If ( we need to check grouping
                          *      and (   grouping missmatches
@@ -1042,9 +1312,10 @@ namespace boost
                            ) return false;
 #endif
 
-                        if(*begin == decimal_point){
+                        if(Traits::eq(*begin, decimal_point)) {
                             ++ begin;
                             found_decimal = true;
+                            if (!found_number_before_exp && begin==end) return false;
                             continue;
                         }else {
                             if (!found_number_before_exp) return false;
@@ -1052,7 +1323,7 @@ namespace boost
                         }
                     }
 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
-                    else if (grouping_size && *begin == thousands_sep){
+                    else if (grouping_size && Traits::eq(*begin, thousands_sep)){
                         if(found_grouping)
                         {
                             /* It is not he first time, when we find thousands separator,
@@ -1094,16 +1365,16 @@ namespace boost
             }
 
             // Exponent found
-            if ( begin != end && ( *begin == lowercase_e || *begin == capital_e ) ) {
+            if ( begin != end && (Traits::eq(*begin, lowercase_e) || Traits::eq(*begin, capital_e)) ) {
                 ++ begin;
                 if ( begin == end ) return false;
 
                 bool exp_has_minus = false;
-                if( *begin == minus ) {
+                if(Traits::eq(*begin, minus)) {
                     exp_has_minus = true;
                     ++ begin;
                     if ( begin == end ) return false;
-                } else if (*begin == plus ) {
+                } else if (Traits::eq(*begin, plus)) {
                     ++ begin;
                     if ( begin == end ) return false;
                 }
@@ -1134,38 +1405,116 @@ namespace boost
             /* We need a more accurate algorithm... We can not use current algorithm
              * with long doubles (and with doubles if sizeof(double)==sizeof(long double)).
              */
-            long double result = std::pow(10.0L, pow_of_10) * mantissa;
+            const wide_result_t result = std::pow(static_cast<wide_result_t>(10.0), pow_of_10) * mantissa;
             value = static_cast<T>( has_minus ? (boost::math::changesign)(result) : result);
 
             if ( (boost::math::isinf)(value) || (boost::math::isnan)(value) ) return false;
 
             return true;
         }
+// Unsilence buggy MS warnings like C4244: '+=' : conversion from 'int' to 'unsigned short', possible loss of data 
+#if defined(_MSC_VER) && (_MSC_VER == 1400) 
+#  pragma warning(pop) 
+#endif 
     }
 
-    namespace detail // stl_buf_unlocker
+    namespace detail // parser_buf
     {
-        template< class BufferType, class CharT >
-        class stl_buf_unlocker: public BufferType{
+        //
+        // class parser_buf:
+        // acts as a stream buffer which wraps around a pair of pointers
+        //
+        // This class is copied (and slightly changed) from
+        // boost/regex/v4/cpp_regex_traits.hpp
+        // Thanks John Maddock for it! (previous version had some
+        // problems with libc++ and some other STL implementations)
+        template <class BufferType, class charT>
+        class parser_buf : public BufferType {
+           typedef BufferType base_type;
+           typedef typename base_type::int_type int_type;
+           typedef typename base_type::char_type char_type;
+           typedef typename base_type::pos_type pos_type;
+           typedef ::std::streamsize streamsize;
+           typedef typename base_type::off_type off_type;
+
         public:
-            typedef BufferType base_class;
+           parser_buf() : base_type() { setbuf(0, 0); }
+           const charT* getnext() { return this->gptr(); }
 #ifndef BOOST_NO_USING_TEMPLATE
-            using base_class::pptr;
-            using base_class::pbase;
-            using base_class::setg;
-            using base_class::setp;
+            using base_type::pptr;
+            using base_type::pbase;
 #else
-            CharT* pptr() const { return base_class::pptr(); }
-            CharT* pbase() const { return base_class::pbase(); }
-            void setg(CharT* gbeg, CharT* gnext, CharT* gend){ return base_class::setg(gbeg, gnext, gend); }
-            void setp(CharT* pbeg, CharT* pend) { return setp(pbeg, pend); }
+            charT* pptr() const { return base_type::pptr(); }
+            charT* pbase() const { return base_type::pbase(); }
+#endif
+           base_type* setbuf(char_type* s, streamsize n) {
+               this->setg(s, s, s + n);
+               return this;
+           }
+
+           pos_type seekpos(pos_type sp, ::std::ios_base::openmode which) {
+               if(which & ::std::ios_base::out)
+                  return pos_type(off_type(-1));
+               off_type size = static_cast<off_type>(this->egptr() - this->eback());
+               charT* g = this->eback();
+               if(off_type(sp) <= size)
+               {
+                  this->setg(g, g + off_type(sp), g + size);
+               }
+               return pos_type(off_type(-1));
+            }
+
+           pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which) {
+               typedef typename boost::int_t<sizeof(way) * CHAR_BIT>::least cast_type;
+
+               if(which & ::std::ios_base::out)
+                  return pos_type(off_type(-1));
+               std::ptrdiff_t size = this->egptr() - this->eback();
+               std::ptrdiff_t pos = this->gptr() - this->eback();
+               charT* g = this->eback();
+               switch(static_cast<cast_type>(way))
+               {
+               case ::std::ios_base::beg:
+                  if((off < 0) || (off > size))
+                     return pos_type(off_type(-1));
+                  else
+                     this->setg(g, g + off, g + size);
+                  break;
+               case ::std::ios_base::end:
+                  if((off < 0) || (off > size))
+                     return pos_type(off_type(-1));
+                  else
+                     this->setg(g, g + size - off, g + size);
+                  break;
+               case ::std::ios_base::cur:
+               {
+                  std::ptrdiff_t newpos = static_cast<std::ptrdiff_t>(pos + off);
+                  if((newpos < 0) || (newpos > size))
+                     return pos_type(off_type(-1));
+                  else
+                     this->setg(g, g + newpos, g + size);
+                  break;
+               }
+               default: ;
+               }
+#ifdef BOOST_MSVC
+#pragma warning(push)
+#pragma warning(disable:4244)
+#endif
+               return static_cast<pos_type>(this->gptr() - this->eback());
+#ifdef BOOST_MSVC
+#pragma warning(pop)
 #endif
+            }
+        private:
+           parser_buf& operator=(const parser_buf&);
+           parser_buf(const parser_buf&);
         };
     }
 
     namespace detail
     {
-        struct do_not_construct_stringbuffer_t{};
+        struct do_not_construct_out_stream_t{};
     }
 
     namespace detail // optimized stream wrapper
@@ -1177,28 +1526,29 @@ namespace boost
                 >
         class lexical_stream_limited_src
         {
-            typedef stl_buf_unlocker<std::basic_streambuf<CharT, Traits>, CharT > local_streambuffer_t;
 
 #if defined(BOOST_NO_STRINGSTREAM)
-            typedef stl_buf_unlocker<std::strstream, CharT > local_stringbuffer_t;
+            typedef std::ostrstream                         out_stream_t;
 #elif defined(BOOST_NO_STD_LOCALE)
-            typedef stl_buf_unlocker<std::stringstream, CharT > local_stringbuffer_t;
+            typedef std::ostringstream                      out_stream_t;
+            typedef parser_buf<std::streambuf, char>        buffer_t;
 #else
-            typedef stl_buf_unlocker<std::basic_stringbuf<CharT, Traits>, CharT > local_stringbuffer_t;
+            typedef std::basic_ostringstream<CharT, Traits>                 out_stream_t;
+            typedef parser_buf<std::basic_streambuf<CharT, Traits>, CharT>  buffer_t;
 #endif
-            typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
+            typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
                 RequiresStringbuffer,
-                local_stringbuffer_t,
-                do_not_construct_stringbuffer_t
-            >::type deduced_stringbuffer_t;
+                out_stream_t,
+                do_not_construct_out_stream_t
+            >::type deduced_out_stream_t;
 
             // A string representation of Source is written to [start, finish).
             CharT* start;
             CharT* finish;
-            deduced_stringbuffer_t stringbuffer;
+            deduced_out_stream_t out_stream;
 
         public:
-            lexical_stream_limited_src(CharT* sta, CharT* fin)
+            lexical_stream_limited_src(CharT* sta, CharT* fin) BOOST_NOEXCEPT
               : start(sta)
               , finish(fin)
             {}
@@ -1209,7 +1559,7 @@ namespace boost
             void operator=(lexical_stream_limited_src const&);
 
 /************************************ HELPER FUNCTIONS FOR OPERATORS << ( ... ) ********************************/
-            bool shl_char(CharT ch)
+            bool shl_char(CharT ch) BOOST_NOEXCEPT
             {
                 Traits::assign(*start, ch);
                 finish = start + 1;
@@ -1221,13 +1571,13 @@ namespace boost
             bool shl_char(T ch)
             {
                 BOOST_STATIC_ASSERT_MSG(( sizeof(T) <= sizeof(CharT)) ,
-                    "boost::lexical_cast does not support conversions from whar_t to char types."
+                    "boost::lexical_cast does not support narrowing of char types."
                     "Use boost::locale instead" );
 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
                 std::locale loc;
-                wchar_t w = BOOST_USE_FACET(std::ctype<wchar_t>, loc).widen(ch);
+                CharT const w = BOOST_USE_FACET(std::ctype<CharT>, loc).widen(ch);
 #else
-                wchar_t w = ch;
+                CharT const w = static_cast<CharT>(ch);
 #endif
                 Traits::assign(*start, w);
                 finish = start + 1;
@@ -1235,32 +1585,54 @@ namespace boost
             }
 #endif
 
-            bool shl_char_array(CharT const* str)
+            bool shl_char_array(CharT const* str) BOOST_NOEXCEPT
             {
                 start = const_cast<CharT*>(str);
                 finish = start + Traits::length(str);
                 return true;
             }
 
-#ifndef BOOST_LCAST_NO_WCHAR_T
             template <class T>
             bool shl_char_array(T const* str)
             {
                 BOOST_STATIC_ASSERT_MSG(( sizeof(T) <= sizeof(CharT)),
-                    "boost::lexical_cast does not support conversions from wchar_t to char types."
+                    "boost::lexical_cast does not support narrowing of char types."
                     "Use boost::locale instead" );
                 return shl_input_streamable(str);
             }
-#endif
+            
+            bool shl_char_array_limited(CharT const* str, std::size_t max_size) BOOST_NOEXCEPT
+            {
+                start = const_cast<CharT*>(str);
+                finish = std::find(start, start + max_size, Traits::to_char_type(0));
+                return true;
+            }
 
             template<typename InputStreamable>
             bool shl_input_streamable(InputStreamable& input)
             {
-                std::basic_ostream<CharT> stream(&stringbuffer);
-                bool const result = !(stream << input).fail();
-                start = stringbuffer.pbase();
-                finish = stringbuffer.pptr();
+#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_LOCALE)
+                // If you have compilation error at this point, than your STL library
+                // does not support such conversions. Try updating it.
+                BOOST_STATIC_ASSERT((boost::is_same<char, CharT>::value));
+#endif
+
+#ifndef BOOST_NO_EXCEPTIONS
+                out_stream.exceptions(std::ios::badbit);
+                try {
+#endif
+                bool const result = !(out_stream << input).fail();
+                const buffer_t* const p = static_cast<buffer_t*>(
+                    static_cast<std::basic_streambuf<CharT, Traits>*>(out_stream.rdbuf())
+                );
+                start = p->pbase();
+                finish = p->pptr();
                 return result;
+#ifndef BOOST_NO_EXCEPTIONS
+                } catch (const ::std::ios_base::failure& /*f*/) {
+                    return false;
+                }
+#endif
             }
 
             template <class T>
@@ -1276,110 +1648,105 @@ namespace boost
                 return true;
             }
 
-#if (defined _MSC_VER)
-# pragma warning( push )
-// C4996: This function or variable may be unsafe. Consider using sprintf_s instead
-# pragma warning( disable : 4996 )
-#endif
+            template <class T, class SomeCharT>
+            bool shl_real_type(const T& val, SomeCharT* begin, SomeCharT*& end)
+            {
+                if (put_inf_nan(begin, end, val)) return true;
+                lcast_set_precision(out_stream, &val);
+                return shl_input_streamable(val);
+            }
 
-            template <class T>
-            bool shl_float(float val,T* out)
+            static bool shl_real_type(float val, char* begin, char*& end)
             {   using namespace std;
-                if (put_inf_nan(start,finish,val)) return true;
-                finish = start + sprintf(out,"%.*g", static_cast<int>(boost::detail::lcast_get_precision<float >()), val );
-                return finish > start;
+                if (put_inf_nan(begin, end, val)) return true;
+                const double val_as_double = val;
+                end = begin + 
+#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
+                    sprintf_s(begin, end-begin,
+#else
+                    sprintf(begin, 
+#endif
+                    "%.*g", static_cast<int>(boost::detail::lcast_get_precision<float>()), val_as_double);
+                return end > begin;
             }
 
-            template <class T>
-            bool shl_double(double val,T* out)
+            static bool shl_real_type(double val, char* begin, char*& end)
             {   using namespace std;
-                if (put_inf_nan(start,finish,val)) return true;
-                finish = start + sprintf(out,"%.*lg", static_cast<int>(boost::detail::lcast_get_precision<double >()), val );
-                return finish > start;
+                if (put_inf_nan(begin, end, val)) return true;
+                end = begin + 
+#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
+                    sprintf_s(begin, end-begin,
+#else
+                    sprintf(begin, 
+#endif
+                    "%.*g", static_cast<int>(boost::detail::lcast_get_precision<double>()), val);
+                return end > begin;
             }
+
 #ifndef __MINGW32__
-            template <class T>
-            bool shl_long_double(long double val,T* out)
+            static bool shl_real_type(long double val, char* begin, char*& end)
             {   using namespace std;
-                if (put_inf_nan(start,finish,val)) return true;
-                finish = start + sprintf(out,"%.*Lg", static_cast<int>(boost::detail::lcast_get_precision<long double >()), val );
-                return finish > start;
-            }
+                if (put_inf_nan(begin, end, val)) return true;
+                end = begin + 
+#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
+                    sprintf_s(begin, end-begin,
+#else
+                    sprintf(begin, 
 #endif
-
-#if (defined _MSC_VER)
-# pragma warning( pop )
+                    "%.*Lg", static_cast<int>(boost::detail::lcast_get_precision<long double>()), val );
+                return end > begin;
+            }
 #endif
 
 
-#ifndef BOOST_LCAST_NO_WCHAR_T
-            bool shl_float(float val,wchar_t* out)
+#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_SWPRINTF) && !defined(__MINGW32__)
+            static bool shl_real_type(float val, wchar_t* begin, wchar_t*& end)
             {   using namespace std;
-                if (put_inf_nan(start,finish,val)) return true;
-                finish = start + swprintf(out,
-#if !defined(__MINGW32__) && !defined(UNDER_CE)
-                                          finish-start,
-#endif
-                                          L"%.*g", static_cast<int>(boost::detail::lcast_get_precision<float >()), val );
-
-                return finish > start;
+                if (put_inf_nan(begin, end, val)) return true;
+                const double val_as_double = val;
+                end = begin + swprintf(begin, end-begin,
+                                       L"%.*g",
+                                       static_cast<int>(boost::detail::lcast_get_precision<float >()),
+                                       val_as_double );
+                return end > begin;
             }
 
-
-            bool shl_double(double val,wchar_t* out)
+            static bool shl_real_type(double val, wchar_t* begin, wchar_t*& end)
             {   using namespace std;
-                if (put_inf_nan(start,finish,val)) return true;
-                /* __MINGW32__ is defined for both mingw.org and for mingw-w64.
-                 * For mingw-w64, __MINGW64__ is defined, too, when targetting
-                 * 64 bits.
-                 *
-                 * swprintf realization in MinGW and under WinCE does not conform
-                 * to the ISO C
-                 * Standard.
-                 */
-                finish = start + swprintf(out,
-#if !defined(__MINGW32__) && !defined(UNDER_CE)
-                                          finish-start,
-#endif
-                                          L"%.*lg", static_cast<int>(boost::detail::lcast_get_precision<double >()), val );
-                return finish > start;
+                if (put_inf_nan(begin, end, val)) return true;
+                end = begin + swprintf(begin, end-begin,
+                                          L"%.*g", static_cast<int>(boost::detail::lcast_get_precision<double >()), val );
+                return end > begin;
             }
 
-#ifndef __MINGW32__
-            bool shl_long_double(long double val,wchar_t* out)
+            static bool shl_real_type(long double val, wchar_t* begin, wchar_t*& end)
             {   using namespace std;
-                if (put_inf_nan(start,finish,val)) return true;
-                finish = start + swprintf(out,
-#if !defined(UNDER_CE)
-                                          finish-start,
-#endif
+                if (put_inf_nan(begin, end, val)) return true;
+                end = begin + swprintf(begin, end-begin,
                                           L"%.*Lg", static_cast<int>(boost::detail::lcast_get_precision<long double >()), val );
-                return finish > start;
+                return end > begin;
             }
 #endif
 
-#endif
-
 /************************************ OPERATORS << ( ... ) ********************************/
         public:
             template<class Alloc>
-            bool operator<<(std::basic_string<CharT,Traits,Alloc> const& str)
+            bool operator<<(std::basic_string<CharT,Traits,Alloc> const& str) BOOST_NOEXCEPT
             {
                 start = const_cast<CharT*>(str.data());
                 finish = start + str.length();
                 return true;
             }
 
-#if !defined(__SUNPRO_CC)
             template<class Alloc>
-            bool operator<<(::boost::container::basic_string<CharT,Traits,Alloc> const& str)
+            bool operator<<(boost::container::basic_string<CharT,Traits,Alloc> const& str) BOOST_NOEXCEPT
             {
                 start = const_cast<CharT*>(str.data());
                 finish = start + str.length();
                 return true;
             }
-#endif // !defined(__SUNPRO_CC)
-            bool operator<<(bool value)
+
+            bool operator<<(bool value) BOOST_NOEXCEPT
             {
                 CharT const czero = lcast_char_constants<CharT>::zero;
                 Traits::assign(*start, Traits::to_char_type(czero + value));
@@ -1387,6 +1754,52 @@ namespace boost
                 return true;
             }
 
+            bool operator<<(const iterator_range<CharT*>& rng) BOOST_NOEXCEPT
+            {
+                start = rng.begin();
+                finish = rng.end();
+                return true; 
+            }
+            
+            bool operator<<(const iterator_range<const CharT*>& rng) BOOST_NOEXCEPT
+            {
+                start = const_cast<CharT*>(rng.begin());
+                finish = const_cast<CharT*>(rng.end());
+                return true; 
+            }
+
+            bool operator<<(const iterator_range<const signed char*>& rng) BOOST_NOEXCEPT
+            {
+                return (*this) << iterator_range<char*>(
+                    const_cast<char*>(reinterpret_cast<const char*>(rng.begin())),
+                    const_cast<char*>(reinterpret_cast<const char*>(rng.end()))
+                );
+            }
+
+            bool operator<<(const iterator_range<const unsigned char*>& rng) BOOST_NOEXCEPT
+            {
+                return (*this) << iterator_range<char*>(
+                    const_cast<char*>(reinterpret_cast<const char*>(rng.begin())),
+                    const_cast<char*>(reinterpret_cast<const char*>(rng.end()))
+                );
+            }
+
+            bool operator<<(const iterator_range<signed char*>& rng) BOOST_NOEXCEPT
+            {
+                return (*this) << iterator_range<char*>(
+                    reinterpret_cast<char*>(rng.begin()),
+                    reinterpret_cast<char*>(rng.end())
+                );
+            }
+
+            bool operator<<(const iterator_range<unsigned char*>& rng) BOOST_NOEXCEPT
+            {
+                return (*this) << iterator_range<char*>(
+                    reinterpret_cast<char*>(rng.begin()),
+                    reinterpret_cast<char*>(rng.end())
+                );
+            }
+
             bool operator<<(char ch)                    { return shl_char(ch); }
             bool operator<<(unsigned char ch)           { return ((*this) << static_cast<char>(ch)); }
             bool operator<<(signed char ch)             { return ((*this) << static_cast<char>(ch)); }
@@ -1396,6 +1809,16 @@ namespace boost
 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
             bool operator<<(wchar_t ch)                 { return shl_char(ch); }
 #endif
+#endif
+#if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
+            bool operator<<(char16_t ch)                { return shl_char(ch); }
+            bool operator<<(char16_t * str)             { return shl_char_array(str); }
+            bool operator<<(char16_t const * str)       { return shl_char_array(str); }
+#endif
+#if !defined(BOOST_NO_CXX11_CHAR32_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
+            bool operator<<(char32_t ch)                { return shl_char(ch); }
+            bool operator<<(char32_t * str)             { return shl_char_array(str); }
+            bool operator<<(char32_t const * str)       { return shl_char_array(str); }
 #endif
             bool operator<<(unsigned char const* ch)    { return ((*this) << reinterpret_cast<char const*>(ch)); }
             bool operator<<(unsigned char * ch)         { return ((*this) << reinterpret_cast<char *>(ch)); }
@@ -1417,17 +1840,79 @@ namespace boost
             bool operator<<(unsigned __int64 n)         { start = lcast_put_unsigned<Traits>(n, finish); return true; }
             bool operator<<(         __int64 n)         { return shl_signed(n); }
 #endif
-            bool operator<<(float val)                  { return shl_float(val,start); }
-            bool operator<<(double val)                 { return shl_double(val,start); }
+
+#ifdef BOOST_HAS_INT128
+        bool operator<<(const boost::uint128_type& n)   { start = lcast_put_unsigned<Traits>(n, finish); return true; }
+        bool operator<<(const boost::int128_type& n)    { return shl_signed(n); }
+#endif
+
+            bool operator<<(float val)                  { return shl_real_type(val, start, finish); }
+            bool operator<<(double val)                 { return shl_real_type(val, start, finish); }
             bool operator<<(long double val)            {
 #ifndef __MINGW32__
-                return shl_long_double(val,start);
+                return shl_real_type(val, start, finish);
 #else
-                return shl_double(val,start);
+                return shl_real_type(static_cast<double>(val), start, finish);
 #endif
             }
+            
+            template <std::size_t N>
+            bool operator<<(boost::array<CharT, N> const& input) BOOST_NOEXCEPT
+            { return shl_char_array_limited(input.begin(), N); }
+
+            template <std::size_t N>
+            bool operator<<(boost::array<unsigned char, N> const& input) BOOST_NOEXCEPT
+            { return ((*this) << reinterpret_cast<boost::array<char, N> const& >(input)); }
+
+            template <std::size_t N>
+            bool operator<<(boost::array<signed char, N> const& input) BOOST_NOEXCEPT
+            { return ((*this) << reinterpret_cast<boost::array<char, N> const& >(input)); }
+
+            template <std::size_t N>
+            bool operator<<(boost::array<const CharT, N> const& input) BOOST_NOEXCEPT
+            { return shl_char_array_limited(input.begin(), N); }
+
+            template <std::size_t N>
+            bool operator<<(boost::array<const unsigned char, N> const& input) BOOST_NOEXCEPT
+            { return ((*this) << reinterpret_cast<boost::array<const char, N> const& >(input)); }
+
+            template <std::size_t N>
+            bool operator<<(boost::array<const signed char, N> const& input) BOOST_NOEXCEPT
+            { return ((*this) << reinterpret_cast<boost::array<const char, N> const& >(input)); }
+#ifndef BOOST_NO_CXX11_HDR_ARRAY
+            template <std::size_t N>
+            bool operator<<(std::array<CharT, N> const& input) BOOST_NOEXCEPT
+            { 
+                if (input.size()) return shl_char_array_limited(&input[0], N);
+                else return true; 
+            }
+
+            template <std::size_t N>
+            bool operator<<(std::array<unsigned char, N> const& input) BOOST_NOEXCEPT
+            { return ((*this) << reinterpret_cast<boost::array<char, N> const& >(input)); }
+
+            template <std::size_t N>
+            bool operator<<(std::array<signed char, N> const& input) BOOST_NOEXCEPT
+            { return ((*this) << reinterpret_cast<boost::array<char, N> const& >(input)); }
+
+            template <std::size_t N>
+            bool operator<<(std::array<const CharT, N> const& input) BOOST_NOEXCEPT
+            { 
+                if (input.size()) return shl_char_array_limited(&input[0], N);
+                else return true; 
+            }
+
+            template <std::size_t N>
+            bool operator<<(std::array<const unsigned char, N> const& input) BOOST_NOEXCEPT
+            { return ((*this) << reinterpret_cast<boost::array<const char, N> const& >(input)); }
 
-            template<class InStreamable>
+            template <std::size_t N>
+            bool operator<<(std::array<const signed char, N> const& input) BOOST_NOEXCEPT
+            { return ((*this) << reinterpret_cast<boost::array<const char, N> const& >(input)); }
+#endif
+            
+            template <class InStreamable>
             bool operator<<(const InStreamable& input)  { return shl_input_streamable(input); }
 
 /************************************ HELPER FUNCTIONS FOR OPERATORS >> ( ... ) ********************************/
@@ -1452,19 +1937,11 @@ namespace boost
                 }
 
                 bool const succeed = lcast_ret_unsigned<Traits>(output, start, finish);
-#if (defined _MSC_VER)
-# pragma warning( push )
-// C4146: unary minus operator applied to unsigned type, result still unsigned
-# pragma warning( disable : 4146 )
-#elif defined( __BORLANDC__ )
-# pragma option push -w-8041
-#endif
-                if (has_minus) output = static_cast<Type>(-output);
-#if (defined _MSC_VER)
-# pragma warning( pop )
-#elif defined( __BORLANDC__ )
-# pragma option pop
-#endif
+
+                if (has_minus) {
+                    output = static_cast<Type>(0u - output);
+                }
+
                 return succeed;
             }
 
@@ -1490,21 +1967,9 @@ namespace boost
 
                 bool succeed = lcast_ret_unsigned<Traits>(out_tmp, start, finish);
                 if (has_minus) {
-#if (defined _MSC_VER)
-# pragma warning( push )
-// C4146: unary minus operator applied to unsigned type, result still unsigned
-# pragma warning( disable : 4146 )
-#elif defined( __BORLANDC__ )
-# pragma option push -w-8041
-#endif
-                    utype const comp_val = static_cast<utype>(-(std::numeric_limits<Type>::min)());
+                    utype const comp_val = (static_cast<utype>(1) << std::numeric_limits<Type>::digits);
                     succeed = succeed && out_tmp<=comp_val;
-                    output = -out_tmp;
-#if (defined _MSC_VER)
-# pragma warning( pop )
-#elif defined( __BORLANDC__ )
-# pragma option pop
-#endif
+                    output = static_cast<Type>(0u - out_tmp);
                 } else {
                     utype const comp_val = static_cast<utype>((std::numeric_limits<Type>::max)());
                     succeed = succeed && out_tmp<=comp_val;
@@ -1516,22 +1981,38 @@ namespace boost
             template<typename InputStreamable>
             bool shr_using_base_class(InputStreamable& output)
             {
-#if (defined _MSC_VER)
-# pragma warning( push )
-  // conditional expression is constant
-# pragma warning( disable : 4127 )
+                BOOST_STATIC_ASSERT_MSG(
+                    (!boost::is_pointer<InputStreamable>::value),
+                    "boost::lexical_cast can not convert to pointers"
+                );
+
+#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_LOCALE)
+                BOOST_STATIC_ASSERT_MSG((boost::is_same<char, CharT>::value),
+                    "boost::lexical_cast can not convert, because your STL library does not "
+                    "support such conversions. Try updating it."
+                );
 #endif
-                if(is_pointer<InputStreamable>::value)
-                    return false;
 
-                local_streambuffer_t bb;
-                bb.setg(start, start, finish);
-                std::basic_istream<CharT> stream(&bb);
+#if defined(BOOST_NO_STRINGSTREAM)
+                std::istrstream stream(start, finish - start);
+#else
+
+                buffer_t buf;
+                buf.setbuf(start, finish - start);
+#if defined(BOOST_NO_STD_LOCALE)
+                std::istream stream(&buf);
+#else
+                std::basic_istream<CharT, Traits> stream(&buf);
+#endif // BOOST_NO_STD_LOCALE
+#endif // BOOST_NO_STRINGSTREAM
+
+#ifndef BOOST_NO_EXCEPTIONS
+                stream.exceptions(std::ios::badbit);
+                try {
+#endif
                 stream.unsetf(std::ios::skipws);
                 lcast_set_precision(stream, static_cast<InputStreamable*>(0));
-#if (defined _MSC_VER)
-# pragma warning( pop )
-#endif
+
                 return stream >> output &&
                     stream.get() ==
 #if defined(__GNUC__) && (__GNUC__<3) && defined(BOOST_NO_STD_WSTRING)
@@ -1543,16 +2024,22 @@ namespace boost
 #else
                 Traits::eof();
 #endif
+
+#ifndef BOOST_NO_EXCEPTIONS
+                } catch (const ::std::ios_base::failure& /*f*/) {
+                    return false;
+                }
+#endif
             }
 
             template<class T>
             inline bool shr_xchar(T& output)
             {
                 BOOST_STATIC_ASSERT_MSG(( sizeof(CharT) == sizeof(T) ),
-                    "boost::lexical_cast does not support conversions from whar_t to char types."
+                    "boost::lexical_cast does not support narrowing of character types."
                     "Use boost::locale instead" );
                 bool const ok = (finish - start == 1);
-                if(ok) {
+                if (ok) {
                     CharT out;
                     Traits::assign(out, *start);
                     output = static_cast<T>(out);
@@ -1561,7 +2048,7 @@ namespace boost
             }
 
 /************************************ OPERATORS >> ( ... ) ********************************/
-            public:
+        public:
             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); }
@@ -1575,37 +2062,93 @@ namespace boost
             bool operator>>(unsigned __int64& output)           { return shr_unsigned(output); }
             bool operator>>(__int64& output)                    { return shr_signed(output); }
 #endif
+
+#ifdef BOOST_HAS_INT128
+            bool operator>>(boost::uint128_type& output)        { return shr_unsigned(output); }
+            bool operator>>(boost::int128_type& output)         { return shr_signed(output); }
+#endif
+
             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
+#if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
             bool operator>>(char16_t& output)                   { return shr_xchar(output); }
 #endif
-#ifndef BOOST_NO_CHAR32_T
+#if !defined(BOOST_NO_CXX11_CHAR32_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
             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
-            bool operator>>(std::wstring& str)                  { str.assign(start, finish); return true; }
-#   endif
-#else
             template<class Alloc>
             bool operator>>(std::basic_string<CharT,Traits,Alloc>& str) { str.assign(start, finish); return true; }
-#if !defined(__SUNPRO_CC)
+
             template<class Alloc>
-            bool operator>>(::boost::container::basic_string<CharT,Traits,Alloc>& str) { str.assign(start, finish); return true; }
-#endif // !defined(__SUNPRO_CC)
+            bool operator>>(boost::container::basic_string<CharT,Traits,Alloc>& str) { str.assign(start, finish); return true; }
+
+            
+    private:
+            template <std::size_t N, class ArrayT>
+            bool shr_std_array(ArrayT& output) BOOST_NOEXCEPT
+            {
+                using namespace std;
+                const std::size_t size = finish - start;
+                if (size > N - 1) { // `-1` because we need to store \0 at the end 
+                    return false;
+                }
+
+                memcpy(&output[0], start, size * sizeof(CharT));
+                output[size] = Traits::to_char_type(0);
+                return true;
+            }
+
+    public:
+
+            template <std::size_t N>
+            bool operator>>(boost::array<CharT, N>& output) BOOST_NOEXCEPT
+            { 
+                return shr_std_array<N>(output); 
+            }
+
+            template <std::size_t N>
+            bool operator>>(boost::array<unsigned char, N>& output)   
+            { 
+                return ((*this) >> reinterpret_cast<boost::array<char, N>& >(output)); 
+            }
+
+            template <std::size_t N>
+            bool operator>>(boost::array<signed char, N>& output)   
+            { 
+                return ((*this) >> reinterpret_cast<boost::array<char, N>& >(output)); 
+            }
+#ifndef BOOST_NO_CXX11_HDR_ARRAY
+            template <std::size_t N>
+            bool operator>>(std::array<CharT, N>& output) BOOST_NOEXCEPT
+            { 
+                return shr_std_array<N>(output); 
+            }
+
+            template <std::size_t N>
+            bool operator>>(std::array<unsigned char, N>& output)   
+            { 
+                return ((*this) >> reinterpret_cast<std::array<char, N>& >(output)); 
+            }
+
+            template <std::size_t N>
+            bool operator>>(std::array<signed char, N>& output)
+            { 
+                return ((*this) >> reinterpret_cast<std::array<char, N>& >(output)); 
+            }
 #endif
+
+
             /*
              * case "-0" || "0" || "+0" :   output = false; return true;
              * case "1" || "+1":            output = true;  return true;
              * default:                     return false;
              */
-            bool operator>>(bool& output)
+            bool operator>>(bool& output) BOOST_NOEXCEPT
             {
                 CharT const zero = lcast_char_constants<CharT>::zero;
                 CharT const plus = lcast_char_constants<CharT>::plus;
@@ -1655,10 +2198,10 @@ namespace boost
                 CharT const lowercase_e = lcast_char_constants<CharT>::lowercase_e;
                 if ( return_value &&
                      (
-                        *(finish-1) == lowercase_e                   // 1.0e
-                        || *(finish-1) == capital_e                  // 1.0E
-                        || *(finish-1) == minus                      // 1.0e- or 1.0E-
-                        || *(finish-1) == plus                       // 1.0e+ or 1.0E+
+                        Traits::eq(*(finish-1), lowercase_e)                   // 1.0e
+                        || Traits::eq(*(finish-1), capital_e)                  // 1.0E
+                        || Traits::eq(*(finish-1), minus)                      // 1.0e- or 1.0E-
+                        || Traits::eq(*(finish-1), plus)                       // 1.0e+ or 1.0E+
                      )
                 ) return false;
 
@@ -1680,10 +2223,10 @@ namespace boost
                  * double, because it will give a big precision loss.
                  * */
                 boost::mpl::if_c<
-#if defined(BOOST_HAS_LONG_LONG) || defined(BOOST_HAS_MS_INT64)
-                    ::boost::type_traits::ice_eq< sizeof(double), sizeof(long double) >::value,
+#if (defined(BOOST_HAS_LONG_LONG) || defined(BOOST_HAS_MS_INT64)) && !defined(BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS)
+                    boost::type_traits::ice_eq< sizeof(double), sizeof(long double) >::value,
 #else
-                     0
+                     1,
 #endif
                     int,
                     char
@@ -1705,24 +2248,8 @@ namespace boost
         };
     }
 
-#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
-
-    // call-by-const reference version
-
     namespace detail
     {
-        template<class T>
-        struct array_to_pointer_decay
-        {
-            typedef T type;
-        };
-
-        template<class T, std::size_t N>
-        struct array_to_pointer_decay<T[N]>
-        {
-            typedef const T * type;
-        };
-
         template<typename T>
         struct is_stdstring
         {
@@ -1734,62 +2261,25 @@ namespace boost
         {
             BOOST_STATIC_CONSTANT(bool, value = true );
         };
-#if !defined(__SUNPRO_CC)
+
         template<typename CharT, typename Traits, typename Alloc>
-        struct is_stdstring< ::boost::container::basic_string<CharT, Traits, Alloc> >
+        struct is_stdstring< boost::container::basic_string<CharT, Traits, Alloc> >
         {
             BOOST_STATIC_CONSTANT(bool, value = true );
         };
-#endif // !defined(__SUNPRO_CC)
-        template<typename T>
-        struct is_char_or_wchar
-        {
-        private:
-#ifndef BOOST_LCAST_NO_WCHAR_T
-            typedef wchar_t wchar_t_if_supported;
-#else
-            typedef char wchar_t_if_supported;
-#endif
-
-#ifndef BOOST_NO_CHAR16_T
-            typedef char16_t char16_t_if_supported;
-#else
-            typedef char char16_t_if_supported;
-#endif
-
-#ifndef BOOST_NO_CHAR32_T
-            typedef char32_t char32_t_if_supported;
-#else
-            typedef char char32_t_if_supported;
-#endif
-            public:
-
-            BOOST_STATIC_CONSTANT(bool, value =
-                    (
-                    ::boost::type_traits::ice_or<
-                         is_same< T, char >::value,
-                         is_same< T, wchar_t_if_supported >::value,
-                         is_same< T, char16_t_if_supported >::value,
-                         is_same< T, char32_t_if_supported >::value,
-                         is_same< T, unsigned char >::value,
-                         is_same< T, signed char >::value
-                    >::value
-                    )
-            );
-        };
 
         template<typename Target, typename Source>
         struct is_arithmetic_and_not_xchars
         {
             BOOST_STATIC_CONSTANT(bool, value =
                (
-                   ::boost::type_traits::ice_and<
-                           is_arithmetic<Source>::value,
-                           is_arithmetic<Target>::value,
-                           ::boost::type_traits::ice_not<
+                   boost::type_traits::ice_and<
+                           boost::is_arithmetic<Source>::value,
+                           boost::is_arithmetic<Target>::value,
+                           boost::type_traits::ice_not<
                                 detail::is_char_or_wchar<Target>::value
                            >::value,
-                           ::boost::type_traits::ice_not<
+                           boost::type_traits::ice_not<
                                 detail::is_char_or_wchar<Source>::value
                            >::value
                    >::value
@@ -1807,14 +2297,14 @@ namespace boost
         {
             BOOST_STATIC_CONSTANT(bool, value =
                 (
-                    ::boost::type_traits::ice_or<
-                        ::boost::type_traits::ice_and<
+                    boost::type_traits::ice_or<
+                        boost::type_traits::ice_and<
                              is_same<Source,Target>::value,
                              is_char_or_wchar<Target>::value
                         >::value,
-                        ::boost::type_traits::ice_and<
-                             ::boost::type_traits::ice_eq< sizeof(char),sizeof(Target)>::value,
-                             ::boost::type_traits::ice_eq< sizeof(char),sizeof(Source)>::value,
+                        boost::type_traits::ice_and<
+                             boost::type_traits::ice_eq< sizeof(char),sizeof(Target)>::value,
+                             boost::type_traits::ice_eq< sizeof(char),sizeof(Source)>::value,
                              is_char_or_wchar<Target>::value,
                              is_char_or_wchar<Source>::value
                         >::value
@@ -1840,19 +2330,18 @@ namespace boost
         {
             BOOST_STATIC_CONSTANT(bool, value = true );
         };
-#if !defined(__SUNPRO_CC)
+
         template<typename CharT, typename Traits, typename Alloc>
-        struct is_char_array_to_stdstring< ::boost::container::basic_string<CharT, Traits, Alloc>, CharT* >
+        struct is_char_array_to_stdstring< boost::container::basic_string<CharT, Traits, Alloc>, CharT* >
         {
             BOOST_STATIC_CONSTANT(bool, value = true );
         };
 
         template<typename CharT, typename Traits, typename Alloc>
-        struct is_char_array_to_stdstring< ::boost::container::basic_string<CharT, Traits, Alloc>, const CharT* >
+        struct is_char_array_to_stdstring< boost::container::basic_string<CharT, Traits, Alloc>, const CharT* >
         {
             BOOST_STATIC_CONSTANT(bool, value = true );
         };
-#endif // !defined(__SUNPRO_CC)
 
 #if (defined _MSC_VER)
 # pragma warning( push )
@@ -1865,60 +2354,26 @@ namespace boost
         {
             static inline Target lexical_cast_impl(const Source& arg)
             {
-                typedef BOOST_DEDUCED_TYPENAME detail::array_to_pointer_decay<Source>::type src;
-
-                typedef BOOST_DEDUCED_TYPENAME detail::widest_char<
-                    BOOST_DEDUCED_TYPENAME detail::stream_char<Target>::type
-                    , BOOST_DEDUCED_TYPENAME detail::stream_char<src>::type
-                >::type char_type;
-
-                typedef detail::lcast_src_length<src> lcast_src_length;
-                std::size_t const src_len = lcast_src_length::value;
-                char_type buf[src_len + 1];
-                lcast_src_length::check_coverage();
-
-                typedef BOOST_DEDUCED_TYPENAME
-                    deduce_char_traits<char_type,Target,Source>::type traits;
-
-                typedef BOOST_DEDUCED_TYPENAME remove_pointer<src >::type removed_ptr_t;
-
-                // is_char_types_match variable value can be computed via
-                // sizeof(char_type) == sizeof(removed_ptr_t). But when
-                // removed_ptr_t is an incomplete type or void*, compilers
-                // produce warnings or errors.
-                const bool is_char_types_match =
-                (::boost::type_traits::ice_or<
-                    ::boost::type_traits::ice_and<
-                        ::boost::type_traits::ice_eq<sizeof(char_type), sizeof(char) >::value,
-                        ::boost::type_traits::ice_or<
-                            ::boost::is_same<char, removed_ptr_t>::value,
-                            ::boost::is_same<unsigned char, removed_ptr_t>::value,
-                            ::boost::is_same<signed char, removed_ptr_t>::value
-                        >::value
-                    >::value,
-                    is_same<char_type, removed_ptr_t>::value
-                >::value);
-
-                const bool requires_stringbuf =
-                        !(
-                             ::boost::type_traits::ice_or<
-                                 is_stdstring<src >::value,
-                                 is_arithmetic<src >::value,
-                                 ::boost::type_traits::ice_and<
-                                     is_pointer<src >::value,
-                                     is_char_or_wchar<removed_ptr_t >::value,
-                                     is_char_types_match
-                                 >::value
-                             >::value
-                        );
-
-                detail::lexical_stream_limited_src<char_type,traits, requires_stringbuf >
-                        interpreter(buf, buf + src_len);
-
-                Target result;
+                typedef lexical_cast_stream_traits<Source, Target>  stream_trait;
+                
+                typedef detail::lexical_stream_limited_src<
+                    BOOST_DEDUCED_TYPENAME stream_trait::char_type, 
+                    BOOST_DEDUCED_TYPENAME stream_trait::traits, 
+                    stream_trait::requires_stringbuf 
+                > interpreter_type;
+
+                // Target type must be default constructible
+                Target result;               
+
+                BOOST_DEDUCED_TYPENAME stream_trait::char_type buf[stream_trait::len_t::value + 1];
+                stream_trait::len_t::check_coverage();
+
+                interpreter_type interpreter(buf, buf + stream_trait::len_t::value + 1);
+
                 // Disabling ADL, by directly specifying operators.
                 if(!(interpreter.operator <<(arg) && interpreter.operator >>(result)))
                   BOOST_LCAST_THROW_BAD_CAST(Source, Target);
+
                 return result;
             }
         };
@@ -1926,88 +2381,89 @@ namespace boost
 # pragma warning( pop )
 #endif
 
-        template<typename Source>
+        template <typename Source>
         struct lexical_cast_copy
         {
-            static inline Source lexical_cast_impl(const Source &arg)
+            static inline const Source& lexical_cast_impl(const Source &arg) BOOST_NOEXCEPT
             {
                 return arg;
             }
         };
 
-        class precision_loss_error : public boost::numeric::bad_numeric_cast
-        {
-         public:
-            virtual const char * what() const throw()
-             {  return "bad numeric conversion: precision loss error"; }
-        };
-
-        template<class S >
-        struct throw_on_precision_loss
+        template <class Source, class Target >
+        struct detect_precision_loss
         {
-         typedef boost::numeric::Trunc<S> Rounder;
-         typedef S source_type ;
+         typedef boost::numeric::Trunc<Source> Rounder;
+         typedef Source source_type ;
 
-         typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
+         typedef BOOST_DEDUCED_TYPENAME mpl::if_<
+            boost::is_arithmetic<Source>, Source, Source const&
+          >::type argument_type ;
 
          static source_type nearbyint ( argument_type s )
          {
-            source_type orig_div_round = s / Rounder::nearbyint(s);
+            const source_type near_int = Rounder::nearbyint(s);
+            if (near_int) {
+                const source_type orig_div_round = s / near_int;
+                const source_type eps = std::numeric_limits<source_type>::epsilon();
+
+                if ((orig_div_round > 1 ? orig_div_round - 1 : 1 - orig_div_round) > eps)
+                    BOOST_LCAST_THROW_BAD_CAST(Source, Target);
+            }
 
-            if ( (orig_div_round > 1 ? orig_div_round - 1 : 1 - orig_div_round) > std::numeric_limits<source_type>::epsilon() )
-               BOOST_THROW_EXCEPTION( precision_loss_error() );
             return s ;
          }
 
          typedef typename Rounder::round_style round_style;
         } ;
 
-        template<typename Target, typename Source>
+        template <class Source, class Target >
+        struct nothrow_overflow_handler
+        {
+          void operator() ( boost::numeric::range_check_result r )
+          {
+            if (r != boost::numeric::cInRange)
+                BOOST_LCAST_THROW_BAD_CAST(Source, Target);
+          }
+        } ;
+
+        template <typename Target, typename Source>
         struct lexical_cast_dynamic_num_not_ignoring_minus
         {
             static inline Target lexical_cast_impl(const Source &arg)
             {
-                try{
-                    typedef boost::numeric::converter<
-                            Target,
-                            Source,
-                            boost::numeric::conversion_traits<Target,Source>,
-                            boost::numeric::def_overflow_handler,
-                            throw_on_precision_loss<Source>
-                    > Converter ;
-
-                    return Converter::convert(arg);
-                } catch( ::boost::numeric::bad_numeric_cast const& ) {
-                    BOOST_LCAST_THROW_BAD_CAST(Source, Target);
-                }
-                BOOST_UNREACHABLE_RETURN(static_cast<Target>(0));
+                return boost::numeric::converter<
+                        Target,
+                        Source,
+                        boost::numeric::conversion_traits<Target,Source>,
+                        nothrow_overflow_handler<Source, Target>,
+                        detect_precision_loss<Source, Target>
+                >::convert(arg);
             }
         };
 
-        template<typename Target, typename Source>
+        template <typename Target, typename Source>
         struct lexical_cast_dynamic_num_ignoring_minus
         {
             static inline Target lexical_cast_impl(const Source &arg)
             {
-                try{
-                    typedef boost::numeric::converter<
-                            Target,
-                            Source,
-                            boost::numeric::conversion_traits<Target,Source>,
-                            boost::numeric::def_overflow_handler,
-                            throw_on_precision_loss<Source>
-                    > Converter ;
-
-                    bool has_minus = ( arg < 0);
-                    if ( has_minus ) {
-                        return static_cast<Target>(-Converter::convert(-arg));
-                    } else {
-                        return Converter::convert(arg);
-                    }
-                } catch( ::boost::numeric::bad_numeric_cast const& ) {
-                    BOOST_LCAST_THROW_BAD_CAST(Source, Target);
-                }
-                BOOST_UNREACHABLE_RETURN(static_cast<Target>(0));
+                typedef BOOST_DEDUCED_TYPENAME boost::mpl::eval_if_c<
+                        boost::is_float<Source>::value,
+                        boost::mpl::identity<Source>,
+                        boost::make_unsigned<Source>
+                >::type usource_t;
+
+                typedef boost::numeric::converter<
+                        Target,
+                        usource_t,
+                        boost::numeric::conversion_traits<Target,usource_t>,
+                        nothrow_overflow_handler<usource_t, Target>,
+                        detect_precision_loss<usource_t, Target>
+                > converter_t;
+
+                return (
+                    arg < 0 ? static_cast<Target>(0u - converter_t::convert(0u - arg)) : converter_t::convert(arg)
+                );
             }
         };
 
@@ -2029,24 +2485,24 @@ namespace boost
          * optional, so if a negative number is read, no errors will arise
          * and the result will be the two's complement.
          */
-        template<typename Target, typename Source>
+        template <typename Target, typename Source>
         struct lexical_cast_dynamic_num
         {
             static inline Target lexical_cast_impl(const Source &arg)
             {
-                typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
-                    ::boost::type_traits::ice_and<
-                        ::boost::type_traits::ice_or<
-                            ::boost::is_signed<Source>::value,
-                            ::boost::is_float<Source>::value
+                typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
+                    boost::type_traits::ice_and<
+                        boost::type_traits::ice_or<
+                            boost::is_signed<Source>::value,
+                            boost::is_float<Source>::value
                         >::value,
-                        ::boost::type_traits::ice_not<
-                            is_same<Source, bool>::value
+                        boost::type_traits::ice_not<
+                            boost::is_same<Source, bool>::value
                         >::value,
-                        ::boost::type_traits::ice_not<
-                            is_same<Target, bool>::value
+                        boost::type_traits::ice_not<
+                            boost::is_same<Target, bool>::value
                         >::value,
-                        ::boost::is_unsigned<Target>::value
+                        boost::is_unsigned<Target>::value
                     >::value,
                     lexical_cast_dynamic_num_ignoring_minus<Target, Source>,
                     lexical_cast_dynamic_num_not_ignoring_minus<Target, Source>
@@ -2057,40 +2513,133 @@ namespace boost
         };
     }
 
-    template<typename Target, typename Source>
+    template <typename Target, typename Source>
     inline Target lexical_cast(const Source &arg)
     {
-        typedef BOOST_DEDUCED_TYPENAME detail::array_to_pointer_decay<Source>::type src;
-
-        typedef BOOST_DEDUCED_TYPENAME ::boost::type_traits::ice_or<
-                detail::is_xchar_to_xchar<Target, src>::value,
-                detail::is_char_array_to_stdstring<Target,src>::value,
-                ::boost::type_traits::ice_and<
-                     is_same<Target, src>::value,
-                     detail::is_stdstring<Target>::value
+        typedef BOOST_DEDUCED_TYPENAME boost::detail::array_to_pointer_decay<Source>::type src;
+
+        typedef BOOST_DEDUCED_TYPENAME boost::type_traits::ice_or<
+                boost::detail::is_xchar_to_xchar<Target, src >::value,
+                boost::detail::is_char_array_to_stdstring<Target, src >::value,
+                boost::type_traits::ice_and<
+                     boost::is_same<Target, src >::value,
+                     boost::detail::is_stdstring<Target >::value
                 >::value
-        > do_copy_type;
+        > shall_we_copy_t;
 
         typedef BOOST_DEDUCED_TYPENAME
-                detail::is_arithmetic_and_not_xchars<Target, src> do_copy_with_dynamic_check_type;
-
-        typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
-            do_copy_type::value,
-            detail::lexical_cast_copy<src>,
-            BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
-                 do_copy_with_dynamic_check_type::value,
-                 detail::lexical_cast_dynamic_num<Target, src>,
-                 detail::lexical_cast_do_cast<Target, src>
+                boost::detail::is_arithmetic_and_not_xchars<Target, src > shall_we_copy_with_dynamic_check_t;
+
+        typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
+            shall_we_copy_t::value,
+            boost::detail::lexical_cast_copy<src >,
+            BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
+                 shall_we_copy_with_dynamic_check_t::value,
+                 boost::detail::lexical_cast_dynamic_num<Target, src >,
+                 boost::detail::lexical_cast_do_cast<Target, src >
             >::type
         >::type caster_type;
 
         return caster_type::lexical_cast_impl(arg);
     }
 
-    #else
+    template <typename Target>
+    inline Target lexical_cast(const char* chars, std::size_t count)
+     {
+        return ::boost::lexical_cast<Target>(
+            ::boost::iterator_range<const char*>(chars, chars + count)
+        );
+    }
+
 
-    namespace detail // stream wrapper for handling lexical conversions
+    template <typename Target>
+    inline Target lexical_cast(const unsigned char* chars, std::size_t count)
     {
+         return ::boost::lexical_cast<Target>(
+            ::boost::iterator_range<const unsigned char*>(chars, chars + count)
+         );
+     }
+
+    template <typename Target>
+    inline Target lexical_cast(const signed char* chars, std::size_t count)
+    {
+        return ::boost::lexical_cast<Target>(
+            ::boost::iterator_range<const signed char*>(chars, chars + count)
+        );
+    }
+
+#ifndef BOOST_LCAST_NO_WCHAR_T
+    template <typename Target>
+    inline Target lexical_cast(const wchar_t* chars, std::size_t count)
+    {
+        return ::boost::lexical_cast<Target>(
+            ::boost::iterator_range<const wchar_t*>(chars, chars + count)
+        );
+    }
+#endif
+#ifndef BOOST_NO_CXX11_CHAR16_T
+    template <typename Target>
+    inline Target lexical_cast(const char16_t* chars, std::size_t count)
+    {
+        return ::boost::lexical_cast<Target>(
+            ::boost::iterator_range<const char16_t*>(chars, chars + count)
+        );
+    }
+#endif
+#ifndef BOOST_NO_CXX11_CHAR32_T
+    template <typename Target>
+    inline Target lexical_cast(const char32_t* chars, std::size_t count)
+    {
+        return ::boost::lexical_cast<Target>(
+            ::boost::iterator_range<const char32_t*>(chars, chars + count)
+        );
+    }
+#endif
+
+} // namespace boost
+
+#else // #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
+
+namespace boost {
+    namespace detail
+    {
+
+        // selectors for choosing stream character type
+        template<typename Type>
+        struct stream_char
+        {
+            typedef char type;
+        };
+
+#ifndef BOOST_LCAST_NO_WCHAR_T
+#ifndef BOOST_NO_INTRINSIC_WCHAR_T
+        template<>
+        struct stream_char<wchar_t>
+        {
+            typedef wchar_t type;
+        };
+#endif
+
+        template<>
+        struct stream_char<wchar_t *>
+        {
+            typedef wchar_t type;
+        };
+
+        template<>
+        struct stream_char<const wchar_t *>
+        {
+            typedef wchar_t type;
+        };
+
+        template<>
+        struct stream_char<std::wstring>
+        {
+            typedef wchar_t type;
+        };
+#endif
+
+        // stream wrapper for handling lexical conversions
         template<typename Target, typename Source, typename Traits>
         class lexical_stream
         {
@@ -2180,16 +2729,20 @@ namespace boost
         return result;
     }
 
-    #endif
-}
+} // namespace boost
+
+#endif
 
 // Copyright Kevlin Henney, 2000-2005.
 // Copyright Alexander Nasonov, 2006-2010.
-// Copyright Antony Polukhin, 2011-2012.
+// Copyright Antony Polukhin, 2011-2013.
 //
 // 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)
 
+#undef BOOST_LCAST_THROW_BAD_CAST
 #undef BOOST_LCAST_NO_WCHAR_T
-#endif
+
+#endif // BOOST_LEXICAL_CAST_INCLUDED
+