Remove charconv requirement (#142)

* Remove charconv requirement

It's the most restrictive compiler requirement (gcc 8.1, clang 7)

* Use has_include preprocessor function
This commit is contained in:
Nicholas Smith 2021-01-28 18:04:23 -06:00 committed by GitHub
parent 6f73b76c5f
commit 15aa1380de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,7 +11,9 @@
#include <any> #include <any>
#include <cassert> #include <cassert>
#include <cctype> #include <cctype>
#if __has_include(<charconv>)
#include <charconv> #include <charconv>
#endif
#include <cstring> #include <cstring>
#include <functional> #include <functional>
#include <initializer_list> #include <initializer_list>
@ -478,6 +480,7 @@ struct SemanticValues : protected std::vector<std::any> {
template <typename T> T token_to_number() const { template <typename T> T token_to_number() const {
T n = 0; T n = 0;
#if __has_include(<charconv>)
if constexpr (std::is_floating_point<T>::value) { if constexpr (std::is_floating_point<T>::value) {
// TODO: The following code should be removed eventually. // TODO: The following code should be removed eventually.
std::istringstream ss(token_to_string()); std::istringstream ss(token_to_string());
@ -488,6 +491,11 @@ struct SemanticValues : protected std::vector<std::any> {
std::from_chars(sv.data(), sv.data() + sv.size(), n); std::from_chars(sv.data(), sv.data() + sv.size(), n);
return n; return n;
} }
#else
std::istringstream ss(token_to_string());
ss >> n;
return n;
#endif
} }
// Transform the semantic value vector to another vector // Transform the semantic value vector to another vector
@ -3770,6 +3778,7 @@ template <typename Annotation> struct AstBase : public Annotation {
template <typename T> T token_to_number() const { template <typename T> T token_to_number() const {
T n = 0; T n = 0;
#if __has_include(<charconv>)
if constexpr (std::is_floating_point<T>::value) { if constexpr (std::is_floating_point<T>::value) {
// TODO: The following code should be removed eventually. // TODO: The following code should be removed eventually.
std::istringstream ss(token_to_string()); std::istringstream ss(token_to_string());
@ -3780,6 +3789,11 @@ template <typename Annotation> struct AstBase : public Annotation {
std::from_chars(token.data(), token.data() + token.size(), n); std::from_chars(token.data(), token.data() + token.size(), n);
return n; return n;
} }
#else
std::istringstream ss(token_to_string());
ss >> n;
return n;
#endif
} }
}; };