diff --git a/doc/lexical_cast.qbk b/doc/lexical_cast.qbk index ea31181..78137ce 100644 --- a/doc/lexical_cast.qbk +++ b/doc/lexical_cast.qbk @@ -278,7 +278,9 @@ limitation of compiler options that you use. * [*boost 1.54.0 :] - * Added code to convert boost::int128_type and boost::uint128_type (requires GCC 4.7 or higher). + * Added code to convert `boost::int128_type` and `boost::uint128_type` types (requires GCC 4.7 or higher). + * Conversions to pointers will now fail to compile, instead of throwing at runtime. + * Restored ability to get pointers to `lexical_cast` function (was broken in 1.53.0). * [*boost 1.53.0 :] diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index f5f2d32..76736a3 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -1968,35 +1968,36 @@ namespace boost { template bool shr_using_base_class(InputStreamable& output) { -#if (defined _MSC_VER) -# pragma warning( push ) - // conditional expression is constant -# pragma warning( disable : 4127 ) -#endif - if(is_pointer::value) - return false; + BOOST_STATIC_ASSERT_MSG( + (!boost::is_pointer::value), + "boost::lexical_cast can not convert to pointers" + ); #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_LOCALE) - // If you have compilation error at this point, than your STL library - // unsupports such conversions. Try updating it. - BOOST_STATIC_ASSERT((boost::is_same::value)); + BOOST_STATIC_ASSERT_MSG((boost::is_same::value), + "boost::lexical_cast can not convert, because your STL library does not " + "support such conversions. Try updating it." + ); #endif #if defined(BOOST_NO_STRINGSTREAM) std::istrstream stream(start, finish - start); -#elif defined(BOOST_NO_STD_LOCALE) +#else + +#if defined(BOOST_NO_STD_LOCALE) std::istringstream stream; #else std::basic_istringstream stream; -#endif - static_cast(stream.rdbuf()) - ->setbuf(start, finish - start); +#endif // BOOST_NO_STD_LOCALE + + unlocked_but_t buf; + buf.setbuf(start, finish - start); + dynamic_cast&>(stream).rdbuf(&buf); +#endif // BOOST_NO_STRINGSTREAM stream.unsetf(std::ios::skipws); lcast_set_precision(stream, static_cast(0)); -#if (defined _MSC_VER) -# pragma warning( pop ) -#endif + return stream >> output && stream.get() == #if defined(__GNUC__) && (__GNUC__<3) && defined(BOOST_NO_STD_WSTRING) diff --git a/lexical_cast_test.cpp b/lexical_cast_test.cpp index b7b7fb3..875d820 100644 --- a/lexical_cast_test.cpp +++ b/lexical_cast_test.cpp @@ -67,7 +67,6 @@ void test_conversion_to_bool(); void test_conversion_with_nonconst_char(); void test_conversion_to_string(); void test_conversion_from_to_wchar_t_alias(); -void test_conversion_to_pointer(); void test_conversion_from_wchar_t(); void test_conversion_to_wchar_t(); void test_conversion_from_wstring(); @@ -100,7 +99,6 @@ unit_test::test_suite *init_unit_test_suite(int, char *[]) suite->add(BOOST_TEST_CASE(test_conversion_to_double)); suite->add(BOOST_TEST_CASE(test_conversion_to_bool)); suite->add(BOOST_TEST_CASE(test_conversion_from_to_wchar_t_alias)); - suite->add(BOOST_TEST_CASE(test_conversion_to_pointer)); suite->add(BOOST_TEST_CASE(test_conversion_to_string)); suite->add(BOOST_TEST_CASE(test_conversion_with_nonconst_char)); #ifndef BOOST_LCAST_NO_WCHAR_T @@ -312,14 +310,6 @@ void test_conversion_from_to_wchar_t_alias() BOOST_CHECK_EQUAL(std::string("123"), lexical_cast(123ul)); } -void test_conversion_to_pointer() -{ - BOOST_CHECK_THROW(lexical_cast("Test"), bad_lexical_cast); -#ifndef BOOST_LCAST_NO_WCHAR_T - BOOST_CHECK_THROW(lexical_cast("Test"), bad_lexical_cast); -#endif -} - void test_conversion_from_wchar_t() { #ifndef BOOST_LCAST_NO_WCHAR_T diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 5b1447f..9c3aff3 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -55,5 +55,6 @@ test-suite conversion [ run lexical_cast_integral_types_test.cpp ] [ run lexical_cast_stream_detection_test.cpp ] [ run lexical_cast_stream_traits_test.cpp ] + [ compile-fail lexical_cast_to_pointer_test.cpp ] ; diff --git a/test/lexical_cast_to_pointer_test.cpp b/test/lexical_cast_to_pointer_test.cpp new file mode 100644 index 0000000..6756146 --- /dev/null +++ b/test/lexical_cast_to_pointer_test.cpp @@ -0,0 +1,21 @@ +// // Unit test for boost::lexical_cast. +// +// See http://www.boost.org for most recent version, including documentation. +// +// Copyright Antony Polukhin, 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). + +#include +#include + +#define BOOST_INCLUDE_MAIN +#include + +int test_main(int, char*[]) +{ + boost::lexical_cast("Hello"); + return 0; +}