Do not overload lexical_cast<To, From>(const From&), instead add lexical_cast<To>(character_type,size_type) templates (refs #7421). Added tests for getting &boost::lexical_cast<To,from> functions pointers

[SVN r80715]
This commit is contained in:
Antony Polukhin
2012-09-26 19:57:31 +00:00
parent 77eff6b47d
commit 824fbd9b06
2 changed files with 64 additions and 6 deletions

View File

@@ -2300,17 +2300,59 @@ namespace boost {
return caster_type::lexical_cast_impl(arg);
}
template <typename Target, typename CharType>
inline Target lexical_cast(const CharType* chars, std::size_t count)
template <typename Target>
inline Target lexical_cast(const char* chars, std::size_t count)
{
BOOST_STATIC_ASSERT_MSG(::boost::detail::is_char_or_wchar<CharType>::value,
"CharType must be a character or wide character type");
return ::boost::lexical_cast<Target>(
::boost::iterator_range<const CharType*>(chars, chars + count)
::boost::iterator_range<const char*>(chars, chars + count)
);
}
template <typename Target>
inline Target lexical_cast(const unsigned char* chars, std::size_t count)
{
return ::boost::lexical_cast<Target>(
::boost::iterator_range<const unsigned char*>(chars, chars + count)
);
}
template <typename Target>
inline Target lexical_cast(const signed char* chars, std::size_t count)
{
return ::boost::lexical_cast<Target>(
::boost::iterator_range<const signed char*>(chars, chars + count)
);
}
#ifndef BOOST_LCAST_NO_WCHAR_T
template <typename Target>
inline Target lexical_cast(const wchar_t* chars, std::size_t count)
{
return ::boost::lexical_cast<Target>(
::boost::iterator_range<const wchar_t*>(chars, chars + count)
);
}
#endif
#ifndef BOOST_NO_CHAR16_T
template <typename Target>
inline Target lexical_cast(const char16_t* chars, std::size_t count)
{
return ::boost::lexical_cast<Target>(
::boost::iterator_range<const char16_t*>(chars, chars + count)
);
}
#endif
#ifndef BOOST_NO_CHAR32_T
template <typename Target>
inline Target lexical_cast(const char32_t* chars, std::size_t count)
{
return ::boost::lexical_cast<Target>(
::boost::iterator_range<const char32_t*>(chars, chars + count)
);
}
#endif
} // namespace boost
#else // #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION

View File

@@ -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 <class To, class From, class Func>
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<To, From> is not ambiguous
(void)&lexical_cast<int, const char*>;
BOOST_CHECK_EQUAL(100, try_cast_by_ptr<int>("100", &boost::lexical_cast<int, const char[4]>));
BOOST_CHECK_EQUAL(100, try_cast_by_ptr<int>("100", &boost::lexical_cast<int, std::string>));
BOOST_CHECK_EQUAL(std::string("100"), try_cast_by_ptr<std::string>(100, &boost::lexical_cast<std::string, int>));
}