QmlDesigner: Fix compile

Use:
if constexpr (std::is_floating_point_v<Type> && hasNoFloatStdToChar() {
to prevent instantiation of the std::to_chars branch which avoids
ambiguous overload errors.

Use:
static_cast<std::size_t> to fix narrowing conversion warning

Change-Id: I8058e7b5cf520165bd8e8f449fe87f81ad403cf4
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Tim Jenssen
2025-03-20 20:45:24 +01:00
committed by Marco Bubke
parent 6fd2efab6b
commit 1acd199665

View File

@@ -9,13 +9,6 @@
#include <charconv> #include <charconv>
#include <limits> #include <limits>
#if !defined(__cpp_lib_to_chars) || (__cpp_lib_to_chars < 201611L) || \
(defined(__GNUC__) && __GNUC__ < 11) || \
(defined(_MSC_VER) && _MSC_VER < 1930) || \
(defined(__clang_major__) && __clang_major__ < 14)
#define NO_STD_FLOAT_TO_CHARS 1
#endif
namespace NanotraceHR { namespace NanotraceHR {
template<std::size_t Capacity> template<std::size_t Capacity>
@@ -58,26 +51,34 @@ public:
} }
} }
static consteval bool hasNoFloatStdToChar()
{
#if !defined(__cpp_lib_to_chars) || (__cpp_lib_to_chars < 201611L) || \
(defined(__GNUC__) && __GNUC__ < 11) || \
(defined(_MSC_VER) && _MSC_VER < 1930) || \
(defined(__clang_major__) && __clang_major__ < 14)
return true;
#else
return false;
#endif
}
template<typename Type, typename std::enable_if_t<std::is_arithmetic_v<Type>, bool> = true> template<typename Type, typename std::enable_if_t<std::is_arithmetic_v<Type>, bool> = true>
void append(Type number) void append(Type number)
{ {
#if NO_STD_FLOAT_TO_CHARS if constexpr (std::is_floating_point_v<Type> && hasNoFloatStdToChar()) {
// Fallback using snprintf with sufficient precision. // Fallback using snprintf for floating point numbers
if constexpr (std::is_floating_point_v<Type>) {
char buffer[std::numeric_limits<Type>::max_digits10 + 2]; char buffer[std::numeric_limits<Type>::max_digits10 + 2];
auto size = std::snprintf(buffer, sizeof(buffer), "%.9f", number); auto size = std::snprintf(buffer, sizeof(buffer), "%.9f", number);
if (size >= 0) if (size >= 0)
append({buffer, size}); append({buffer, static_cast<std::size_t>(size)});
return; } else {
// 2 bytes for the sign and because digits10 returns the floor
char buffer[std::numeric_limits<Type>::digits10 + 2];
auto result = std::to_chars(buffer, buffer + sizeof(buffer), number);
auto endOfConversionString = result.ptr;
append({buffer, endOfConversionString});
} }
#endif
// 2 bytes for the sign and because digits10 returns the floor
char buffer[std::numeric_limits<Type>::digits10 + 2];
auto result = std::to_chars(buffer, buffer + sizeof(buffer), number);
auto endOfConversionString = result.ptr;
append({buffer, endOfConversionString});
} }
void pop_back() { --m_size; } void pop_back() { --m_size; }