1// Copyright John Maddock 2016.
2// Copyright Matt Borland 2023.
3// Use, modification and distribution are subject to the
4// Boost Software License, Version 1.0. (See accompanying file
5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7#ifndef BOOST_MATH_TOOLS_CONVERT_FROM_STRING_INCLUDED
8#define BOOST_MATH_TOOLS_CONVERT_FROM_STRING_INCLUDED
9
10#ifdef _MSC_VER
11#pragma once
12#endif
13
14#include <boost/math/tools/config.hpp>
15#include <type_traits>
16#ifndef BOOST_MATH_STANDALONE
17#include <boost/lexical_cast.hpp>
18#endif
19
20namespace boost{ namespace math{ namespace tools{
21
22 template <class T>
23 struct convert_from_string_result
24 {
25 typedef typename std::conditional<std::is_constructible<T, const char*>::value, const char*, T>::type type;
26 };
27
28 template <class Real>
29 Real convert_from_string(const char* p, const std::false_type&)
30 {
31 #ifdef BOOST_MATH_NO_LEXICAL_CAST
32
33 // This function should not compile, we don't have the necessary functionality to support it:
34 static_assert(sizeof(Real) == 0, "boost.lexical_cast is not supported in standalone mode.");
35 (void)p; // Suppresses -Wunused-parameter
36 return Real(0);
37
38 #elif defined(BOOST_MATH_USE_CHARCONV_FOR_CONVERSION)
39
40 if constexpr (std::is_arithmetic_v<Real>)
41 {
42 Real v {};
43 std::from_chars(p, p + std::strlen(p), v);
44
45 return v;
46 }
47 else
48 {
49 return boost::lexical_cast<Real>(p);
50 }
51
52 #else
53
54 return boost::lexical_cast<Real>(p);
55
56 #endif
57 }
58 template <class Real>
59 constexpr const char* convert_from_string(const char* p, const std::true_type&) noexcept
60 {
61 return p;
62 }
63 template <class Real>
64 constexpr typename convert_from_string_result<Real>::type convert_from_string(const char* p) noexcept((std::is_constructible<Real, const char*>::value))
65 {
66 return convert_from_string<Real>(p, std::is_constructible<Real, const char*>());
67 }
68
69} // namespace tools
70} // namespace math
71} // namespace boost
72
73#endif // BOOST_MATH_TOOLS_CONVERT_FROM_STRING_INCLUDED
74
75

source code of include/boost/math/tools/convert_from_string.hpp