From 461dce3851922cbaf4b04da0a0350030b9b9098c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Terje=20Sletteb=C3=B8?= Date: Thu, 16 Jun 2005 18:27:22 +0000 Subject: [PATCH] Fixed so it doesn't trim any whitespace when converting, fixed reversed bad_lexical_cast constructor parameters, changed to pass-by-const reference [SVN r29629] --- include/boost/lexical_cast.hpp | 62 +++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 079ca68..ad2aea2 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -12,7 +12,7 @@ // with additional fixes and suggestions from Gennaro Prota, // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, // and other Boosters -// when: November 2000, March 2003 +// when: November 2000, March 2003, June 2005 #include #include @@ -45,9 +45,9 @@ namespace boost { } bad_lexical_cast( - const std::type_info &s, - const std::type_info &t) : - source(&s), target(&t) + const std::type_info &source_type, + const std::type_info &target_type) : + source(&source_type), target(&target_type) { } const std::type_info &source_type() const @@ -123,6 +123,11 @@ namespace boost template class lexical_stream { + private: + typedef typename widest_char< + typename stream_char::type, + typename stream_char::type>::type char_type; + public: lexical_stream() { @@ -148,7 +153,7 @@ namespace boost { return !is_pointer::value && stream >> output && - (stream >> std::ws).eof(); + stream.get() == std::char_traits::eof(); } bool operator>>(std::string &output) { @@ -166,10 +171,6 @@ namespace boost } #endif private: - typedef typename widest_char< - typename stream_char::type, - typename stream_char::type>::type char_type; - #if defined(BOOST_NO_STRINGSTREAM) std::strstream stream; #elif defined(BOOST_NO_STD_LOCALE) @@ -180,6 +181,42 @@ namespace boost }; } + #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + + // call-by-const reference version + + namespace detail + { + template + struct array_to_pointer_decay + { + typedef T type; + }; + + template + struct array_to_pointer_decay + { + typedef const T * type; + }; + } + + template + Target lexical_cast(const Source &arg) + { + typedef typename detail::array_to_pointer_decay::type NewSource; + + detail::lexical_stream interpreter; + Target result; + + if(!(interpreter << arg && interpreter >> result)) + throw_exception(bad_lexical_cast(typeid(NewSource), typeid(Target))); + return result; + } + + #else + + // call-by-value fallback version (deprecated) + template Target lexical_cast(Source arg) { @@ -187,12 +224,14 @@ namespace boost Target result; if(!(interpreter << arg && interpreter >> result)) - throw_exception(bad_lexical_cast(typeid(Target), typeid(Source))); + throw_exception(bad_lexical_cast(typeid(Source), typeid(Target))); return result; } + + #endif } -// Copyright Kevlin Henney, 2000-2003. All rights reserved. +// Copyright Kevlin Henney, 2000-2005. All rights reserved. // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at @@ -200,4 +239,3 @@ namespace boost #undef DISABLE_WIDE_CHAR_SUPPORT #endif -