55
66#include < charconv>
77#include < concepts>
8+ #include < sstream>
89#include < string_view>
910#include < system_error>
1011#include < typeinfo>
@@ -55,7 +56,7 @@ T string_to_impl(std::string_view str, Arg &&arg) {
5556} // namespace
5657
5758template <typename T>
58- concept string_to_supports =
59+ concept from_chars_supports =
5960 requires (const char *c, T &v) { std::from_chars (c, c + 1 , v); };
6061
6162// / Converts the provided string to the desired integral type.
@@ -74,7 +75,7 @@ concept string_to_supports =
7475// / the parsed value can't be represented as a T.
7576template <std::integral T>
7677T string_to (std::string_view str, int base = 10 ) {
77- static_assert (string_to_supports <T>,
78+ static_assert (from_chars_supports <T>,
7879 " Your C++ standard library is missing a std::from_chars "
7980 " implementation for this integral type" );
8081 return string_to_impl<T>(str, base);
@@ -97,11 +98,28 @@ T string_to(std::string_view str, int base = 10) {
9798template <std::floating_point T>
9899T string_to (std::string_view str,
99100 std::chars_format fmt = std::chars_format::general) {
100- static_assert (string_to_supports<T>,
101+ #ifndef __APPLE__
102+ static_assert (from_chars_supports<T>,
101103 " Your C++ standard library is missing a std::from_chars "
102104 " implementation for this floating point type" );
103105
104106 return string_to_impl<T>(str, fmt);
107+ #else
108+ // For some reason it seems that Apple is unable to supply an implementation
109+ // of std::from_chars so we need to work around its (potential) absence
110+ if constexpr (from_chars_supports<T>) {
111+ // In case they update their standard lib…
112+ return string_to_impl<T>(str, fmt);
113+ }
114+
115+ // Workaround implementation that doesn't do any error checking - not great
116+ // but better than not being able to use this function at all
117+ std::stringstream stream{std::string (str)};
118+ T val = 0 ;
119+ stream >> val;
120+
121+ return val;
122+ #endif
105123}
106124
107125} // namespace sequant
0 commit comments