86 lines · plain
1// Copyright John Maddock 2016.2// Copyright Matt Borland 2023.3// Use, modification and distribution are subject to the4// Boost Software License, Version 1.0. (See accompanying file5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)6 7#ifndef BOOST_MATH_TOOLS_CONVERT_FROM_STRING_INCLUDED8#define BOOST_MATH_TOOLS_CONVERT_FROM_STRING_INCLUDED9 10#ifdef _MSC_VER11#pragma once12#endif13 14#include <boost/math/tools/config.hpp>15#include <type_traits>16#ifndef BOOST_MATH_STANDALONE17 18#if defined(_MSC_VER) || defined(__GNUC__)19# pragma push_macro( "I" )20# undef I21#endif22 23#include <boost/lexical_cast.hpp>24 25#if defined(_MSC_VER) || defined(__GNUC__)26# pragma pop_macro( "I" )27#endif28 29#endif30 31namespace boost{ namespace math{ namespace tools{32 33 template <class T>34 struct convert_from_string_result35 {36 typedef typename std::conditional<std::is_constructible<T, const char*>::value, const char*, T>::type type;37 };38 39 template <class Real>40 Real convert_from_string(const char* p, const std::false_type&)41 {42 #ifdef BOOST_MATH_NO_LEXICAL_CAST43 44 // This function should not compile, we don't have the necessary functionality to support it:45 static_assert(sizeof(Real) == 0, "boost.lexical_cast is not supported in standalone mode.");46 (void)p; // Suppresses -Wunused-parameter47 return Real(0);48 49 #elif defined(BOOST_MATH_USE_CHARCONV_FOR_CONVERSION)50 51 if constexpr (std::is_arithmetic_v<Real>)52 {53 Real v {};54 std::from_chars(p, p + std::strlen(p), v);55 56 return v;57 }58 else59 {60 return boost::lexical_cast<Real>(p);61 }62 63 #else64 65 return boost::lexical_cast<Real>(p);66 67 #endif68 }69 template <class Real>70 constexpr const char* convert_from_string(const char* p, const std::true_type&) noexcept71 {72 return p;73 }74 template <class Real>75 constexpr typename convert_from_string_result<Real>::type convert_from_string(const char* p) noexcept((std::is_constructible<Real, const char*>::value))76 {77 return convert_from_string<Real>(p, std::is_constructible<Real, const char*>());78 }79 80} // namespace tools81} // namespace math82} // namespace boost83 84#endif // BOOST_MATH_TOOLS_CONVERT_FROM_STRING_INCLUDED85 86