|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r80715 - in trunk: boost libs/conversion
From: antoshkka_at_[hidden]
Date: 2012-09-26 15:57:32
Author: apolukhin
Date: 2012-09-26 15:57:31 EDT (Wed, 26 Sep 2012)
New Revision: 80715
URL: http://svn.boost.org/trac/boost/changeset/80715
Log:
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
Text files modified:
trunk/boost/lexical_cast.hpp | 52 ++++++++++++++++++++++++++++++++++++---
trunk/libs/conversion/lexical_cast_test.cpp | 16 ++++++++++++
2 files changed, 63 insertions(+), 5 deletions(-)
Modified: trunk/boost/lexical_cast.hpp
==============================================================================
--- trunk/boost/lexical_cast.hpp (original)
+++ trunk/boost/lexical_cast.hpp 2012-09-26 15:57:31 EDT (Wed, 26 Sep 2012)
@@ -2300,17 +2300,59 @@
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 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 CharType*>(chars, chars + count)
+ ::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
Modified: trunk/libs/conversion/lexical_cast_test.cpp
==============================================================================
--- trunk/libs/conversion/lexical_cast_test.cpp (original)
+++ trunk/libs/conversion/lexical_cast_test.cpp 2012-09-26 15:57:31 EDT (Wed, 26 Sep 2012)
@@ -88,6 +88,7 @@
#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 @@
#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 @@
}
#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>));
+}
+
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk