convert_from_string.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright John Maddock 2016.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_TOOLS_CONVERT_FROM_STRING_INCLUDED
  6. #define BOOST_MATH_TOOLS_CONVERT_FROM_STRING_INCLUDED
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <boost/type_traits/is_constructible.hpp>
  11. #include <boost/type_traits/conditional.hpp>
  12. #include <boost/lexical_cast.hpp>
  13. namespace boost{ namespace math{ namespace tools{
  14. template <class T>
  15. struct convert_from_string_result
  16. {
  17. typedef typename boost::conditional<std::is_constructible<T, const char*>::value, const char*, T>::type type;
  18. };
  19. template <class Real>
  20. Real convert_from_string(const char* p, const std::false_type&)
  21. {
  22. #ifdef BOOST_MATH_NO_LEXICAL_CAST
  23. // This function should not compile, we don't have the necessary functionality to support it:
  24. BOOST_STATIC_ASSERT(sizeof(Real) == 0);
  25. #else
  26. return boost::lexical_cast<Real>(p);
  27. #endif
  28. }
  29. template <class Real>
  30. BOOST_CONSTEXPR const char* convert_from_string(const char* p, const std::true_type&) BOOST_NOEXCEPT
  31. {
  32. return p;
  33. }
  34. template <class Real>
  35. BOOST_CONSTEXPR typename convert_from_string_result<Real>::type convert_from_string(const char* p) BOOST_NOEXCEPT_IF((std::is_constructible<Real, const char*>::value))
  36. {
  37. return convert_from_string<Real>(p, std::is_constructible<Real, const char*>());
  38. }
  39. } // namespace tools
  40. } // namespace math
  41. } // namespace boost
  42. #endif // BOOST_MATH_TOOLS_CONVERT_FROM_STRING_INCLUDED