Skip to content

Commit

Permalink
Guard against min/max being macros in document.h
Browse files Browse the repository at this point in the history
Sometimes, particularly when Microsoft's windows.h is included, min/max
are defined as macros, interfering with use of
std::numeric_limits::min() and the like.

To guard against this, the function name is wrapped in an extra set of
parenthesis, which inhibits function-style macro expansion.
  • Loading branch information
chwarr committed Sep 6, 2017
1 parent 1e46091 commit 6e38649
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions include/rapidjson/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ RAPIDJSON_DIAG_PUSH
#ifdef _MSC_VER
RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data
#ifdef _MINWINDEF_ // see: http://stackoverflow.com/questions/22744262/cant-call-stdmax-because-minwindef-h-defines-max
#ifndef NOMINMAX
#pragma push_macro("min")
#pragma push_macro("max")
#undef min
#undef max
#endif
#endif
#endif

#ifdef __clang__
Expand Down Expand Up @@ -1018,14 +1010,14 @@ class GenericValue {
uint64_t u = GetUint64();
volatile double d = static_cast<double>(u);
return (d >= 0.0)
&& (d < static_cast<double>(std::numeric_limits<uint64_t>::max()))
&& (d < static_cast<double>((std::numeric_limits<uint64_t>::max)()))
&& (u == static_cast<uint64_t>(d));
}
if (IsInt64()) {
int64_t i = GetInt64();
volatile double d = static_cast<double>(i);
return (d >= static_cast<double>(std::numeric_limits<int64_t>::min()))
&& (d < static_cast<double>(std::numeric_limits<int64_t>::max()))
return (d >= static_cast<double>((std::numeric_limits<int64_t>::min)()))
&& (d < static_cast<double>((std::numeric_limits<int64_t>::max)()))
&& (i == static_cast<int64_t>(d));
}
return true; // double, int, uint are always lossless
Expand All @@ -1042,8 +1034,8 @@ class GenericValue {
bool IsLosslessFloat() const {
if (!IsNumber()) return false;
double a = GetDouble();
if (a < static_cast<double>(-std::numeric_limits<float>::max())
|| a > static_cast<double>(std::numeric_limits<float>::max()))
if (a < static_cast<double>(-(std::numeric_limits<float>::max)())
|| a > static_cast<double>((std::numeric_limits<float>::max)()))
return false;
double b = static_cast<double>(static_cast<float>(a));
return a >= b && a <= b; // Prevent -Wfloat-equal
Expand Down Expand Up @@ -2616,12 +2608,6 @@ class GenericObject {
};

RAPIDJSON_NAMESPACE_END
#ifdef _MINWINDEF_ // see: http://stackoverflow.com/questions/22744262/cant-call-stdmax-because-minwindef-h-defines-max
#ifndef NOMINMAX
#pragma pop_macro("min")
#pragma pop_macro("max")
#endif
#endif
RAPIDJSON_DIAG_POP

#endif // RAPIDJSON_DOCUMENT_H_

0 comments on commit 6e38649

Please sign in to comment.