HuaHuaY commented on PR #572:
URL: https://github.com/apache/iceberg-cpp/pull/572#issuecomment-3950201156
> > 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,
```cpp
#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;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]