Skip to content

Commit

Permalink
[C++] Make code compile with -Wfloat-equal
Browse files Browse the repository at this point in the history
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<typename T> 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 ==.
  • Loading branch information
fliiiix committed Jan 21, 2024
1 parent 129ef42 commit 9d04c2b
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/flatbuffers/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,13 @@ inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) {
return ((~buf_size) + 1) & (scalar_size - 1);
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
// Generic 'operator==' with conditional specialisations.
// T e - new value of a scalar field.
// T def - default of scalar (is known at compile-time).
template<typename T> inline bool IsTheSameAs(T e, T def) { return e == def; }
#pragma GCC diagnostic pop

#if defined(FLATBUFFERS_NAN_DEFAULTS) && \
defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
Expand Down

0 comments on commit 9d04c2b

Please sign in to comment.