From 0e034ecdba87ea5b22d6c523db7d9747e5d70c32 Mon Sep 17 00:00:00 2001 From: Felix Date: Wed, 29 May 2024 04:52:14 +0200 Subject: [PATCH] [C++] Make code compile with -Wfloat-equal (#8221) This will allow the code to be compiled with `-Wfloat-equal` as this would result in the folowing warning/error: vendor/flatbuffers/include/flatbuffers/base.h:465:69: error: comparing floating point with == or != is unsafe [-Werror,-Wfloat-equal] template inline bool IsTheSameAs(T e, T def) { return e == def; } But the way it is used in flatbuffers it is ok to compare floating points with ==. Co-authored-by: Derek Bailey --- include/flatbuffers/base.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/flatbuffers/base.h b/include/flatbuffers/base.h index 1c19dde98..a782cb1a9 100644 --- a/include/flatbuffers/base.h +++ b/include/flatbuffers/base.h @@ -459,10 +459,17 @@ inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) { return ((~buf_size) + 1) & (scalar_size - 1); } +#if !defined(_MSC_VER) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wfloat-equal" +#endif // Generic 'operator==' with conditional specialisations. // T e - new value of a scalar field. // T def - default of scalar (is known at compile-time). template inline bool IsTheSameAs(T e, T def) { return e == def; } +#if !defined(_MSC_VER) + #pragma GCC diagnostic pop +#endif #if defined(FLATBUFFERS_NAN_DEFAULTS) && \ defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)