From 12ada57ee8d0f2cf0af07667853a9318783f0a84 Mon Sep 17 00:00:00 2001 From: hg <4683435+hgtw@users.noreply.github.com> Date: Sun, 24 Nov 2024 18:18:36 -0500 Subject: [PATCH] [Code] Fix build with older C++ libraries (#4549) This adds a compile time concept to determine if from_chars has floating-point support and uses fallbacks if not. This is a C++17 feature but support for floats was only added to libstdc++ with GCC 11.1 and LLVM libc++ in 20.0 (unreleased). --- common/mysql_stmt.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/common/mysql_stmt.cpp b/common/mysql_stmt.cpp index 0c71aa53c..872df0086 100644 --- a/common/mysql_stmt.cpp +++ b/common/mysql_stmt.cpp @@ -414,6 +414,12 @@ static uint64_t MakeBits(std::span data) return bits; } +template +concept has_from_chars = requires (const char* first, const char* last, T value) +{ + std::from_chars(first, last, value); +}; + template static T FromString(std::string_view sv) { @@ -422,6 +428,14 @@ static T FromString(std::string_view sv) // return false for empty (zero-length) strings return !sv.empty(); } + else if constexpr (std::is_same_v && !has_from_chars) + { + return std::strtof(std::string(sv).c_str(), nullptr); + } + else if constexpr (std::is_same_v && !has_from_chars) + { + return std::strtod(std::string(sv).c_str(), nullptr); + } else { // non numbers return a zero initialized T (could return nullopt instead)