fix: use strtod fallback when std::from_chars(float) unavailable#572
Conversation
|
Can we implement this by SFINAE or concept instead of using macro? |
I tried, but it still requires relying on the _LIBCPP_VERSION macro, I would appreciate your suggestions if there is a better solution. |
For examples, #include <charconv>
#include <string_view>
#include <utility>
#include <iostream>
template <typename T>
concept FromChars = requires(const char* p, T& v) { std::from_chars(p, p, v); };
template <typename T>
requires std::is_arithmetic_v<T> && (!std::same_as<T, bool>)
&& FromChars<T>
static T ParseNumber(std::string_view str) {
T value = 0;
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value);
if (ec == std::errc()) [[likely]] {
return value;
}
std::unreachable();
}
template <typename T>
requires std::floating_point<T> && (!FromChars<T>)
static T ParseNumber(std::string_view str) {
char* end = nullptr;
return std::strtod(str.data(), &end);
}
int main() {
auto v = ParseNumber<double>("123.12");
std::cout << v << std::endl;
} |
src/iceberg/util/string_util.h
Outdated
There was a problem hiding this comment.
I think that you need to add FromChars<T> here otherwise old compiler will report error because both requires of the two functions are satisfied.
There was a problem hiding this comment.
Yeah, I don't have my old mac with me right now. I'll give it a try once I have access to it.
There was a problem hiding this comment.
I just tried it, and it works now, thanks for catching that.
closes #571