22a23,40 > // > // Jan 2014: Added try_lexical_cast, by Troy Korjuslommi. > > /** > * try_lexical_cast: A version which doesn't throw bad_lexical_cast. > * All other exceptions will be thrown as normal. > * > * Getting rid of exceptions thrown in the callbacks nothrow_overflow_handler and detect_precision_loss > * would mean rewriting boost::numeric::converter, which seems excessive, and futile. And it would create > * a dependency on those patches going through for this patch to be accepted. Ergo, I resorted to catching > * these exceptions to provide non throwing behavior. > * > * Dropped the deprecated call-by-value fallback version > * template > * bool bool try_lexical_cast(Target& result, Source arg); > */ > > 2378a2397,2423 > > > static inline bool try_lexical_cast_impl(Target& result, const Source& arg) > { > typedef lexical_cast_stream_traits 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))) > return false; > > return true; > } > 2442a2488,2508 > > static inline bool try_lexical_cast_impl(Target& result, const Source &arg) > { > try { > result = boost::numeric::converter< > Target, > Source, > boost::numeric::conversion_traits, > nothrow_overflow_handler, > detect_precision_loss > >::convert(arg); > return true; > > } catch (const bad_lexical_cast& ex) { > return false; > } > catch (...) { > throw; > } > } > 2467a2534,2563 > > static inline bool try_lexical_cast_impl(Target& result, const Source &arg) > { > typedef BOOST_DEDUCED_TYPENAME boost::mpl::eval_if_c< > boost::is_float::value, > boost::mpl::identity, > boost::make_unsigned > >::type usource_t; > > typedef boost::numeric::converter< > Target, > usource_t, > boost::numeric::conversion_traits, > nothrow_overflow_handler, > detect_precision_loss > > converter_t; > > try { > result = ( > arg < 0 ? static_cast(0u - converter_t::convert(0u - arg)) : converter_t::convert(arg) > ); > return true; > } catch (const bad_lexical_cast& ex) { > return false; > } > catch (...) { > throw; > } > } > 2512a2609,2632 > > static inline bool try_lexical_cast_impl(Target& result, const Source &arg) > { > typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c< > boost::type_traits::ice_and< > boost::type_traits::ice_or< > boost::is_signed::value, > boost::is_float::value > >::value, > boost::type_traits::ice_not< > boost::is_same::value > >::value, > boost::type_traits::ice_not< > boost::is_same::value > >::value, > boost::is_unsigned::value > >::value, > lexical_cast_dynamic_num_ignoring_minus, > lexical_cast_dynamic_num_not_ignoring_minus > >::type caster_type; > > return caster_type::try_lexical_cast_impl(result, arg); > } > 2735a2856,2951 > > > #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(__SUNPRO_CC) && !defined(__PGIC__) > > namespace boost > { > > template > inline bool try_lexical_cast(Target& result, const Source &arg) > { > typedef BOOST_DEDUCED_TYPENAME boost::detail::array_to_pointer_decay::type src; > > typedef BOOST_DEDUCED_TYPENAME boost::type_traits::ice_or< > boost::detail::is_xchar_to_xchar::value, > boost::detail::is_char_array_to_stdstring::value, > boost::type_traits::ice_and< > boost::is_same::value, > boost::detail::is_stdstring::value > >::value > > shall_we_copy_t; > > typedef BOOST_DEDUCED_TYPENAME > boost::detail::is_arithmetic_and_not_xchars 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, > BOOST_DEDUCED_TYPENAME boost::mpl::if_c< > shall_we_copy_with_dynamic_check_t::value, > boost::detail::lexical_cast_dynamic_num, > boost::detail::lexical_cast_do_cast > >::type > >::type caster_type; > > return caster_type::try_lexical_cast_impl(result, arg); > } > > template > inline bool try_lexical_cast(Target& result, const char* chars, std::size_t count) > { > return ::boost::try_lexical_cast(result, > ::boost::iterator_range(chars, chars + count) > ); > } > > > template > inline bool try_lexical_cast(Target& result, const unsigned char* chars, std::size_t count) > { > return ::boost::try_lexical_cast(result, > ::boost::iterator_range(chars, chars + count) > ); > } > > template > inline bool try_lexical_cast(Target& result, const signed char* chars, std::size_t count) > { > return ::boost::try_lexical_cast(result, > ::boost::iterator_range(chars, chars + count) > ); > } > > #ifndef BOOST_LCAST_NO_WCHAR_T > template > inline bool try_lexical_cast(Target& result, const wchar_t* chars, std::size_t count) > { > return ::boost::try_lexical_cast(result, > ::boost::iterator_range(chars, chars + count) > ); > } > #endif > #ifndef BOOST_NO_CXX11_CHAR16_T > template > inline bool try_lexical_cast(Target& result, const char16_t* chars, std::size_t count) > { > return ::boost::try_lexical_cast(result, > ::boost::iterator_range(chars, chars + count) > ); > } > #endif > #ifndef BOOST_NO_CXX11_CHAR32_T > template > inline bool try_lexical_cast(Target& result, const char32_t* chars, std::size_t count) > { > return ::boost::try_lexical_cast(result, > ::boost::iterator_range(chars, chars + count) > ); > } > #endif > > } // namespace boost > > #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION > > > 2738a2955 > // Copyright Troy Korjuslommi, 2014.