Skip to content

fix: use strtod fallback when std::from_chars(float) unavailable#572

Merged
wgtmac merged 4 commits intoapache:mainfrom
zhjwpku:fix_parser_number_when_linking_libc++
Feb 25, 2026
Merged

fix: use strtod fallback when std::from_chars(float) unavailable#572
wgtmac merged 4 commits intoapache:mainfrom
zhjwpku:fix_parser_number_when_linking_libc++

Conversation

@zhjwpku
Copy link
Collaborator

@zhjwpku zhjwpku commented Feb 22, 2026

closes #571

@HuaHuaY
Copy link
Contributor

HuaHuaY commented Feb 22, 2026

Can we implement this by SFINAE or concept instead of using macro?

@zhjwpku
Copy link
Collaborator Author

zhjwpku commented Feb 23, 2026

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.

@HuaHuaY
Copy link
Contributor

HuaHuaY commented Feb 24, 2026

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;
}

Copy link
Contributor

@HuaHuaY HuaHuaY Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't have my old mac with me right now. I'll give it a try once I have access to it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried it, and it works now, thanks for catching that.

Copy link
Contributor

@HuaHuaY HuaHuaY left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wgtmac
Copy link
Member

wgtmac commented Feb 25, 2026

Thanks @zhjwpku for fixing this and @HuaHuaY for the review!

@wgtmac wgtmac merged commit 9826147 into apache:main Feb 25, 2026
12 checks passed
@zhjwpku zhjwpku deleted the fix_parser_number_when_linking_libc++ branch February 25, 2026 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libc++ on Apple platforms may not provide floating-point std::from_chars

3 participants