Merege fix for #7421 from trunk

[SVN r80787]
This commit is contained in:
Antony Polukhin
2012-09-30 16:07:32 +00:00
parent 8d373a0c99
commit 0920c53e23
3 changed files with 65 additions and 8 deletions

View File

@@ -107,8 +107,8 @@ Library features defined in [@boost:boost/lexical_cast.hpp boost/lexical_cast.hp
Returns the result of streaming arg into a standard library string-based stream and then out as a Target object. Where Target is either `std::string` or `std::wstring`, stream extraction takes the whole content of the string, including spaces, rather than relying on the default `operator>>` behavior. If the conversion is unsuccessful, a `bad_lexical_cast` exception is thrown.
``
template <typename Target, typename CharType>
Target lexical_cast(const CharType* chars, std::size_t count);
template <typename Target>
Target lexical_cast(const AnyCharacterType* chars, std::size_t count);
``
Takes an array of `count` characters as input parameter and streams them out as a Target object. If the conversion is unsuccessful, a `bad_lexical_cast` exception is thrown. This call may be useful for processing nonzero terminated array of characters or processing just some part of character array.

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,17 @@ 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
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>));
}