Skip to content

Commit 6e38649

Browse files
committed
Guard against min/max being macros in document.h
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.
1 parent 1e46091 commit 6e38649

File tree

1 file changed

+5
-19
lines changed

1 file changed

+5
-19
lines changed

include/rapidjson/document.h

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ RAPIDJSON_DIAG_PUSH
2929
#ifdef _MSC_VER
3030
RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
3131
RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data
32-
#ifdef _MINWINDEF_ // see: http://stackoverflow.com/questions/22744262/cant-call-stdmax-because-minwindef-h-defines-max
33-
#ifndef NOMINMAX
34-
#pragma push_macro("min")
35-
#pragma push_macro("max")
36-
#undef min
37-
#undef max
38-
#endif
39-
#endif
4032
#endif
4133

4234
#ifdef __clang__
@@ -1018,14 +1010,14 @@ class GenericValue {
10181010
uint64_t u = GetUint64();
10191011
volatile double d = static_cast<double>(u);
10201012
return (d >= 0.0)
1021-
&& (d < static_cast<double>(std::numeric_limits<uint64_t>::max()))
1013+
&& (d < static_cast<double>((std::numeric_limits<uint64_t>::max)()))
10221014
&& (u == static_cast<uint64_t>(d));
10231015
}
10241016
if (IsInt64()) {
10251017
int64_t i = GetInt64();
10261018
volatile double d = static_cast<double>(i);
1027-
return (d >= static_cast<double>(std::numeric_limits<int64_t>::min()))
1028-
&& (d < static_cast<double>(std::numeric_limits<int64_t>::max()))
1019+
return (d >= static_cast<double>((std::numeric_limits<int64_t>::min)()))
1020+
&& (d < static_cast<double>((std::numeric_limits<int64_t>::max)()))
10291021
&& (i == static_cast<int64_t>(d));
10301022
}
10311023
return true; // double, int, uint are always lossless
@@ -1042,8 +1034,8 @@ class GenericValue {
10421034
bool IsLosslessFloat() const {
10431035
if (!IsNumber()) return false;
10441036
double a = GetDouble();
1045-
if (a < static_cast<double>(-std::numeric_limits<float>::max())
1046-
|| a > static_cast<double>(std::numeric_limits<float>::max()))
1037+
if (a < static_cast<double>(-(std::numeric_limits<float>::max)())
1038+
|| a > static_cast<double>((std::numeric_limits<float>::max)()))
10471039
return false;
10481040
double b = static_cast<double>(static_cast<float>(a));
10491041
return a >= b && a <= b; // Prevent -Wfloat-equal
@@ -2616,12 +2608,6 @@ class GenericObject {
26162608
};
26172609

26182610
RAPIDJSON_NAMESPACE_END
2619-
#ifdef _MINWINDEF_ // see: http://stackoverflow.com/questions/22744262/cant-call-stdmax-because-minwindef-h-defines-max
2620-
#ifndef NOMINMAX
2621-
#pragma pop_macro("min")
2622-
#pragma pop_macro("max")
2623-
#endif
2624-
#endif
26252611
RAPIDJSON_DIAG_POP
26262612

26272613
#endif // RAPIDJSON_DOCUMENT_H_

0 commit comments

Comments
 (0)