From 824fbd9b060c101eb50ec8cf9c94bb63f2548eea Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Wed, 26 Sep 2012 19:57:31 +0000 Subject: [PATCH] Do not overload lexical_cast(const From&), instead add lexical_cast(character_type,size_type) templates (refs #7421). Added tests for getting &boost::lexical_cast functions pointers [SVN r80715] --- include/boost/lexical_cast.hpp | 54 ++++++++++++++++++++++++++++++---- lexical_cast_test.cpp | 16 ++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 244090a..c475982 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -2300,17 +2300,59 @@ namespace boost { return caster_type::lexical_cast_impl(arg); } - template - inline Target lexical_cast(const CharType* chars, std::size_t count) + template + inline Target lexical_cast(const char* chars, std::size_t count) { - BOOST_STATIC_ASSERT_MSG(::boost::detail::is_char_or_wchar::value, - "CharType must be a character or wide character type"); - return ::boost::lexical_cast( - ::boost::iterator_range(chars, chars + count) + ::boost::iterator_range(chars, chars + count) ); } + + template + inline Target lexical_cast(const unsigned char* chars, std::size_t count) + { + return ::boost::lexical_cast( + ::boost::iterator_range(chars, chars + count) + ); + } + + template + inline Target lexical_cast(const signed char* chars, std::size_t count) + { + return ::boost::lexical_cast( + ::boost::iterator_range(chars, chars + count) + ); + } + +#ifndef BOOST_LCAST_NO_WCHAR_T + template + inline Target lexical_cast(const wchar_t* chars, std::size_t count) + { + return ::boost::lexical_cast( + ::boost::iterator_range(chars, chars + count) + ); + } +#endif +#ifndef BOOST_NO_CHAR16_T + template + inline Target lexical_cast(const char16_t* chars, std::size_t count) + { + return ::boost::lexical_cast( + ::boost::iterator_range(chars, chars + count) + ); + } +#endif +#ifndef BOOST_NO_CHAR32_T + template + inline Target lexical_cast(const char32_t* chars, std::size_t count) + { + return ::boost::lexical_cast( + ::boost::iterator_range(chars, chars + count) + ); + } +#endif + } // namespace boost #else // #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION diff --git a/lexical_cast_test.cpp b/lexical_cast_test.cpp index bc4184f..7358270 100644 --- a/lexical_cast_test.cpp +++ b/lexical_cast_test.cpp @@ -88,6 +88,7 @@ void test_char16_conversions(); #if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS) void test_char32_conversions(); #endif +void test_getting_pointer_to_function(); unit_test::test_suite *init_unit_test_suite(int, char *[]) { @@ -125,6 +126,7 @@ unit_test::test_suite *init_unit_test_suite(int, char *[]) #if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS) suite->add(BOOST_TEST_CASE(&test_char32_conversions)); #endif + suite->add(BOOST_TEST_CASE(&test_getting_pointer_to_function)); return suite; } @@ -599,4 +601,18 @@ void test_char32_conversions() } #endif +template +To try_cast_by_ptr(const From& from, const Func& f) { + return f(from); +}; + +void test_getting_pointer_to_function() +{ + // Just checking that &lexical_cast is not ambiguous + (void)&lexical_cast; + BOOST_CHECK_EQUAL(100, try_cast_by_ptr("100", &boost::lexical_cast)); + BOOST_CHECK_EQUAL(100, try_cast_by_ptr("100", &boost::lexical_cast)); + BOOST_CHECK_EQUAL(std::string("100"), try_cast_by_ptr(100, &boost::lexical_cast)); +} +