From 0c91bbc4ba0275f99e65a8ea948885f6176d29f2 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 20 Jul 2026 20:03:57 +0000 Subject: [PATCH] Guard from_chars use on JSON_HAS_CPP_17, not just __cpp_lib_to_chars libstdc++ 15 defines __cpp_lib_to_chars even in C++14 mode (via bits/version.h pulled in by other headers), but is only included under JSON_HAS_CPP_17. That made parse_float_from_chars() reference std::from_chars without the header in C++14 builds, breaking gcc-latest, icpx, and the offline-testdata jobs. Gate the use on JSON_HAS_CPP_17 && __cpp_lib_to_chars so it matches the include condition exactly; C++11/14 always take the scalar fallback. Verified by forcing __cpp_lib_to_chars in a C++14 build: the guard suppresses std::from_chars and it compiles. C++17 behavior is unchanged. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01AXcDtEma2PjxgmPS9cQGzA Signed-off-by: Niels Lohmann --- include/nlohmann/detail/input/number_parse.hpp | 5 ++++- single_include/nlohmann/json.hpp | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/input/number_parse.hpp b/include/nlohmann/detail/input/number_parse.hpp index 1a2d6d489..4da712f81 100644 --- a/include/nlohmann/detail/input/number_parse.hpp +++ b/include/nlohmann/detail/input/number_parse.hpp @@ -263,7 +263,10 @@ expects (side-stepping the P4168 divergence between implementations). template bool parse_float_from_chars(const char* first, const char* last, FloatType& out) noexcept { -#if defined(__cpp_lib_to_chars) + // JSON_HAS_CPP_17 must gate the use as well as the include above: + // some standard libraries (e.g. libstdc++ 15) define __cpp_lib_to_chars even + // in C++14 mode, where is not included. +#if defined(JSON_HAS_CPP_17) && defined(__cpp_lib_to_chars) const auto result = std::from_chars(first, last, out); return result.ec == std::errc() && result.ptr == last; #else diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 240ef7db4..f40630517 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -8046,7 +8046,10 @@ expects (side-stepping the P4168 divergence between implementations). template bool parse_float_from_chars(const char* first, const char* last, FloatType& out) noexcept { -#if defined(__cpp_lib_to_chars) + // JSON_HAS_CPP_17 must gate the use as well as the include above: + // some standard libraries (e.g. libstdc++ 15) define __cpp_lib_to_chars even + // in C++14 mode, where is not included. +#if defined(JSON_HAS_CPP_17) && defined(__cpp_lib_to_chars) const auto result = std::from_chars(first, last, out); return result.ec == std::errc() && result.ptr == last; #else