1 #ifndef BOOST_LEXICAL_CAST_INCLUDED
2 #define BOOST_LEXICAL_CAST_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
10 // Boost lexical_cast.hpp header -------------------------------------------//
12 // See http://www.boost.org/libs/conversion for documentation.
13 // See end of this header for rights and permissions.
15 // what: lexical_cast custom keyword cast
16 // who: contributed by Kevlin Henney,
17 // enhanced with contributions from Terje Slettebo,
18 // with additional fixes and suggestions from Gennaro Prota,
19 // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
20 // Alexander Nasonov, Antony Polukhin and other Boosters
21 // when: November 2000, March 2003, June 2005, June 2006, March 2011
32 #include <boost/config.hpp>
33 #include <boost/limits.hpp>
34 #include <boost/mpl/if.hpp>
35 #include <boost/throw_exception.hpp>
36 #include <boost/type_traits/is_pointer.hpp>
37 #include <boost/type_traits/is_integral.hpp>
38 #include <boost/type_traits/is_arithmetic.hpp>
39 #include <boost/type_traits/remove_pointer.hpp>
40 #include <boost/numeric/conversion/cast.hpp>
41 #include <boost/type_traits/ice.hpp>
42 #include <boost/type_traits/make_unsigned.hpp>
43 #include <boost/type_traits/is_signed.hpp>
44 #include <boost/math/special_functions/sign.hpp>
45 #include <boost/math/special_functions/fpclassify.hpp>
46 #include <boost/static_assert.hpp>
47 #include <boost/detail/lcast_precision.hpp>
48 #include <boost/detail/workaround.hpp>
52 #ifndef BOOST_NO_STD_LOCALE
55 # ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
56 # warning "Unable to use <locale> header. boost::lexical_cast will use the 'C' locale."
57 # define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
61 #ifdef BOOST_NO_STRINGSTREAM
67 #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
68 #define BOOST_LCAST_NO_WCHAR_T
71 #ifdef BOOST_NO_TYPEID
72 #define BOOST_LCAST_THROW_BAD_CAST(S, T) throw_exception(bad_lexical_cast())
74 #define BOOST_LCAST_THROW_BAD_CAST(Source, Target) \
75 throw_exception(bad_lexical_cast(typeid(Source), typeid(Target)))
80 // exception used to indicate runtime lexical_cast failure
81 class bad_lexical_cast :
82 // workaround MSVC bug with std::bad_cast when _HAS_EXCEPTIONS == 0
83 #if defined(BOOST_MSVC) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
89 #if defined(__BORLANDC__) && BOOST_WORKAROUND( __BORLANDC__, < 0x560 )
90 // under bcc32 5.5.1 bad_cast doesn't derive from exception
91 , public std::exception
97 #ifndef BOOST_NO_TYPEID
98 source(&typeid(void)), target(&typeid(void))
100 source(0), target(0) // this breaks getters
106 const std::type_info &source_type_arg,
107 const std::type_info &target_type_arg) :
108 source(&source_type_arg), target(&target_type_arg)
112 const std::type_info &source_type() const
116 const std::type_info &target_type() const
121 virtual const char *what() const throw()
123 return "bad lexical cast: "
124 "source type value could not be interpreted as target";
126 virtual ~bad_lexical_cast() throw()
130 const std::type_info *source;
131 const std::type_info *target;
134 namespace detail // selectors for choosing stream character type
136 template<typename Type>
142 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
143 template<class CharT, class Traits, class Alloc>
144 struct stream_char< std::basic_string<CharT,Traits,Alloc> >
150 #ifndef BOOST_LCAST_NO_WCHAR_T
151 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
153 struct stream_char<wchar_t>
155 typedef wchar_t type;
160 struct stream_char<wchar_t *>
162 typedef wchar_t type;
166 struct stream_char<const wchar_t *>
168 typedef wchar_t type;
171 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
173 struct stream_char<std::wstring>
175 typedef wchar_t type;
181 #ifndef BOOST_NO_CHAR16_T
184 struct stream_char<char16_t>
186 typedef char16_t type;
190 struct stream_char<char16_t *>
192 typedef char16_t type;
196 struct stream_char<const char16_t *>
198 typedef char16_t type;
203 #ifndef BOOST_NO_CHAR32_T
206 struct stream_char<char32_t>
208 typedef char32_t type;
212 struct stream_char<char32_t *>
214 typedef char32_t type;
218 struct stream_char<const char32_t *>
220 typedef char32_t type;
225 template<typename TargetChar, typename SourceChar>
228 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
229 (sizeof(TargetChar) > sizeof(SourceChar))
231 , SourceChar >::type type;
235 namespace detail // deduce_char_traits template
237 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
238 template<class CharT, class Target, class Source>
239 struct deduce_char_traits
241 typedef std::char_traits<CharT> type;
244 template<class CharT, class Traits, class Alloc, class Source>
245 struct deduce_char_traits< CharT
246 , std::basic_string<CharT,Traits,Alloc>
253 template<class CharT, class Target, class Traits, class Alloc>
254 struct deduce_char_traits< CharT
256 , std::basic_string<CharT,Traits,Alloc>
262 template<class CharT, class Traits, class Alloc1, class Alloc2>
263 struct deduce_char_traits< CharT
264 , std::basic_string<CharT,Traits,Alloc1>
265 , std::basic_string<CharT,Traits,Alloc2>
273 namespace detail // lcast_src_length
275 // Return max. length of string representation of Source;
276 template< class Source // Source type of lexical_cast.
278 struct lcast_src_length
280 BOOST_STATIC_CONSTANT(std::size_t, value = 1);
281 // To check coverage, build the test with
282 // bjam --v2 profile optimization=off
283 static void check_coverage() {}
286 // Helper for integral types.
287 // Notes on length calculation:
288 // Max length for 32bit int with grouping "\1" and thousands_sep ',':
289 // "-2,1,4,7,4,8,3,6,4,7"
291 // ^ - 1 digit not counted by digits10
292 // ^^^^^^^^^^^^^^^^^^ - digits10 * 2
294 // Constant is_specialized is used instead of constant 1
295 // to prevent buffer overflow in a rare case when
296 // <boost/limits.hpp> doesn't add missing specialization for
297 // numeric_limits<T> for some integral type T.
298 // When is_specialized is false, the whole expression is 0.
299 template<class Source>
300 struct lcast_src_length_integral
302 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
303 BOOST_STATIC_CONSTANT(std::size_t, value =
304 std::numeric_limits<Source>::is_signed +
305 std::numeric_limits<Source>::is_specialized + /* == 1 */
306 std::numeric_limits<Source>::digits10 * 2
309 BOOST_STATIC_CONSTANT(std::size_t, value = 156);
310 BOOST_STATIC_ASSERT(sizeof(Source) * CHAR_BIT <= 256);
313 // TODO: FIX for char16_t, char32_t, we can ignore CharT
314 #define BOOST_LCAST_DEF(T) \
315 template<> struct lcast_src_length<T> \
316 : lcast_src_length_integral<T> \
317 { static void check_coverage() {} };
319 BOOST_LCAST_DEF(short)
320 BOOST_LCAST_DEF(unsigned short)
322 BOOST_LCAST_DEF(unsigned int)
323 BOOST_LCAST_DEF(long)
324 BOOST_LCAST_DEF(unsigned long)
325 #if defined(BOOST_HAS_LONG_LONG)
326 BOOST_LCAST_DEF(boost::ulong_long_type)
327 BOOST_LCAST_DEF(boost::long_long_type )
328 #elif defined(BOOST_HAS_MS_INT64)
329 BOOST_LCAST_DEF(unsigned __int64)
330 BOOST_LCAST_DEF( __int64)
333 #undef BOOST_LCAST_DEF
335 #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
336 // Helper for floating point types.
337 // -1.23456789e-123456
341 // ^^^^^^^^ lcast_precision<Source>::value
344 // ^^^^^^ exponent (assumed 6 or less digits)
345 // sign + leading digit + decimal point + "e" + exponent sign == 5
346 template<class Source>
347 struct lcast_src_length_floating
350 std::numeric_limits<Source>::max_exponent10 <= 999999L &&
351 std::numeric_limits<Source>::min_exponent10 >= -999999L
353 BOOST_STATIC_CONSTANT(std::size_t, value =
354 5 + lcast_precision<Source>::value + 6
359 struct lcast_src_length<float>
360 : lcast_src_length_floating<float>
362 static void check_coverage() {}
366 struct lcast_src_length<double>
367 : lcast_src_length_floating<double>
369 static void check_coverage() {}
373 struct lcast_src_length<long double>
374 : lcast_src_length_floating<long double>
376 static void check_coverage() {}
379 #endif // #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
382 namespace detail // '0', '+' and '-' constants
384 template<typename CharT> struct lcast_char_constants;
387 struct lcast_char_constants<char>
389 BOOST_STATIC_CONSTANT(char, zero = '0');
390 BOOST_STATIC_CONSTANT(char, minus = '-');
391 BOOST_STATIC_CONSTANT(char, plus = '+');
392 BOOST_STATIC_CONSTANT(char, lowercase_e = 'e');
393 BOOST_STATIC_CONSTANT(char, capital_e = 'E');
394 BOOST_STATIC_CONSTANT(char, c_decimal_separator = '.');
397 #ifndef BOOST_LCAST_NO_WCHAR_T
399 struct lcast_char_constants<wchar_t>
401 BOOST_STATIC_CONSTANT(wchar_t, zero = L'0');
402 BOOST_STATIC_CONSTANT(wchar_t, minus = L'-');
403 BOOST_STATIC_CONSTANT(wchar_t, plus = L'+');
404 BOOST_STATIC_CONSTANT(wchar_t, lowercase_e = L'e');
405 BOOST_STATIC_CONSTANT(wchar_t, capital_e = L'E');
406 BOOST_STATIC_CONSTANT(wchar_t, c_decimal_separator = L'.');
410 #ifndef BOOST_NO_CHAR16_T
412 struct lcast_char_constants<char16_t>
414 BOOST_STATIC_CONSTANT(char16_t, zero = u'0');
415 BOOST_STATIC_CONSTANT(char16_t, minus = u'-');
416 BOOST_STATIC_CONSTANT(char16_t, plus = u'+');
417 BOOST_STATIC_CONSTANT(char16_t, lowercase_e = u'e');
418 BOOST_STATIC_CONSTANT(char16_t, capital_e = u'E');
419 BOOST_STATIC_CONSTANT(char16_t, c_decimal_separator = u'.');
423 #ifndef BOOST_NO_CHAR32_T
425 struct lcast_char_constants<char32_t>
427 BOOST_STATIC_CONSTANT(char32_t, zero = U'0');
428 BOOST_STATIC_CONSTANT(char32_t, minus = U'-');
429 BOOST_STATIC_CONSTANT(char32_t, plus = U'+');
430 BOOST_STATIC_CONSTANT(char32_t, lowercase_e = U'e');
431 BOOST_STATIC_CONSTANT(char32_t, capital_e = U'E');
432 BOOST_STATIC_CONSTANT(char32_t, c_decimal_separator = U'.');
437 namespace detail // lcast_to_unsigned
439 #if (defined _MSC_VER)
440 # pragma warning( push )
441 // C4146: unary minus operator applied to unsigned type, result still unsigned
442 # pragma warning( disable : 4146 )
443 #elif defined( __BORLANDC__ )
444 # pragma option push -w-8041
448 BOOST_DEDUCED_TYPENAME make_unsigned<T>::type lcast_to_unsigned(T value)
450 typedef BOOST_DEDUCED_TYPENAME make_unsigned<T>::type result_type;
451 result_type uvalue = static_cast<result_type>(value);
452 return value < 0 ? -uvalue : uvalue;
454 #if (defined _MSC_VER)
455 # pragma warning( pop )
456 #elif defined( __BORLANDC__ )
461 namespace detail // lcast_put_unsigned
463 template<class Traits, class T, class CharT>
464 CharT* lcast_put_unsigned(const T n_param, CharT* finish)
466 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
467 BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
470 typedef typename Traits::int_type int_type;
471 CharT const czero = lcast_char_constants<CharT>::zero;
472 int_type const zero = Traits::to_int_type(czero);
473 BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
474 (sizeof(int_type) > sizeof(T))
479 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
481 if (loc != std::locale::classic()) {
482 typedef std::numpunct<CharT> numpunct;
483 numpunct const& np = BOOST_USE_FACET(numpunct, loc);
484 std::string const grouping = np.grouping();
485 std::string::size_type const grouping_size = grouping.size();
487 if ( grouping_size && grouping[0] > 0 )
490 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
491 // Check that ulimited group is unreachable:
492 BOOST_STATIC_ASSERT(std::numeric_limits<T>::digits10 < CHAR_MAX);
494 CharT thousands_sep = np.thousands_sep();
495 std::string::size_type group = 0; // current group number
496 char last_grp_size = grouping[0];
497 char left = last_grp_size;
504 if(group < grouping_size)
506 char const grp_size = grouping[group];
507 last_grp_size = grp_size <= 0 ? CHAR_MAX : grp_size;
510 left = last_grp_size;
512 Traits::assign(*finish, thousands_sep);
518 int_type const digit = static_cast<int_type>(n % 10U);
519 Traits::assign(*finish, Traits::to_char_type(zero + digit));
530 int_type const digit = static_cast<int_type>(n % 10U);
531 Traits::assign(*finish, Traits::to_char_type(zero + digit));
540 namespace detail // lcast_ret_unsigned
542 template<class Traits, class T, class CharT>
543 inline bool lcast_ret_unsigned(T& value, const CharT* const begin, const CharT* end)
545 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
546 BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
548 typedef typename Traits::int_type int_type;
549 CharT const czero = lcast_char_constants<CharT>::zero;
553 if ( *end < czero || *end >= czero + 10 || begin > end)
555 value = *end - czero;
559 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
561 if (loc != std::locale::classic()) {
562 typedef std::numpunct<CharT> numpunct;
563 numpunct const& np = BOOST_USE_FACET(numpunct, loc);
564 std::string const& grouping = np.grouping();
565 std::string::size_type const grouping_size = grouping.size();
567 /* According to Programming languages - C++
568 * we MUST check for correct grouping
570 if (grouping_size && grouping[0] > 0)
572 unsigned char current_grouping = 0;
573 CharT const thousands_sep = np.thousands_sep();
574 char remained = grouping[current_grouping] - 1;
575 bool shall_we_return = true;
577 for(;end>=begin; --end)
580 T const new_sub_value = multiplier * 10 * (*end - czero);
582 if (*end < czero || *end >= czero + 10
583 /* detecting overflow */
584 || new_sub_value/10 != multiplier * (*end - czero)
585 || static_cast<T>((std::numeric_limits<T>::max)()-new_sub_value) < value
589 value += new_sub_value;
593 if ( !Traits::eq(*end, thousands_sep) ) //|| begin == end ) return false;
596 * According to Programming languages - C++
597 * Digit grouping is checked. That is, the positions of discarded
598 * separators is examined for consistency with
599 * use_facet<numpunct<charT> >(loc ).grouping()
601 * BUT what if there is no separators at all and grouping()
602 * is not empty? Well, we have no extraced separators, so we
603 * won`t check them for consistency. This will allow us to
604 * work with "C" locale from other locales
606 shall_we_return = false;
609 if ( begin == end ) return false;
610 if (current_grouping < grouping_size-1 ) ++current_grouping;
611 remained = grouping[current_grouping];
616 if (shall_we_return) return true;
621 while ( begin <= end )
623 T const new_sub_value = multiplier * 10 * (*end - czero);
625 if (*end < czero || *end >= czero + 10
626 /* detecting overflow */
627 || new_sub_value/10 != multiplier * (*end - czero)
628 || static_cast<T>((std::numeric_limits<T>::max)()-new_sub_value) < value
632 value += new_sub_value;
643 /* Returns true and sets the correct value if found NaN or Inf. */
644 template <class CharT, class T>
645 inline bool parse_inf_nan_impl(const CharT* begin, const CharT* end, T& value
646 , const CharT* lc_NAN, const CharT* lc_nan
647 , const CharT* lc_INFINITY, const CharT* lc_infinity
648 , const CharT opening_brace, const CharT closing_brace)
651 const wchar_t minus = lcast_char_constants<wchar_t>::minus;
652 const wchar_t plus = lcast_char_constants<wchar_t>::plus;
653 const int inifinity_size = 8;
655 bool has_minus = false;
662 else if( *begin == plus ) ++begin;
664 if( end-begin < 3 ) return false;
665 if( !memcmp(begin, lc_nan, 3*sizeof(CharT)) || !memcmp(begin, lc_NAN, 3*sizeof(CharT)) )
668 if (end != begin) /* It is 'nan(...)' or some bad input*/
670 if(end-begin<2) return false; // bad input
672 if( *begin != opening_brace || *end != closing_brace) return false; // bad input
675 if( !has_minus ) value = std::numeric_limits<T>::quiet_NaN();
676 else value = (boost::math::changesign) (std::numeric_limits<T>::quiet_NaN());
679 if (( /* 'INF' or 'inf' */
682 (!memcmp(begin, lc_infinity, 3*sizeof(CharT)) || !memcmp(begin, lc_INFINITY, 3*sizeof(CharT)))
685 ( /* 'INFINITY' or 'infinity' */
686 end-begin==inifinity_size
688 (!memcmp(begin, lc_infinity, inifinity_size)|| !memcmp(begin, lc_INFINITY, inifinity_size))
692 if( !has_minus ) value = std::numeric_limits<T>::infinity();
693 else value = (boost::math::changesign) (std::numeric_limits<T>::infinity());
700 #ifndef BOOST_LCAST_NO_WCHAR_T
702 bool parse_inf_nan(const wchar_t* begin, const wchar_t* end, T& value)
704 return parse_inf_nan_impl(begin, end, value
706 , L"INFINITY", L"infinity"
711 template <class CharT, class T>
712 bool parse_inf_nan(const CharT* begin, const CharT* end, T& value)
714 return parse_inf_nan_impl(begin, end, value
716 , "INFINITY", "infinity"
719 #ifndef BOOST_LCAST_NO_WCHAR_T
721 bool put_inf_nan(wchar_t* begin, wchar_t*& end, const T& value)
724 if ( (boost::math::isnan)(value) )
726 if ( (boost::math::signbit)(value) )
728 memcpy(begin,L"-nan", sizeof(L"-nan"));
732 memcpy(begin,L"nan", sizeof(L"nan"));
736 } else if ( (boost::math::isinf)(value) )
738 if ( (boost::math::signbit)(value) )
740 memcpy(begin,L"-inf", sizeof(L"-inf"));
744 memcpy(begin,L"inf", sizeof(L"inf"));
753 template <class CharT, class T>
754 bool put_inf_nan(CharT* begin, CharT*& end, const T& value)
757 if ( (boost::math::isnan)(value) )
759 if ( (boost::math::signbit)(value) )
761 memcpy(begin,"-nan", sizeof("-nan"));
765 memcpy(begin,"nan", sizeof("nan"));
769 } else if ( (boost::math::isinf)(value) )
771 if ( (boost::math::signbit)(value) )
773 memcpy(begin,"-inf", sizeof("-inf"));
777 memcpy(begin,"inf", sizeof("inf"));
789 namespace detail // lcast_ret_float
792 struct mantissa_holder_type
794 /* Can not be used with this type */
798 struct mantissa_holder_type<float>
800 typedef unsigned int type;
804 struct mantissa_holder_type<double>
806 #if defined(BOOST_HAS_LONG_LONG)
807 typedef boost::ulong_long_type type;
808 #elif defined(BOOST_HAS_MS_INT64)
809 typedef unsigned __int64 type;
813 template<class Traits, class T, class CharT>
814 inline bool lcast_ret_float(T& value, const CharT* begin, const CharT* end)
817 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
819 typedef std::numpunct<CharT> numpunct;
820 numpunct const& np = BOOST_USE_FACET(numpunct, loc);
821 std::string const grouping(
822 (loc == std::locale::classic())
826 std::string::size_type const grouping_size = grouping.size();
827 CharT const thousands_sep = grouping_size ? np.thousands_sep() : 0;
828 CharT const decimal_point = np.decimal_point();
829 bool found_grouping = false;
830 unsigned int last_grouping_pos = grouping_size - 1;
832 CharT const decimal_point = lcast_char_constants<CharT>::c_decimal_separator;
835 CharT const czero = lcast_char_constants<CharT>::zero;
836 CharT const minus = lcast_char_constants<CharT>::minus;
837 CharT const plus = lcast_char_constants<CharT>::plus;
838 CharT const capital_e = lcast_char_constants<CharT>::capital_e;
839 CharT const lowercase_e = lcast_char_constants<CharT>::lowercase_e;
843 if (parse_inf_nan(begin, end, value)) return true;
845 typedef typename Traits::int_type int_type;
846 typedef BOOST_DEDUCED_TYPENAME mantissa_holder_type<T>::type mantissa_type;
847 int_type const zero = Traits::to_int_type(czero);
848 if (begin == end) return false;
850 /* Getting the plus/minus sign */
851 bool has_minus = false;
852 if ( *begin == minus ) {
855 if (begin == end) return false;
856 } else if ( *begin == plus ) {
858 if (begin == end) return false;
861 bool found_decimal = false;
862 bool found_number_before_exp = false;
864 mantissa_type mantissa=0;
865 bool is_mantissa_full = false;
867 char length_since_last_delim = 0;
869 while ( begin != end )
872 /* We allow no thousand_separators after decimal point */
874 mantissa_type tmp_mantissa = mantissa * 10u;
875 if ( *begin == lowercase_e || *begin == capital_e ) break;
876 if ( *begin < czero || *begin >= czero + 10 ) return false;
877 if ( is_mantissa_full
878 || tmp_mantissa / 10u != mantissa
879 || (std::numeric_limits<mantissa_type>::max)()-(*begin - zero) < tmp_mantissa
881 is_mantissa_full = true;
887 mantissa = tmp_mantissa;
888 mantissa += *begin - zero;
890 found_number_before_exp = true;
893 if (*begin >= czero && *begin < czero + 10) {
895 /* Checking for mantissa overflow. If overflow will
896 * occur, them we only increase multiplyer
898 mantissa_type tmp_mantissa = mantissa * 10u;
899 if( !is_mantissa_full
900 && tmp_mantissa / 10u == mantissa
901 && (std::numeric_limits<mantissa_type>::max)()-(*begin - zero) >= tmp_mantissa
904 mantissa = tmp_mantissa;
905 mantissa += *begin - zero;
908 is_mantissa_full = true;
912 found_number_before_exp = true;
913 ++ length_since_last_delim;
914 } else if ( *begin == decimal_point || *begin == lowercase_e || *begin == capital_e) {
915 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
916 /* If ( we need to check grouping
917 * and ( grouping missmatches
918 * or grouping position is incorrect
919 * or we are using the grouping position 0 twice
921 * ) then return error
923 if( grouping_size && found_grouping
925 length_since_last_delim != grouping[0]
926 || last_grouping_pos>1
927 || (last_grouping_pos==0 && grouping_size>1)
932 if(*begin == decimal_point){
934 found_decimal = true;
937 if (!found_number_before_exp) return false;
941 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
942 else if (grouping_size && *begin == thousands_sep){
945 /* It is not he first time, when we find thousands separator,
946 * so we need to chek, is the distance between two groupings
947 * equal to grouping[last_grouping_pos] */
949 if (length_since_last_delim != grouping[last_grouping_pos] )
951 if (!last_grouping_pos) return false;
954 -- last_grouping_pos;
955 if (length_since_last_delim != grouping[last_grouping_pos]) return false;
958 /* We are calling the grouping[0] twice, when grouping size is more than 1 */
959 if (grouping_size>1u && last_grouping_pos+1<grouping_size) return false;
962 /* Delimiter at the begining ',000' */
963 if (!length_since_last_delim) return false;
965 found_grouping = true;
966 if (length_since_last_delim > grouping[last_grouping_pos] ) return false;
969 length_since_last_delim = 0;
972 /* Delimiter at the end '100,' */
973 if (begin == end) return false;
984 if ( begin != end && ( *begin == lowercase_e || *begin == capital_e ) ) {
986 if ( begin == end ) return false;
988 bool exp_has_minus = false;
989 if( *begin == minus ) {
990 exp_has_minus = true;
992 if ( begin == end ) return false;
993 } else if (*begin == plus ) {
995 if ( begin == end ) return false;
998 int exp_pow_of_10 = 0;
999 while ( begin != end )
1002 || *begin >= czero + 10
1003 || exp_pow_of_10 * 10 < exp_pow_of_10) /* Overflows are checked lower more precisely*/
1006 exp_pow_of_10 *= 10;
1007 exp_pow_of_10 += *begin - zero;
1011 if ( exp_pow_of_10 ) {
1012 /* Overflows are checked lower */
1013 if ( exp_has_minus ) {
1014 pow_of_10 -= exp_pow_of_10;
1016 pow_of_10 += exp_pow_of_10;
1021 /* We need a more accurate algorithm... We can not use current algorithm
1022 * with long doubles (and with doubles if sizeof(double)==sizeof(long double)).
1024 long double result = std::pow(10.0L, pow_of_10) * mantissa;
1025 value = static_cast<T>( has_minus ? (boost::math::changesign)(result) : result);
1027 if ( (boost::math::isinf)(value) || (boost::math::isnan)(value) ) return false;
1033 namespace detail // stl_buf_unlocker
1035 template< class BufferType, class CharT >
1036 class stl_buf_unlocker: public BufferType{
1038 typedef BufferType base_class;
1039 #ifndef BOOST_NO_USING_TEMPLATE
1040 using base_class::pptr;
1041 using base_class::pbase;
1042 using base_class::setg;
1043 using base_class::setp;
1045 CharT* pptr() const { return base_class::pptr(); }
1046 CharT* pbase() const { return base_class::pbase(); }
1047 void setg(CharT* gbeg, CharT* gnext, CharT* gend){ return base_class::setg(gbeg, gnext, gend); }
1048 void setp(CharT* pbeg, CharT* pend) { return setp(pbeg, pend); }
1055 struct do_not_construct_stringbuffer_t{};
1058 namespace detail // optimized stream wrapper
1060 // String representation of Source has an upper limit.
1061 template< class CharT // a result of widest_char transformation
1062 , class Traits // usually char_traits<CharT>
1063 , bool RequiresStringbuffer
1065 class lexical_stream_limited_src
1067 typedef stl_buf_unlocker<std::basic_streambuf<CharT, Traits>, CharT > local_streambuffer_t;
1069 #if defined(BOOST_NO_STRINGSTREAM)
1070 typedef stl_buf_unlocker<std::strstream, CharT > local_stringbuffer_t;
1071 #elif defined(BOOST_NO_STD_LOCALE)
1072 typedef stl_buf_unlocker<std::stringstream, CharT > local_stringbuffer_t;
1074 typedef stl_buf_unlocker<std::basic_stringbuf<CharT, Traits>, CharT > local_stringbuffer_t;
1076 typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
1077 RequiresStringbuffer,
1078 local_stringbuffer_t,
1079 do_not_construct_stringbuffer_t
1080 >::type deduced_stringbuffer_t;
1082 // A string representation of Source is written to [start, finish).
1085 deduced_stringbuffer_t stringbuffer;
1088 lexical_stream_limited_src(CharT* sta, CharT* fin)
1095 lexical_stream_limited_src(lexical_stream_limited_src const&);
1096 void operator=(lexical_stream_limited_src const&);
1098 /************************************ HELPER FUNCTIONS FOR OPERATORS << ( ... ) ********************************/
1099 bool shl_char(CharT ch)
1101 Traits::assign(*start, ch);
1106 #ifndef BOOST_LCAST_NO_WCHAR_T
1110 BOOST_STATIC_ASSERT_MSG(( sizeof(T) <= sizeof(CharT)) ,
1111 "boost::lexical_cast does not support conversions from whar_t to char types."
1112 "Use boost::locale instead" );
1113 #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
1115 wchar_t w = BOOST_USE_FACET(std::ctype<wchar_t>, loc).widen(ch);
1119 Traits::assign(*start, w);
1125 bool shl_char_array(CharT const* str)
1127 start = const_cast<CharT*>(str);
1128 finish = start + Traits::length(str);
1132 #ifndef BOOST_LCAST_NO_WCHAR_T
1134 bool shl_char_array(T const* str)
1136 BOOST_STATIC_ASSERT_MSG(( sizeof(T) <= sizeof(CharT)),
1137 "boost::lexical_cast does not support conversions from wchar_t to char types."
1138 "Use boost::locale instead" );
1139 return shl_input_streamable(str);
1143 template<typename InputStreamable>
1144 bool shl_input_streamable(InputStreamable& input)
1146 std::basic_ostream<CharT> stream(&stringbuffer);
1147 bool const result = !(stream << input).fail();
1148 start = stringbuffer.pbase();
1149 finish = stringbuffer.pptr();
1150 return result && (start != finish);
1154 inline bool shl_signed(T n)
1156 start = lcast_put_unsigned<Traits>(lcast_to_unsigned(n), finish);
1160 CharT const minus = lcast_char_constants<CharT>::minus;
1161 Traits::assign(*start, minus);
1166 #if (defined _MSC_VER)
1167 # pragma warning( push )
1168 // C4996: This function or variable may be unsafe. Consider using sprintf_s instead
1169 # pragma warning( disable : 4996 )
1173 bool shl_float(float val,T* out)
1174 { using namespace std;
1175 if (put_inf_nan(start,finish,val)) return true;
1176 finish = start + sprintf(out,"%.*g", static_cast<int>(boost::detail::lcast_get_precision<float >()), val );
1177 return finish > start;
1181 bool shl_double(double val,T* out)
1182 { using namespace std;
1183 if (put_inf_nan(start,finish,val)) return true;
1184 finish = start + sprintf(out,"%.*lg", static_cast<int>(boost::detail::lcast_get_precision<double >()), val );
1185 return finish > start;
1189 bool shl_long_double(long double val,T* out)
1190 { using namespace std;
1191 if (put_inf_nan(start,finish,val)) return true;
1192 finish = start + sprintf(out,"%.*Lg", static_cast<int>(boost::detail::lcast_get_precision<long double >()), val );
1193 return finish > start;
1197 #if (defined _MSC_VER)
1198 # pragma warning( pop )
1202 #ifndef BOOST_LCAST_NO_WCHAR_T
1203 bool shl_float(float val,wchar_t* out)
1204 { using namespace std;
1205 if (put_inf_nan(start,finish,val)) return true;
1206 finish = start + swprintf(out,
1207 #if !defined(__MINGW32__) && !defined(UNDER_CE)
1210 L"%.*g", static_cast<int>(boost::detail::lcast_get_precision<float >()), val );
1212 return finish > start;
1216 bool shl_double(double val,wchar_t* out)
1217 { using namespace std;
1218 if (put_inf_nan(start,finish,val)) return true;
1219 /* __MINGW32__ is defined for both mingw.org and for mingw-w64.
1220 * For mingw-w64, __MINGW64__ is defined, too, when targetting
1223 * swprintf realization in MinGW and under WinCE does not conform
1227 finish = start + swprintf(out,
1228 #if !defined(__MINGW32__) && !defined(UNDER_CE)
1231 L"%.*lg", static_cast<int>(boost::detail::lcast_get_precision<double >()), val );
1232 return finish > start;
1236 bool shl_long_double(long double val,wchar_t* out)
1237 { using namespace std;
1238 if (put_inf_nan(start,finish,val)) return true;
1239 finish = start + swprintf(out,
1240 #if !defined(UNDER_CE)
1243 L"%.*Lg", static_cast<int>(boost::detail::lcast_get_precision<long double >()), val );
1244 return finish > start;
1250 /************************************ OPERATORS << ( ... ) ********************************/
1252 template<class Alloc>
1253 bool operator<<(std::basic_string<CharT,Traits,Alloc> const& str)
1255 start = const_cast<CharT*>(str.data());
1256 finish = start + str.length();
1260 bool operator<<(bool value)
1262 CharT const czero = lcast_char_constants<CharT>::zero;
1263 Traits::assign(*start, Traits::to_char_type(czero + value));
1268 bool operator<<(char ch) { return shl_char(ch); }
1269 bool operator<<(unsigned char ch) { return ((*this) << static_cast<char>(ch)); }
1270 bool operator<<(signed char ch) { return ((*this) << static_cast<char>(ch)); }
1271 #if !defined(BOOST_LCAST_NO_WCHAR_T)
1272 bool operator<<(wchar_t const* str) { return shl_char_array(str); }
1273 bool operator<<(wchar_t * str) { return shl_char_array(str); }
1274 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
1275 bool operator<<(wchar_t ch) { return shl_char(ch); }
1278 bool operator<<(unsigned char const* ch) { return ((*this) << reinterpret_cast<char const*>(ch)); }
1279 bool operator<<(unsigned char * ch) { return ((*this) << reinterpret_cast<char *>(ch)); }
1280 bool operator<<(signed char const* ch) { return ((*this) << reinterpret_cast<char const*>(ch)); }
1281 bool operator<<(signed char * ch) { return ((*this) << reinterpret_cast<char *>(ch)); }
1282 bool operator<<(char const* str) { return shl_char_array(str); }
1283 bool operator<<(char* str) { return shl_char_array(str); }
1284 bool operator<<(short n) { return shl_signed(n); }
1285 bool operator<<(int n) { return shl_signed(n); }
1286 bool operator<<(long n) { return shl_signed(n); }
1287 bool operator<<(unsigned short n) { start = lcast_put_unsigned<Traits>(n, finish); return true; }
1288 bool operator<<(unsigned int n) { start = lcast_put_unsigned<Traits>(n, finish); return true; }
1289 bool operator<<(unsigned long n) { start = lcast_put_unsigned<Traits>(n, finish); return true; }
1291 #if defined(BOOST_HAS_LONG_LONG)
1292 bool operator<<(boost::ulong_long_type n) { start = lcast_put_unsigned<Traits>(n, finish); return true; }
1293 bool operator<<(boost::long_long_type n) { return shl_signed(n); }
1294 #elif defined(BOOST_HAS_MS_INT64)
1295 bool operator<<(unsigned __int64 n) { start = lcast_put_unsigned<Traits>(n, finish); return true; }
1296 bool operator<<( __int64 n) { return shl_signed(n); }
1298 bool operator<<(float val) { return shl_float(val,start); }
1299 bool operator<<(double val) { return shl_double(val,start); }
1300 bool operator<<(long double val) {
1302 return shl_long_double(val,start);
1304 return shl_double(val,start);
1308 template<class InStreamable>
1309 bool operator<<(const InStreamable& input) { return shl_input_streamable(input); }
1311 /************************************ HELPER FUNCTIONS FOR OPERATORS >> ( ... ) ********************************/
1313 template <typename Type>
1314 bool shr_unsigned(Type& output)
1316 CharT const minus = lcast_char_constants<CharT>::minus;
1317 CharT const plus = lcast_char_constants<CharT>::plus;
1318 bool has_minus = false;
1320 /* We won`t use `start' any more, so no need in decrementing it after */
1321 if ( Traits::eq(minus,*start) )
1325 } else if ( Traits::eq( plus, *start ) )
1330 bool const succeed = lcast_ret_unsigned<Traits>(output, start, finish);
1331 #if (defined _MSC_VER)
1332 # pragma warning( push )
1333 // C4146: unary minus operator applied to unsigned type, result still unsigned
1334 # pragma warning( disable : 4146 )
1335 #elif defined( __BORLANDC__ )
1336 # pragma option push -w-8041
1338 if (has_minus) output = static_cast<Type>(-output);
1339 #if (defined _MSC_VER)
1340 # pragma warning( pop )
1341 #elif defined( __BORLANDC__ )
1347 template <typename Type>
1348 bool shr_signed(Type& output)
1350 CharT const minus = lcast_char_constants<CharT>::minus;
1351 CharT const plus = lcast_char_constants<CharT>::plus;
1352 typedef BOOST_DEDUCED_TYPENAME make_unsigned<Type>::type utype;
1354 bool has_minus = false;
1356 /* We won`t use `start' any more, so no need in decrementing it after */
1357 if ( Traits::eq(minus,*start) )
1361 } else if ( Traits::eq(plus, *start) )
1366 bool succeed = lcast_ret_unsigned<Traits>(out_tmp, start, finish);
1368 #if (defined _MSC_VER)
1369 # pragma warning( push )
1370 // C4146: unary minus operator applied to unsigned type, result still unsigned
1371 # pragma warning( disable : 4146 )
1372 #elif defined( __BORLANDC__ )
1373 # pragma option push -w-8041
1375 utype const comp_val = static_cast<utype>(-(std::numeric_limits<Type>::min)());
1376 succeed = succeed && out_tmp<=comp_val;
1378 #if (defined _MSC_VER)
1379 # pragma warning( pop )
1380 #elif defined( __BORLANDC__ )
1384 utype const comp_val = static_cast<utype>((std::numeric_limits<Type>::max)());
1385 succeed = succeed && out_tmp<=comp_val;
1391 template<typename InputStreamable>
1392 bool shr_using_base_class(InputStreamable& output)
1394 #if (defined _MSC_VER)
1395 # pragma warning( push )
1396 // conditional expression is constant
1397 # pragma warning( disable : 4127 )
1399 if(is_pointer<InputStreamable>::value)
1402 local_streambuffer_t bb;
1403 bb.setg(start, start, finish);
1404 std::basic_istream<CharT> stream(&bb);
1405 stream.unsetf(std::ios::skipws);
1406 lcast_set_precision(stream, static_cast<InputStreamable*>(0));
1407 #if (defined _MSC_VER)
1408 # pragma warning( pop )
1410 return stream >> output &&
1412 #if defined(__GNUC__) && (__GNUC__<3) && defined(BOOST_NO_STD_WSTRING)
1413 // GCC 2.9x lacks std::char_traits<>::eof().
1414 // We use BOOST_NO_STD_WSTRING to filter out STLport and libstdc++-v3
1415 // configurations, which do provide std::char_traits<>::eof().
1424 inline bool shr_xchar(T& output)
1426 BOOST_STATIC_ASSERT_MSG(( sizeof(CharT) == sizeof(T) ),
1427 "boost::lexical_cast does not support conversions from whar_t to char types."
1428 "Use boost::locale instead" );
1429 bool const ok = (finish - start == 1);
1432 Traits::assign(out, *start);
1433 output = static_cast<T>(out);
1438 /************************************ OPERATORS >> ( ... ) ********************************/
1440 bool operator>>(unsigned short& output) { return shr_unsigned(output); }
1441 bool operator>>(unsigned int& output) { return shr_unsigned(output); }
1442 bool operator>>(unsigned long int& output) { return shr_unsigned(output); }
1443 bool operator>>(short& output) { return shr_signed(output); }
1444 bool operator>>(int& output) { return shr_signed(output); }
1445 bool operator>>(long int& output) { return shr_signed(output); }
1446 #if defined(BOOST_HAS_LONG_LONG)
1447 bool operator>>(boost::ulong_long_type& output) { return shr_unsigned(output); }
1448 bool operator>>(boost::long_long_type& output) { return shr_signed(output); }
1449 #elif defined(BOOST_HAS_MS_INT64)
1450 bool operator>>(unsigned __int64& output) { return shr_unsigned(output); }
1451 bool operator>>(__int64& output) { return shr_signed(output); }
1454 bool operator>>(CharT& output) { return shr_xchar(output); }
1455 bool operator>>(unsigned char& output) { return shr_xchar(output); }
1456 bool operator>>(signed char& output) { return shr_xchar(output); }
1457 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
1458 bool operator>>(std::string& str) { str.assign(start, finish); return true; }
1459 # ifndef BOOST_LCAST_NO_WCHAR_T
1460 bool operator>>(std::wstring& str) { str.assign(start, finish); return true; }
1463 template<class Alloc>
1464 bool operator>>(std::basic_string<CharT,Traits,Alloc>& str) { str.assign(start, finish); return true; }
1467 * case "-0" || "0" || "+0" : output = false; return true;
1468 * case "1" || "+1": output = true; return true;
1469 * default: return false;
1471 bool operator>>(bool& output)
1473 CharT const zero = lcast_char_constants<CharT>::zero;
1474 CharT const plus = lcast_char_constants<CharT>::plus;
1475 CharT const minus = lcast_char_constants<CharT>::minus;
1477 switch(finish-start)
1480 output = Traits::eq(start[0], zero+1);
1481 return output || Traits::eq(start[0], zero );
1483 if ( Traits::eq( plus, *start) )
1486 output = Traits::eq(start[0], zero +1);
1487 return output || Traits::eq(start[0], zero );
1491 return Traits::eq( minus, *start)
1492 && Traits::eq( zero, start[1]);
1495 output = false; // Suppress warning about uninitalized variable
1500 bool operator>>(float& output) { return lcast_ret_float<Traits>(output,start,finish); }
1503 // Not optimised converter
1505 bool float_types_converter_internal(T& output, int /*tag*/) {
1506 if (parse_inf_nan(start, finish, output)) return true;
1507 bool return_value = shr_using_base_class(output);
1509 /* Some compilers and libraries successfully
1510 * parse 'inf', 'INFINITY', '1.0E', '1.0E-'...
1511 * We are trying to provide a unified behaviour,
1512 * so we just forbid such conversions (as some
1513 * of the most popular compilers/libraries do)
1515 CharT const minus = lcast_char_constants<CharT>::minus;
1516 CharT const plus = lcast_char_constants<CharT>::plus;
1517 CharT const capital_e = lcast_char_constants<CharT>::capital_e;
1518 CharT const lowercase_e = lcast_char_constants<CharT>::lowercase_e;
1519 if ( return_value &&
1521 *(finish-1) == lowercase_e // 1.0e
1522 || *(finish-1) == capital_e // 1.0E
1523 || *(finish-1) == minus // 1.0e- or 1.0E-
1524 || *(finish-1) == plus // 1.0e+ or 1.0E+
1528 return return_value;
1531 // Optimised converter
1532 bool float_types_converter_internal(double& output,char /*tag*/) {
1533 return lcast_ret_float<Traits>(output,start,finish);
1537 bool operator>>(double& output)
1540 * Some compilers implement long double as double. In that case these types have
1541 * same size, same precision, same max and min values... And it means,
1542 * that current implementation of lcast_ret_float cannot be used for type
1543 * double, because it will give a big precision loss.
1546 #if defined(BOOST_HAS_LONG_LONG) || defined(BOOST_HAS_MS_INT64)
1547 ::boost::type_traits::ice_eq< sizeof(double), sizeof(long double) >::value,
1555 return float_types_converter_internal(output, tag);
1558 bool operator>>(long double& output)
1561 return float_types_converter_internal(output, tag);
1564 // Generic istream-based algorithm.
1565 // lcast_streambuf_for_target<InputStreamable>::value is true.
1566 template<typename InputStreamable>
1567 bool operator>>(InputStreamable& output) { return shr_using_base_class(output); }
1571 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
1573 // call-by-const reference version
1578 struct array_to_pointer_decay
1583 template<class T, std::size_t N>
1584 struct array_to_pointer_decay<T[N]>
1586 typedef const T * type;
1589 template<typename T>
1592 BOOST_STATIC_CONSTANT(bool, value = false );
1595 template<typename CharT, typename Traits, typename Alloc>
1596 struct is_stdstring< std::basic_string<CharT, Traits, Alloc> >
1598 BOOST_STATIC_CONSTANT(bool, value = true );
1601 template<typename T>
1602 struct is_char_or_wchar
1605 #ifndef BOOST_LCAST_NO_WCHAR_T
1606 typedef wchar_t wchar_t_if_supported;
1608 typedef char wchar_t_if_supported;
1611 #ifndef BOOST_NO_CHAR16_T
1612 typedef char16_t char16_t_if_supported;
1614 typedef char char16_t_if_supported;
1617 #ifndef BOOST_NO_CHAR32_T
1618 typedef char32_t char32_t_if_supported;
1620 typedef char char32_t_if_supported;
1624 BOOST_STATIC_CONSTANT(bool, value =
1626 ::boost::type_traits::ice_or<
1627 is_same< T, char >::value,
1628 is_same< T, wchar_t_if_supported >::value,
1629 is_same< T, char16_t_if_supported >::value,
1630 is_same< T, char32_t_if_supported >::value,
1631 is_same< T, unsigned char >::value,
1632 is_same< T, signed char >::value
1638 template<typename Target, typename Source>
1639 struct is_arithmetic_and_not_xchars
1641 BOOST_STATIC_CONSTANT(bool, value =
1643 ::boost::type_traits::ice_and<
1644 is_arithmetic<Source>::value,
1645 is_arithmetic<Target>::value,
1646 ::boost::type_traits::ice_not<
1647 detail::is_char_or_wchar<Target>::value
1649 ::boost::type_traits::ice_not<
1650 detail::is_char_or_wchar<Source>::value
1658 * is_xchar_to_xchar<Target, Source>::value is true, when
1659 * Target and Souce are the same char types, or when
1660 * Target and Souce are char types of the same size.
1662 template<typename Target, typename Source>
1663 struct is_xchar_to_xchar
1665 BOOST_STATIC_CONSTANT(bool, value =
1667 ::boost::type_traits::ice_or<
1668 ::boost::type_traits::ice_and<
1669 is_same<Source,Target>::value,
1670 is_char_or_wchar<Target>::value
1672 ::boost::type_traits::ice_and<
1673 ::boost::type_traits::ice_eq< sizeof(char),sizeof(Target)>::value,
1674 ::boost::type_traits::ice_eq< sizeof(char),sizeof(Source)>::value,
1675 is_char_or_wchar<Target>::value,
1676 is_char_or_wchar<Source>::value
1683 template<typename Target, typename Source>
1684 struct is_char_array_to_stdstring
1686 BOOST_STATIC_CONSTANT(bool, value = false );
1689 template<typename CharT, typename Traits, typename Alloc>
1690 struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, CharT* >
1692 BOOST_STATIC_CONSTANT(bool, value = true );
1695 template<typename CharT, typename Traits, typename Alloc>
1696 struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, const CharT* >
1698 BOOST_STATIC_CONSTANT(bool, value = true );
1701 #if (defined _MSC_VER)
1702 # pragma warning( push )
1703 # pragma warning( disable : 4701 ) // possible use of ... before initialization
1704 # pragma warning( disable : 4702 ) // unreachable code
1705 # pragma warning( disable : 4267 ) // conversion from 'size_t' to 'unsigned int'
1707 template<typename Target, typename Source>
1708 struct lexical_cast_do_cast
1710 static inline Target lexical_cast_impl(const Source& arg)
1712 typedef BOOST_DEDUCED_TYPENAME detail::array_to_pointer_decay<Source>::type src;
1714 typedef BOOST_DEDUCED_TYPENAME detail::widest_char<
1715 BOOST_DEDUCED_TYPENAME detail::stream_char<Target>::type
1716 , BOOST_DEDUCED_TYPENAME detail::stream_char<src>::type
1719 typedef detail::lcast_src_length<src> lcast_src_length;
1720 std::size_t const src_len = lcast_src_length::value;
1721 char_type buf[src_len + 1];
1722 lcast_src_length::check_coverage();
1724 typedef BOOST_DEDUCED_TYPENAME
1725 deduce_char_traits<char_type,Target,Source>::type traits;
1727 typedef BOOST_DEDUCED_TYPENAME remove_pointer<src >::type removed_ptr_t;
1728 const bool requires_stringbuf =
1730 ::boost::type_traits::ice_or<
1731 is_stdstring<src >::value,
1732 is_arithmetic<src >::value,
1733 ::boost::type_traits::ice_and<
1734 is_pointer<src >::value,
1735 is_char_or_wchar<removed_ptr_t >::value,
1736 ::boost::type_traits::ice_eq<
1738 sizeof(removed_ptr_t)
1744 detail::lexical_stream_limited_src<char_type,traits, requires_stringbuf >
1745 interpreter(buf, buf + src_len);
1748 // Disabling ADL, by directly specifying operators.
1749 if(!(interpreter.operator <<(arg) && interpreter.operator >>(result)))
1750 BOOST_LCAST_THROW_BAD_CAST(Source, Target);
1754 #if (defined _MSC_VER)
1755 # pragma warning( pop )
1758 template<typename Source>
1759 struct lexical_cast_copy
1761 static inline Source lexical_cast_impl(const Source &arg)
1767 class precision_loss_error : public boost::numeric::bad_numeric_cast
1770 virtual const char * what() const throw()
1771 { return "bad numeric conversion: precision loss error"; }
1775 struct throw_on_precision_loss
1777 typedef boost::numeric::Trunc<S> Rounder;
1778 typedef S source_type ;
1780 typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
1782 static source_type nearbyint ( argument_type s )
1784 source_type orig_div_round = s / Rounder::nearbyint(s);
1786 if ( (orig_div_round > 1 ? orig_div_round - 1 : 1 - orig_div_round) > std::numeric_limits<source_type>::epsilon() )
1787 BOOST_THROW_EXCEPTION( precision_loss_error() );
1791 typedef typename Rounder::round_style round_style;
1794 template<typename Target, typename Source>
1795 struct lexical_cast_dynamic_num_not_ignoring_minus
1797 static inline Target lexical_cast_impl(const Source &arg)
1800 typedef boost::numeric::converter<
1803 boost::numeric::conversion_traits<Target,Source>,
1804 boost::numeric::def_overflow_handler,
1805 throw_on_precision_loss<Source>
1808 return Converter::convert(arg);
1809 } catch( ::boost::numeric::bad_numeric_cast const& ) {
1810 BOOST_LCAST_THROW_BAD_CAST(Source, Target);
1812 BOOST_UNREACHABLE_RETURN(static_cast<Target>(0));
1816 template<typename Target, typename Source>
1817 struct lexical_cast_dynamic_num_ignoring_minus
1819 static inline Target lexical_cast_impl(const Source &arg)
1822 typedef boost::numeric::converter<
1825 boost::numeric::conversion_traits<Target,Source>,
1826 boost::numeric::def_overflow_handler,
1827 throw_on_precision_loss<Source>
1830 bool has_minus = ( arg < 0);
1832 return static_cast<Target>(-Converter::convert(-arg));
1834 return Converter::convert(arg);
1836 } catch( ::boost::numeric::bad_numeric_cast const& ) {
1837 BOOST_LCAST_THROW_BAD_CAST(Source, Target);
1839 BOOST_UNREACHABLE_RETURN(static_cast<Target>(0));
1844 * lexical_cast_dynamic_num follows the rules:
1845 * 1) If Source can be converted to Target without precision loss and
1846 * without overflows, then assign Source to Target and return
1848 * 2) If Source is less than 0 and Target is an unsigned integer,
1849 * then negate Source, check the requirements of rule 1) and if
1850 * successful, assign static_casted Source to Target and return
1852 * 3) Otherwise throw a bad_lexical_cast exception
1855 * Rule 2) required because boost::lexical_cast has the behavior of
1856 * stringstream, which uses the rules of scanf for conversions. And
1857 * in the C99 standard for unsigned input value minus sign is
1858 * optional, so if a negative number is read, no errors will arise
1859 * and the result will be the two's complement.
1861 template<typename Target, typename Source>
1862 struct lexical_cast_dynamic_num
1864 static inline Target lexical_cast_impl(const Source &arg)
1866 typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
1867 ::boost::type_traits::ice_and<
1868 ::boost::type_traits::ice_or<
1869 ::boost::is_signed<Source>::value,
1870 ::boost::is_float<Source>::value
1872 ::boost::type_traits::ice_not<
1873 is_same<Source, bool>::value
1875 ::boost::type_traits::ice_not<
1876 is_same<Target, bool>::value
1878 ::boost::is_unsigned<Target>::value
1880 lexical_cast_dynamic_num_ignoring_minus<Target, Source>,
1881 lexical_cast_dynamic_num_not_ignoring_minus<Target, Source>
1882 >::type caster_type;
1884 return caster_type::lexical_cast_impl(arg);
1889 template<typename Target, typename Source>
1890 inline Target lexical_cast(const Source &arg)
1892 typedef BOOST_DEDUCED_TYPENAME detail::array_to_pointer_decay<Source>::type src;
1894 typedef BOOST_DEDUCED_TYPENAME ::boost::type_traits::ice_or<
1895 detail::is_xchar_to_xchar<Target, src>::value,
1896 detail::is_char_array_to_stdstring<Target,src>::value,
1897 ::boost::type_traits::ice_and<
1898 is_same<Target, src>::value,
1899 detail::is_stdstring<Target>::value
1903 typedef BOOST_DEDUCED_TYPENAME
1904 detail::is_arithmetic_and_not_xchars<Target, src> do_copy_with_dynamic_check_type;
1906 typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
1907 do_copy_type::value,
1908 detail::lexical_cast_copy<src>,
1909 BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
1910 do_copy_with_dynamic_check_type::value,
1911 detail::lexical_cast_dynamic_num<Target, src>,
1912 detail::lexical_cast_do_cast<Target, src>
1914 >::type caster_type;
1916 return caster_type::lexical_cast_impl(arg);
1921 namespace detail // stream wrapper for handling lexical conversions
1923 template<typename Target, typename Source, typename Traits>
1924 class lexical_stream
1927 typedef typename widest_char<
1928 typename stream_char<Target>::type,
1929 typename stream_char<Source>::type>::type char_type;
1931 typedef Traits traits_type;
1934 lexical_stream(char_type* = 0, char_type* = 0)
1936 stream.unsetf(std::ios::skipws);
1937 lcast_set_precision(stream, static_cast<Source*>(0), static_cast<Target*>(0) );
1941 #if defined(BOOST_NO_STRINGSTREAM)
1942 stream.freeze(false);
1945 bool operator<<(const Source &input)
1947 return !(stream << input).fail();
1949 template<typename InputStreamable>
1950 bool operator>>(InputStreamable &output)
1952 return !is_pointer<InputStreamable>::value &&
1955 #if defined(__GNUC__) && (__GNUC__<3) && defined(BOOST_NO_STD_WSTRING)
1956 // GCC 2.9x lacks std::char_traits<>::eof().
1957 // We use BOOST_NO_STD_WSTRING to filter out STLport and libstdc++-v3
1958 // configurations, which do provide std::char_traits<>::eof().
1966 bool operator>>(std::string &output)
1968 #if defined(BOOST_NO_STRINGSTREAM)
1971 stream.str().swap(output);
1974 #ifndef BOOST_LCAST_NO_WCHAR_T
1975 bool operator>>(std::wstring &output)
1977 stream.str().swap(output);
1983 #if defined(BOOST_NO_STRINGSTREAM)
1984 std::strstream stream;
1985 #elif defined(BOOST_NO_STD_LOCALE)
1986 std::stringstream stream;
1988 std::basic_stringstream<char_type,traits_type> stream;
1993 // call-by-value fallback version (deprecated)
1995 template<typename Target, typename Source>
1996 Target lexical_cast(Source arg)
1998 typedef typename detail::widest_char<
1999 BOOST_DEDUCED_TYPENAME detail::stream_char<Target>::type
2000 , BOOST_DEDUCED_TYPENAME detail::stream_char<Source>::type
2003 typedef std::char_traits<char_type> traits;
2004 detail::lexical_stream<Target, Source, traits> interpreter;
2007 if(!(interpreter << arg && interpreter >> result))
2008 BOOST_LCAST_THROW_BAD_CAST(Source, Target);
2015 // Copyright Kevlin Henney, 2000-2005.
2016 // Copyright Alexander Nasonov, 2006-2010.
2017 // Copyright Antony Polukhin, 2011.
2019 // Distributed under the Boost Software License, Version 1.0. (See
2020 // accompanying file LICENSE_1_0.txt or copy at
2021 // http://www.boost.org/LICENSE_1_0.txt)
2023 #undef BOOST_LCAST_NO_WCHAR_T